List of utility methods to do Reflection Method Get from Object
Method | getMethod(Object object, String methodName, Class... arguments) get Method return object.getClass().getDeclaredMethod(methodName, arguments);
|
Method | getMethod(Object object, String name, Object... params) get method by name try { List<Class> classParams = new ArrayList<>(); for (int i = 0; i < params.length; i++) { classParams.add((Class) params[i]); Class<?> classParamsArray[] = new Class[classParams.size()]; classParams.toArray(classParamsArray); return object.getClass().getMethod(name, classParamsArray); ... |
Method | getMethod(Object oo, String mname) get Method try { Method method = oo.getClass().getMethod(toMethodName(mname), new Class[] { String.class }); return method; } catch (Exception ex) { return null; |
Method | getMethod(Object pojo, String methodName, Class>[] params) get Method Method method = null; Class<?> cClass = pojo.getClass(); while (cClass != null && method == null) { try { method = cClass.getDeclaredMethod(methodName, params); } catch (NoSuchMethodException nsf) { cClass = cClass.getSuperclass(); return method; |
Method | getMethod(Object target, String methodName) get Method Method result = (Method) METHODS.get(methodName); if (result == null) { Object[] argTypes = (Object[]) METHOD_ARGS.get(methodName); result = getMethod(target, methodName, argTypes); METHODS.put(methodName, result); return result; |
Method | getMethod(Object target, String methodName, Class... parameterTypes) Get target method try { return target.getClass().getMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); |
Method | getMethod(Object target, String signature) get Method Assert.isNotNull(target); Assert.isNotNull(signature); Class<?> targetClass = getTargetClass(target); while (targetClass != null) { for (Method method : targetClass.getDeclaredMethods()) { if (getMethodSignature(method).equals(signature)) { method.setAccessible(true); return method; ... |
Method | getMethod(String methodName, Object instance) get Method Name Method[] methods = instance.getClass().getMethods(); for (Method m : methods) { if (m.getName().equalsIgnoreCase(methodName)) { return m; return null; |
Method | getMethodAnnotatedWith(final Class> type, final Class extends Annotation> annotation, String fieldName, Object fieldValue) get Method Annotated With List<Method> methods = getMethodsAnnotatedWith(type, annotation); for (Method m : methods) try { Annotation an = m.getAnnotation(annotation); Method anMethod = an.annotationType().getDeclaredMethod(fieldName); Object value = anMethod.invoke(an); if (value.toString().equals(fieldValue)) return m; ... |
Method | getMethodByName(Object target, String methodName) get Method By Name Class<?> classToSearch = target.getClass(); Method method = null; do { try { method = classToSearch.getDeclaredMethod(methodName); } catch (SecurityException | NoSuchMethodException exception) { classToSearch = classToSearch.getSuperclass(); } while (method == null && classToSearch != null); return method; |