Here you can find the source of getMethod(Class> clazz, String methodName, Class>... params)
Parameter | Description |
---|---|
clazz | - The class to retrieve the method from |
methodName | - Name for the method |
params | - A list of the methods parameters |
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params)
//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; } } }