List of utility methods to do Reflection Method Get from Object
Method | getMethod(Class> type, String name, Object[] args) get Method return selectMethod(getMethods(type, name), args);
|
Method | getMethod(final Class extends Object> clazz, final String methodName, final Class>[] parTypes, final Object[] parameters) get Method assert clazz != null && methodName != null && parTypes != null && parameters != null; try { return clazz.getDeclaredMethod(methodName, parTypes); } catch (NoSuchMethodException e) { final Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return getMethod(superClass, methodName, parTypes, parameters); throw e; |
Method | getMethod(final Object object, final String methodName, final Class>... parameterClass) get Method return getMethod(object.getClass(), methodName, parameterClass);
|
Method | getMethod(final Object object, final String methodName, final Object... arguments) Returns object's method with the specified name and arguments. return getMethod(object.getClass(), methodName, arguments);
|
Object | getMethod(final Object object, final String methodName, final Object... arguments) get Method final Class<?> c = object.getClass(); final Class<?>[] parameterTypes = new Class<?>[arguments.length]; int counter = 0; for (final Object argument : arguments) { parameterTypes[counter++] = argument.getClass(); final Method method = c.getDeclaredMethod(methodName, parameterTypes); return method; ... |
Method | getMethod(final Object object, String methodName, final Class>[] argTypes) get Method final Method method; try { method = object.getClass().getMethod(methodName, argTypes); } catch (Exception e) { throw new IllegalArgumentException("Exception when retrieving method " + methodName + " by reflection", e); return method; ... |
Method | getMethod(final Object target, final String methodName, final Class... argumentTypes) Returns a the specified method of the target object, or null if the method does not exist. return getMethod(target.getClass(), methodName, argumentTypes);
|
Method | getMethod(final String methodName, final Object obj, final Class>... argTypes) Returns a method (public or private) object for the given methodName if it exists. Method method = null; try { method = obj.getClass().getDeclaredMethod(methodName, argTypes); method.setAccessible(true); } catch (final SecurityException e) { throw new RuntimeException(e); } catch (final NoSuchMethodException e) { throw new RuntimeException(e); ... |
Method | getMethod(Object bean, String propertyName) Return the named getter method on the bean or null if not found. final StringBuilder sb = new StringBuilder("get").append(Character.toUpperCase(propertyName.charAt(0))); if (propertyName.length() > 1) { sb.append(propertyName.substring(1)); final String getterName = sb.toString(); for (Method m : bean.getClass().getMethods()) { if (getterName.equals(m.getName()) && m.getParameterTypes().length == 0) { return m; ... |
Optional | getMethod(Object instance, String methodName, Class>... argsClass) get Method try { return Optional.of(instance.getClass().getMethod(methodName, argsClass)); } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); return Optional.empty(); |