Example usage for java.lang.reflect Method getName

List of usage examples for java.lang.reflect Method getName

Introduction

In this page you can find the example usage for java.lang.reflect Method getName.

Prototype

@Override
public String getName() 

Source Link

Document

Returns the name of the method represented by this Method object, as a String .

Usage

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(name, "Method name must not be null");
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName()) && (paramTypes == null || paramTypes.length == 0
                    || paramTypes[0] == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }// ww  w  .j  a  v a 2  s  . c  o m
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.startechup.tools.ModelParser.java

/**
 * Invokes the setter method that was link to the json key.
 *
 * @param method The setter method to be invoked.
 * @param classInstance The instance of the container class.
 * @param key The json key serving as the reference of the value in the json object
 * @param jsonObject The API response object.
 *///from w  ww  . ja  va  2s . co m
private static void initMethodInvocation(Method method, Object classInstance, String key,
        JSONObject jsonObject) {
    Object value = getValueFromJsonObject(jsonObject, key, method.getName());

    // Only invoke the method when the value is not null
    if (value != null) {
        method.setAccessible(true);
        Object castedObject = value;

        if (value instanceof Number) {
            castedObject = castNumberObject(method.getParameterTypes()[0], value);
        } else if (value instanceof JSONArray) {
            if (method.getParameterTypes()[0].isArray()) {
                //TODO find a way to genetically convert json array to array, for now throw our custom exception
                throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]);
            } else {
                Object parameterInstance = getNewInstance(method.getParameterTypes()[0]);
                if (parameterInstance instanceof Collection) {
                    ParameterizedType parameterizedType = (ParameterizedType) method
                            .getGenericParameterTypes()[0];
                    Class<?> classType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
                    castedObject = parse(classType, (JSONArray) value);
                } else {
                    //TODO find a way to genetically convert json array to the other parameter class, for now throw our custom exception
                    throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]);
                }
            }
        } else if (value instanceof JSONObject) {
            castedObject = parse(method.getParameterTypes()[0], ((JSONObject) value));
        }

        // Finally invoke the method after casting the values into the method parameter type
        invoke(method, classInstance, castedObject);
    }
}

From source file:cn.webwheel.ActionSetter.java

public static String isSetter(Method method) {
    if (Modifier.isStatic(method.getModifiers()))
        return null;
    String name = method.getName();
    if (!name.startsWith("set"))
        return null;
    if (name.length() < 4)
        return null;
    if (method.getParameterTypes().length != 1)
        return null;
    name = name.substring(3);/*from  www .ja va2  s . c o m*/
    if (name.length() > 1 && name.equals(name.toUpperCase()))
        return name;
    return name.substring(0, 1).toLowerCase() + name.substring(1);
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Method findSetter(Class<?> clazz, String propName) {
    String expectedName = "set" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1);
    for (Method method : clazz.getMethods()) {
        if (!method.getName().equals(expectedName)) {
            continue;
        }//from w  w w  .  j a  v a 2 s  .  c  o m
        if (!method.getReturnType().equals(void.class)) {
            continue;
        }
        Type[] argTypes = method.getGenericParameterTypes();
        if (argTypes == null || argTypes.length != 1) {
            continue;
        }
        return method;
    }
    return null;
}

From source file:com.kcs.core.utilities.Utility.java

public static void traceObject(Object sourceObj) {
    try {//w  w w  . j av a2 s  .  c  o  m
        Method idMethods[] = sourceObj.getClass().getMethods();
        logger.debug("************************************************************");
        for (int j = 0; j < idMethods.length; j++) {
            try {
                Method idMethod = idMethods[j];
                if (idMethod.getName().startsWith("get") && !"getClass".equals(idMethod.getName())) {
                    logger.debug(sourceObj.getClass().getName() + " " + idMethod.getName() + " = "
                            + idMethod.invoke(sourceObj));
                }
            } catch (Exception e) {
            }
        }
    } catch (Exception ex) {
        //
    }
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Find first method with given name.// ww  w .  j av a 2  s  .c om
 *
 * @param clazz the clazz
 * @param method the method
 * @return null if not found
 */
public static Method findFirstMethod(Class<?> clazz, String method) {
    for (Method m : clazz.getDeclaredMethods()) {
        if (m.getName().equals(method) == true) {
            return m;
        }
    }
    if (clazz.getSuperclass() != null) {
        return findFirstMethod(clazz.getSuperclass(), method);
    }
    return null;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

public static Map<String, Map<Class<?>, Method>> findUnaryMethods(Class<?> clazz) {
    Map<String, Map<Class<?>, Method>> result = new TreeMap<>();

    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        int modified = method.getModifiers();
        if (Modifier.isPublic(modified) && method.getParameterTypes().length == 1) {
            Map<Class<?>, Method> methodMap = result.get(method.getName());
            if (methodMap == null) {
                methodMap = new HashMap<>();
                result.put(method.getName(), methodMap);
            }//  ww  w  .j a  va2s.co  m
            methodMap.put(method.getParameterTypes()[0], method);
        }
    }

    return result;
}

From source file:ReflectUtil.java

/**
 * Fetches all methods of all access types from the supplied class and super
 * classes. Methods that have been overridden in the inheritance hierarchy are
 * only returned once, using the instance lowest down the hierarchy.
 *
 * @param clazz the class to inspect/*w  w  w .  j a v a2s .  c  o  m*/
 * @return a collection of methods
 */
public static Collection<Method> getMethods(Class<?> clazz) {
    Collection<Method> found = new ArrayList<Method>();
    while (clazz != null) {
        for (Method m1 : clazz.getDeclaredMethods()) {
            boolean overridden = false;

            for (Method m2 : found) {
                if (m2.getName().equals(m1.getName())
                        && Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) {
                    overridden = true;
                    break;
                }
            }

            if (!overridden)
                found.add(m1);
        }

        clazz = clazz.getSuperclass();
    }

    return found;
}

From source file:io.konik.utils.RandomInvoiceGenerator.java

private static void setValue(Object entity, Method entityMethod, Object paramValue)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (paramValue == null || entity == null)
        return;//from w  w  w. j a va2 s.  c o  m
    String methodToCall = entityMethod.getName().replace("get", "set");
    //      int repeadAdder = 1;
    if (entityMethod.getName().startsWith("add")) {
        Collection.class.isAssignableFrom(entityMethod.getReturnType());
        methodToCall = entityMethod.getName();//overwrite
        //         repeadAdder += random.nextInt(5);//repeat call for adder
    }
    Method setterOrAdder = MethodUtils.getMatchingAccessibleMethod(entity.getClass(), methodToCall,
            paramValue.getClass());
    if (setterOrAdder == null) {
        setterOrAdder = getAccessibleMethod(entity.getClass(), methodToCall,
                paramValue.getClass().getSuperclass());
    }
    if (setterOrAdder == null) {
        System.out.println(
                "Could not find setter on Class Instnace :" + entity.getClass().getSimpleName() + " Getter :"
                        + entityMethod.getName() + " has no setter. Ignoring value:" + paramValue.toString());
        return;
    }
    //repeat a 2 times for adder
    for (int i = 0; i < 2; i++) {
        invokeMethod(entity, setterOrAdder.getName(), paramValue);
    }
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Method findGetter(Class<?> clazz, String propName) {
    Set<String> expectedNames = new HashSet<>(
            Arrays.asList("get" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1),
                    "is" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1) //bool props
            ));/*w ww .  j  a v a 2  s  . c  o  m*/
    for (Method method : clazz.getMethods()) {
        String methodName = method.getName();
        if (!expectedNames.contains(methodName)) {
            continue;
        }
        if (method.getParameterCount() != 0) {
            continue; //getters take no arguments
        }
        Type returnType = method.getGenericReturnType();
        if (returnType.equals(void.class)) {
            continue; //getters return something
        }
        if (methodName.startsWith("is")
                && !(returnType.equals(Boolean.class) || returnType.equals(boolean.class))) {
            continue; //isSomething() only valid for booleans
        }
        return method;
    }
    return null;
}