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

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

Description

Return the number of methods with a given name (with any argument types), for the given class and/or its superclasses.

License

Open Source License

Parameter

Parameter Description
clazz the clazz to check
methodName the name of the method

Return

the number of methods with the given name

Declaration

public static int getMethodCountForName(Class<?> clazz, String methodName) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    /**/*w ww . j a  v  a  2s  . c  o m*/
     * Return the number of methods with a given name (with any argument types),
     * for the given class and/or its superclasses. Includes non-public methods.
     * @param clazz   the clazz to check
     * @param methodName the name of the method
     * @return the number of methods with the given name
     */
    public static int getMethodCountForName(Class<?> clazz, String methodName) {
        int count = 0;
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (methodName.equals(method.getName())) {
                count++;
            }
        }
        Class<?>[] ifcs = clazz.getInterfaces();
        for (Class<?> ifc : ifcs) {
            count += getMethodCountForName(ifc, methodName);
        }
        if (clazz.getSuperclass() != null) {
            count += getMethodCountForName(clazz.getSuperclass(), methodName);
        }
        return count;
    }
}

Related

  1. getMethodByName(Class type, String methodName)
  2. getMethodByName(final Class cls, final String action)
  3. getMethodByNameFromArray(Method[] methods, String methodName)
  4. getMethodByNameSimple(Class clz, String methodName)
  5. getMethodCount(Class clazz, String methodName)
  6. getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)
  7. getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
  8. getMethodFromClassHierarchy(Class clazz, String methodName)
  9. getMethodFromClassWithInheritance(Class cls, String methodName)