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

MethodgetMethod(Class clazz, String methodName, Class... params)
Attempts to get a method from the specified class
return getMethod(clazz, new String[] { methodName }, params);
MethodgetMethod(Class clazz, String methodName, final Class[] classes)
get Method
Method method = null;
try {
    method = clazz.getDeclaredMethod(methodName, classes);
} catch (NoSuchMethodException e) {
    try {
        method = clazz.getMethod(methodName, classes);
    } catch (NoSuchMethodException ex) {
        if (clazz.getSuperclass() == null) {
...
MethodgetMethod(Class clazz, String methodName, int numParams)
Looks for first public method of clazz (or any super class or super interface), whose name is equal to methodName and number of parameters is numParams
Method[] methods = clazz.getMethods();
for (Method method : methods) {
    if (method.getName().equals(methodName)) {
        Class<?>[] params = method.getParameterTypes();
        if (params.length == numParams) {
            return method;
return null;
MethodgetMethod(Class clazz, String name)
get Method
try {
    Method m = clazz.getMethod(name);
    if (m != null)
        return m;
} catch (Exception e) {
for (Method m : clazz.getMethods())
    if (m.getName().equalsIgnoreCase(name))
...
MethodgetMethod(Class clazz, String name)
Get the first method corresponding with name parameter in the clazz class.
Method[] all = clazz.getMethods();
for (Method method : all) {
    String methodName = method.getName();
    if (methodName.equals(name)) {
        return method;
return null;
...
MethodgetMethod(Class clazz, String name)
get Method
if (clazz == null) {
    throw new RuntimeException("Could not find method " + name);
try {
    Method method = clazz.getDeclaredMethod(name);
    method.setAccessible(true);
    return method;
} catch (NoSuchMethodException e) {
...
MethodgetMethod(Class clazz, String name)
Get all methods of the class including parent.
Method[] methods = clazz.getMethods();
for (Method m : methods) {
    if (m.getName().equalsIgnoreCase(name)) {
        return m;
if (null != clazz.getSuperclass()) {
    getMethod(clazz.getSuperclass(), name);
...
MethodgetMethod(Class clazz, String name)
Get method from class.
Method method = clazz.getDeclaredMethod(name);
method.setAccessible(true);
return method;
MethodgetMethod(Class clazz, String name, boolean declared, Class... args)
get Method
try {
    Method method = null;
    if (declared) {
        method = clazz.getDeclaredMethod(name, args);
        method.setAccessible(true);
    } else
        method = clazz.getMethod(name, args);
    return method;
...
MethodgetMethod(Class clazz, String name, Class... args)
get Method
for (Method m : clazz.getDeclaredMethods())
    if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0
            || ClassListEqual(args, m.getParameterTypes()))) {
        m.setAccessible(true);
        return m;
for (Method m : clazz.getMethods())
    if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0
...