Java Reflection Method Name getMethodAsAccessible(String methodName, Class clazz)

Here you can find the source of getMethodAsAccessible(String methodName, Class clazz)

Description

get Method As Accessible

License

Apache License

Declaration

public static <T> Method getMethodAsAccessible(String methodName,
            Class<T> clazz) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;

public class Main {
    public static <T> Method getMethodAsAccessible(String methodName,
            Class<T> clazz) {

        for (Method each : clazz.getDeclaredMethods()) {

            Method result = methodIsCorrectOrNull(methodName, each);
            if (result != null)
                return result;
        }/* w w  w  . j a  va2  s.  c o m*/

        for (Method each : clazz.getSuperclass().getDeclaredMethods()) {
            Method result = methodIsCorrectOrNull(methodName, each);
            if (result != null)
                return result;
        }

        throw new IllegalArgumentException("There is no method named "
                + methodName + " in class " + clazz);

    }

    private static Method methodIsCorrectOrNull(String methodName,
            Method each) {
        if (each.getName().equals(methodName)) {
            each.setAccessible(true);
            return each;
        }
        return null;
    }
}

Related

  1. getMethod(String methodName, Method method, boolean fromComponentType)
  2. getMethod(String name, Class pojoClass)
  3. getMethod(String name, Method method)
  4. getMethod(String name, String methodDesc, Class actual)
  5. getMethod(String strMethodPrefix, A instance, String strAttributeName, Class clazz)
  6. getMethodByName(Class clazz, String methodName)
  7. getMethodByName(Class aClass, String methodName, Class... params)
  8. getMethodByName(Class clazz, String methodName)
  9. getMethodByName(Class clazz, String methodName)