Java Utililty Methods Reflection Method Name

List of utility methods to do Reflection Method Name

Description

The list of methods to do Reflection Method Name are organized into topic(s).

Method

MethodgetMethod(Class klass, String methodName, Class requestClass)
Gets method with expected methodName, and with one parameter whose type is requestClass or its super class.
Class<?> theRequestClass = requestClass;
while (theRequestClass != Object.class) {
    try {
        return klass.getMethod(methodName, theRequestClass);
    } catch (SecurityException e) {
    } catch (NoSuchMethodException e) {
    theRequestClass = theRequestClass.getSuperclass();
...
MethodgetMethod(Class klass, String methodName, Class... paramTypes)
get Method
List<Method> candidates = new ArrayList<Method>();
outer: for (Method method : klass.getMethods()) {
    if (method.getName().equals(methodName)) {
        Class<?>[] methodParamTypes = method.getParameterTypes();
        if (paramTypes.length == methodParamTypes.length
                || (method.isVarArgs() && paramTypes.length >= methodParamTypes.length - 1)) {
            if (method.isVarArgs()) {
                for (int i = 0; i < methodParamTypes.length - 1; i++) {
...
MethodgetMethod(Class klass, String name, String name2)
get Method
Method[] methods = klass.getMethods();
for (int i = 0; i < methods.length; ++i) {
    if (methods[i].getParameterTypes().length == 0) {
        if (methods[i].getName().equals(name) || methods[i].getName().equals(name2))
            return methods[i];
return null;
...
MethodgetMethod(Class theClass, String methodName, Class[] paramTypes)
get Method
Method method = null;
try {
    method = theClass.getDeclaredMethod(methodName, paramTypes);
    method.setAccessible(true);
} catch (NoSuchMethodException | SecurityException e) {
if (method == null) {
    Class<?> superClasss = theClass.getSuperclass();
...
MethodgetMethod(Class c, String name, Class... argTypes)
Get the named method from the class
try {
    return c.getMethod(name, argTypes);
} catch (NoSuchMethodException e) {
    throw new IllegalArgumentException(e);
MethodgetMethod(Class clazz, String methodName)
get Method
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {
    if (m.getName().equals(methodName)) {
        return m;
methods = clazz.getSuperclass().getDeclaredMethods();
for (Method m : methods) {
...
MethodgetMethod(Class clazz, String methodName, Class[] calledTypes)
get Method
Method[] testMethods = clazz.getMethods();
outer: for (int i = 0; i < testMethods.length; i++) {
    if (!testMethods[i].getName().equals(methodName)) {
        continue outer;
    Class<?>[] testTypes = testMethods[i].getParameterTypes();
    if (testTypes.length != calledTypes.length) {
        continue;
...
MethodgetMethod(Class type, String methodName, Class... params)
get Method
if (type != null && methodName != null && methodName.length() > 0) {
    try {
        if (params != null && params.length > 0 && params[0] != null) {
            return type.getMethod(methodName, params);
        } else {
            return type.getMethod(methodName);
    } catch (NoSuchMethodException e) {
...
MethodgetMethod(final Class atClass, final String name, final Class[] paramType)
get Method
try {
    return atClass.getMethod(name, paramType);
} catch (Exception e) {
    try {
        return atClass.getDeclaredMethod(name, paramType);
    } catch (Exception e1) {
        return null;
MethodgetMethod(final Class clazz, final String methodName, final boolean factoryMethod, final boolean failIfNotFound)
get Method
if (methodName == null)
    return null;
final Method method = findMethod(clazz, methodName, failIfNotFound);
if (method == null && !failIfNotFound)
    return null;
final int methodModifiers = method.getModifiers();
if (Modifier.isAbstract(methodModifiers))
    throw new UnsupportedOperationException("Method '" + methodName + "' cannot be abstract");
...