Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class ReflectUtil {
- /**
- * Locates a given field anywhere in the class inheritance hierarchy.
- *
- * @param instance an object to search the field into.
- * @param name field name
- * @return a field object
- * @throws NoSuchFieldException if the field cannot be located
- */
- public static Field findField(Object instance, String name) throws NoSuchFieldException {
- for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
- try {
- Field field = clazz.getDeclaredField(name);
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
- return field;
- } catch (NoSuchFieldException e) {
- // ignore and search next
- }
- }
- throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
- }
- /**
- * Locates a given method anywhere in the class inheritance hierarchy.
- *
- * @param instance an object to search the method into.
- * @param name method name
- * @param parameterTypes method parameter types
- * @return a method object
- * @throws NoSuchMethodException if the method cannot be located
- */
- public static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
- throws NoSuchMethodException {
- for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
- try {
- Method method = clazz.getDeclaredMethod(name, parameterTypes);
- if (!method.isAccessible()) {
- method.setAccessible(true);
- }
- return method;
- } catch (NoSuchMethodException e) {
- // ignore and search next
- }
- }
- throw new NoSuchMethodException("Method " + name + " with parameters " +
- Arrays.asList(parameterTypes) + " not found in " + instance.getClass());
- }
- }
Add Comment
Please, Sign In to add comment