Here you can find the source of getMethod0(Class c, String name, Class... parameterTypes)
Class.getMethod0()
without throwing NoSuchMethodException
.
Parameter | Description |
---|---|
c | class to inspect |
name | name of method to find |
parameterTypes | parameter types |
public static Method getMethod0(Class c, String name, Class... parameterTypes)
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { private static Method _getMethod0; /**/*from w w w .j a v a2s. c o m*/ * Invokes private <code>Class.getMethod0()</code> without throwing <code>NoSuchMethodException</code>. * Returns only public methods or <code>null</code> if method not found. Since no exception is * throwing, it works faster. * * @param c class to inspect * @param name name of method to find * @param parameterTypes parameter types * @return founded method, or null */ public static Method getMethod0(Class c, String name, Class... parameterTypes) { try { return (Method) _getMethod0.invoke(c, name, parameterTypes); } catch (Exception ignore) { return null; } } /** * Invokes accessible method of an object. * * @param c class that contains method * @param obj object to execute * @param method method to invoke * @param paramClasses classes of parameters * @param params parameters */ public static Object invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = c.getMethod(method, paramClasses); return m.invoke(obj, params); } /** * Invokes static method. */ public static Object invoke(Class c, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = c.getMethod(method, paramClasses); return m.invoke(null, params); } /** * Invokes accessible method of an object. * * @param obj object * @param method name of the objects method * @param params method parameters * @param paramClasses method parameter types */ public static Object invoke(Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Method m = obj.getClass().getMethod(method, paramClasses); return m.invoke(obj, params); } /** * Invokes accessible method of an object without specifying parameter types. * @param obj object * @param method method of an object * @param params method parameters */ public static Object invoke(Object obj, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(obj, method, paramClass, params); } public static Object invoke(Class c, Object obj, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(c, obj, method, paramClass, params); } /** * Invokes static method. */ public static Object invoke(Class c, String method, Object... params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class[] paramClass = getClasses(params); return invoke(c, null, method, paramClass, params); } /** * Returns classes from array of specified objects. */ public static Class[] getClasses(Object... objects) { if (objects == null) { return null; } Class[] result = new Class[objects.length]; for (int i = 0; i < objects.length; i++) { if (objects[i] != null) { result[i] = objects[i].getClass(); } } return result; } }