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

intgetMethodCount(Class clazz, String methodName)
get Method Count
int count = 0;
do {
    for (int i = 0; i < clazz.getDeclaredMethods().length; i++) {
        Method method = clazz.getDeclaredMethods()[i];
        if (methodName.equals(method.getName())) {
            count++;
    clazz = clazz.getSuperclass();
} while (clazz != null);
return count;
intgetMethodCountForName(Class clazz, String methodName)
Return the number of methods with a given name (with any argument types), for the given class and/or its superclasses.
int count = 0;
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
    if (methodName.equals(method.getName())) {
        count++;
Class<?>[] ifcs = clazz.getInterfaces();
...
ClassgetMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)
get Method Exception Type
try {
    Method m = cls.getMethod(methodName, argTypes);
    return getMethodExceptionType(cls, m, methodPosition, classPosition, defaultType);
} catch (Exception ex) {
    throw new RuntimeException(ex);
MethodgetMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
Get the method with the given name from the given class, provided that it takes one argument of the provided type.
Method[] allMethods = cls.getDeclaredMethods();
for (Method meth : allMethods) {
    if (!meth.getName().equals(methodName))
        continue;
    if (onlyProtectedAndHigher) {
        int modifiers = meth.getModifiers();
        if (Modifier.isPrivate(modifiers))
            continue;
...
MethodgetMethodFromClassHierarchy(Class clazz, String methodName)
Searches for the method within class hierarchy
while (clazz != null) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(methodName)) {
            return method;
    clazz = clazz.getSuperclass(); 
throw new NoSuchMethodException(methodName);
MethodgetMethodFromClassWithInheritance(Class cls, String methodName)
get Method From Class With Inheritance
Method theMethod = getMethodFromClass(cls, methodName);
while (theMethod == null && (!cls.getName().equals("java.lang.Object"))) {
    cls = cls.getSuperclass();
    theMethod = getMethodFromClass(cls, methodName);
return theMethod;
StringgetMethodFullName(Method method)
get Method Full Name
String name = method.getName();
String owner = method.getDeclaringClass().getName();
return owner + "." + name;
MethodgetMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
Determine whether the given class has a method with the given signature, and return it if available (else return null).
if (clazz == null) {
    throw new IllegalArgumentException("Class must not be null");
if (methodName == null) {
    throw new IllegalArgumentException("Method name must not be null");
try {
    return clazz.getMethod(methodName, paramTypes);
...
MethodgetMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
Determine whether the given class has a public method with the given signature, and return it if available (else return null ).
if (paramTypes != null) {
    try {
        return clazz.getMethod(methodName, paramTypes);
    } catch (NoSuchMethodException ex) {
        return null;
} else {
    Set<Method> candidates = new HashSet<Method>(1);
...
MethodgetMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
get Method If Available
if (clazz == null) {
    return null;
try {
    return clazz.getDeclaredMethod(methodName, paramTypes);
} catch (NoSuchMethodException e) {
    return getMethodIfAvailable(clazz.getSuperclass(), methodName, paramTypes);