Java Reflection Method Name getMethod(Class clazz, String methodName, Class... params)

Here you can find the source of getMethod(Class clazz, String methodName, Class... params)

Description

Attempts to get a method from the specified class

License

LGPL

Parameter

Parameter Description
clazz - The class to retrieve the method from
methodName - Name for the method
params - A list of the methods parameters

Return

The Method that matched, or null

Declaration

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.reflect.Method;

public class Main {
    /**//from w w w. j av  a2  s  .  c om
     * Attempts to get a method from the specified class
     * 
     * @param clazz - The class to retrieve the method from
     * @param methodName - Name for the method
     * @param params - A list of the methods parameters
     * @return The Method that matched, or null
     */
    public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
        return getMethod(clazz, new String[] { methodName }, params);
    }

    /**
     * Attempts to get a method from the specified class
     * 
     * @param clazz - The class to retrieve the method from
     * @param methodNames - Names for the method
     * @param params - A list of the methods parameters
     * @return The Method that matched, or null
     */
    public static Method getMethod(Class<?> clazz, String[] methodNames, Class<?>... params) {
        if (clazz == null) {
            System.err.println("No class specified.");
            return null;
        }
        if (methodNames == null || methodNames.length < 1) {
            System.err.println("No methodNames specified.");
            return null;
        }
        Method method = null;
        for (String methodName : methodNames) {
            try {
                method = clazz.getDeclaredMethod(methodName, params);
                break;
            } catch (NoSuchMethodException nsfe) {
                continue;
            } catch (NoClassDefFoundError ncdfe) {
                continue;
            }
        }
        if (method == null) {
            System.err.println(clazz.getName() + " does not have method " + methodNames[0]);
            return null;
        } else {
            try {
                method.setAccessible(true);
            } catch (SecurityException se) {
            }
            return method;
        }
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class fieldType)
  2. getMethod(Class clazz, String methodName, Class... arguments)
  3. getMethod(Class clazz, String methodName, Class... params)
  4. getMethod(Class clazz, String methodName, Class... params)
  5. getMethod(Class clazz, String methodName, Class... params)
  6. getMethod(Class clazz, String methodName, final Class[] classes)
  7. getMethod(Class clazz, String methodName, int numParams)
  8. getMethod(Class clazz, String name)
  9. getMethod(Class clazz, String name)