Here you can find the source of getMethod(Class targetClass, String name, Class paramClass)
Parameter | Description |
---|---|
targetClass | The target class |
name | The method name |
paramClass | The single parameter class (may be null) |
Parameter | Description |
---|---|
SecurityException | an exception |
NoSuchMethodException | an exception |
public static Method getMethod(Class targetClass, String name, Class paramClass) throws NoSuchMethodException, SecurityException
//package com.java2s; import java.lang.reflect.Method; public class Main { private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; /** Get the method with the given name and with a single argument of the type specified by paramClass. The paramClass may be null in which case the method should have no arguments. /*from w w w . jav a 2 s . c o m*/ @param targetClass The target class @param name The method name @param paramClass The single parameter class (may be null) @return The Method @throws SecurityException @throws NoSuchMethodException */ public static Method getMethod(Class targetClass, String name, Class paramClass) throws NoSuchMethodException, SecurityException { if (paramClass == null) { return getMethod(targetClass, name, EMPTY_CLASS_ARRAY); } else { Class[] paramClasses = new Class[1]; paramClasses[0] = paramClass; return getMethod(targetClass, name, paramClasses); } } /** Get the method with the given name and with arguments of the types specified by paramClasses. If the method is not found then a method which accepts superclasses of the current arguments will be returned, if possible. @param targetClass The target class @param name The method name @param paramClasses An array of parameter classes @return The Method @throws SecurityException @throws NoSuchMethodException */ public static Method getMethod(Class targetClass, String name, Class[] paramClasses) throws NoSuchMethodException, SecurityException { Method method = null; try { method = targetClass.getMethod(name, paramClasses); } catch (NoSuchMethodException nsme) { Method[] methods = targetClass.getMethods(); OUTER: for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equalsIgnoreCase(name) && methods[i].getParameterTypes().length == paramClasses.length) { Class[] params = methods[i].getParameterTypes(); for (int j = 0; j < params.length; j++) { if (!params[j].isAssignableFrom(paramClasses[j])) { continue OUTER; } } method = methods[i]; break; } } if (method == null) { throw nsme; } } return method; } }