Java Reflection Method Name getMethod(Class klazz, String[] methodNames, int argCount)

Here you can find the source of getMethod(Class klazz, String[] methodNames, int argCount)

Description

get Method

License

Open Source License

Declaration

public static Method getMethod(Class klazz, String[] methodNames, int argCount) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;
import java.util.ArrayList;

public class Main {
    public static Method getMethod(Class klazz, String[] methodNames, int argCount) {
        if (klazz == null) {
            return null;
        }/*from   w  w w.  j  a  v a 2 s. co m*/
        for (String method : methodNames) {
            try {
                for (Method m : getAllMethods(klazz)) {
                    if (m.getName().equals(method)
                            && (argCount == -1 || m.getParameterTypes().length == argCount)) {
                        m.setAccessible(true);
                        return m;
                    }
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static Method[] getAllMethods(Class clazz) {
        ArrayList<Method> methods = new ArrayList<Method>();
        while (clazz != null) {
            for (Method m : clazz.getDeclaredMethods()) {
                methods.add(m);
            }
            clazz = clazz.getSuperclass();
        }
        return methods.toArray(new Method[methods.size()]);

    }
}

Related

  1. getMethod(Class clazz, String methodName, Class[] params)
  2. getMethod(Class clazz, String name)
  3. getMethod(Class clazz, String name, Class... args)
  4. getMethod(Class cls, String methodName, Class[] params)
  5. getMethod(Class clz, String methodName, Class expectedTypes[])
  6. getMethod(Class objClass, String methodName, Class argClass)
  7. getMethod(Class serviceClass, String methodName, Class... mapClass)
  8. getMethod(Class targetClass, String methodName, Class[] paramTypes)
  9. getMethod(Class targetClass, String name, Class paramClass)