Java Reflection Method Name getMethodByName(Class clazz, String name)

Here you can find the source of getMethodByName(Class clazz, String name)

Description

get Method By Name

License

Open Source License

Declaration

private static Method getMethodByName(Class<?> clazz, String name) 

Method Source Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    private static Method getMethodByName(Class<?> clazz, String name) {
        Method method = getPublicMethodByName(clazz, name);
        if (method != null)
            return method;
        method = getProtectedMethodByName(clazz, name);
        if (method != null)
            return method;
        return null;
    }/*from w  w w  . ja  va 2s.com*/

    private static Method getPublicMethodByName(Class<?> clazz, String name) {
        Method[] methods = clazz.getMethods();
        for (Method ele : methods) {
            if (name.equals(ele.getName()))
                return ele;
        }
        return null;
    }

    private static Method getProtectedMethodByName(Class<?> clazz, String name) {
        if (clazz == null)
            return null;
        Method[] methods = clazz.getDeclaredMethods();
        for (Method ele : methods) {
            if (ele.isAccessible())
                continue;
            if (name.equals(ele.getName()))
                return ele;
        }
        Method method = getProtectedMethodByName(clazz.getSuperclass(), name);
        if (method != null)
            return method;
        return null;
    }
}

Related

  1. getMethodAsAccessible(String methodName, Class clazz)
  2. getMethodByName(Class clazz, String methodName)
  3. getMethodByName(Class aClass, String methodName, Class... params)
  4. getMethodByName(Class clazz, String methodName)
  5. getMethodByName(Class clazz, String methodName)
  6. getMethodByName(Class cls, String methodName)
  7. getMethodByName(Class type, String methodName)
  8. getMethodByName(final Class cls, final String action)
  9. getMethodByNameFromArray(Method[] methods, String methodName)