List of utility methods to do Reflection Method Get from Object
Method | getMethod(Class aObject, String aMethod, Class>... aParameterTypes) get Method Method rMethod = null; try { rMethod = aObject.getMethod(aMethod, aParameterTypes); rMethod.setAccessible(true); } catch (Throwable e) { return rMethod; |
Method | getMethod(Class c, String methodName, Object... pa) Returns Method by name and parameter value Method[] ma = c.getMethods(); for (Method m : ma) { if (!m.getName().equals(methodName)) { continue; Class[] pts = m.getParameterTypes(); boolean shouldContinue = false; int len = pts.length; ... |
Method | getMethod(Class objectClass, String methodName, String argumentType) get Method Method returnMethod = null; Method[] methods = objectClass.getMethods(); int size = methods.length; for (int i = 0; i < size; i++) { Method method = methods[i]; if (method.getName().equals(methodName)) { Class[] args = method.getParameterTypes(); int argsize = args.length; ... |
Method | getMethod(Class type, String name, Object[] args) Helper to return a method given its name and a list of arguments boolean containsNull = false; Class<?>[] types = null; if (args == null) types = new Class[] {}; else { types = new Class[args.length]; for (int i = 0; i < args.length; ++i) { if (args[i] != null) ... |
Method | getMethod(Class extends Object> clz, String methodName, Class[] methodArgs) Gets method from class clz or any of its superclasses.
if (clz == null) throw new NoSuchMethodException(methodName + "(" + methodArgs + ") method does not exist "); try { return clz.getDeclaredMethod(methodName, methodArgs); } catch (NoSuchMethodException e) { return getMethod(clz.getSuperclass(), methodName, methodArgs); |
Method | getMethod(Class extends Object> type, String name, Class>... parameterTypes) get Method try { return type.getMethod(name, parameterTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException("Failed to get method from class. methodName=" + name + " class=" + type, e); |
Method | getMethod(Class> cls, String method, Object[] params) Get the method based on its name Class<?>[] classes = new Class[params.length]; for (int i = 0; i < params.length; i++) { classes[i] = params[i].getClass(); return cls.getMethod(method, classes); |
Method | getMethod(Class> clz, String methodName, Object... params) Get a non-public (i.e. if (params == null) { return clz.getMethod(methodName); } else { int i = 0; Class<?>[] paramClasses = new Class<?>[params.length]; for (Object param : params) { paramClasses[i++] = param.getClass(); return clz.getDeclaredMethod(methodName, paramClasses); |
Method | getMethod(Class> klass, String methodName, Object... params) get Method Class<?>[] classes = (params.length != 0) ? new Class<?>[params.length] : EMPTY_CLASS_PARAMS; for (int i = 0; i < params.length; i++) { classes[i] = params[i].getClass(); return klass.getMethod(methodName, classes); |
Method | getMethod(Class> objectType, String methodName, Class>... parameterTypes) Returns the method on the objectType with the given name and given parameter types. try { Method method = objectType.getDeclaredMethod(methodName, parameterTypes); if (method == null) { method = objectType.getMethod(methodName, parameterTypes); return method; } catch (Exception e) { return null; ... |