diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java')
-rw-r--r-- | plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java index c9e478cad8..aac1bb35bd 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/ReflectUtil.java @@ -56,20 +56,20 @@ public final class ReflectUtil { } - public static Field getField(Class<?> c, String fieldName) + public static Method getMethod(Class<?> c, String methodName, Class... parameterTypes) { try { try { - return c.getDeclaredField(fieldName); + return c.getDeclaredMethod(methodName, parameterTypes); } - catch (NoSuchFieldException ex) + catch (NoSuchMethodException ex) { Class<?> superclass = c.getSuperclass(); if (superclass != null) { - return getField(superclass, fieldName); + return getMethod(superclass, methodName, parameterTypes); } return null; @@ -85,20 +85,37 @@ public final class ReflectUtil } } - public static Method getMethod(Class<?> c, String methodName, Class... parameterTypes) + public static Object invokeMethod(Method method, Object target, Object... arguments) throws InvocationTargetException + { + if (!method.isAccessible()) + { + method.setAccessible(true); + } + + try + { + return method.invoke(target, arguments); + } + catch (IllegalAccessException ex) + { + throw new ImplementationError(ex); + } + } + + public static Field getField(Class<?> c, String fieldName) { try { try { - return c.getDeclaredMethod(methodName, parameterTypes); + return c.getDeclaredField(fieldName); } - catch (NoSuchMethodException ex) + catch (NoSuchFieldException ex) { Class<?> superclass = c.getSuperclass(); if (superclass != null) { - return getMethod(superclass, methodName, parameterTypes); + return getField(superclass, fieldName); } return null; @@ -114,6 +131,40 @@ public final class ReflectUtil } } + public static Object getValue(Field field, Object target) + { + if (!field.isAccessible()) + { + field.setAccessible(true); + } + + try + { + return field.get(target); + } + catch (IllegalAccessException ex) + { + throw new ImplementationError(ex); + } + } + + public static void setValue(Field field, Object target, Object value) + { + if (!field.isAccessible()) + { + field.setAccessible(true); + } + + try + { + field.set(target, value); + } + catch (IllegalAccessException ex) + { + throw new ImplementationError(ex); + } + } + public static void printStackTrace(PrintStream out, StackTraceElement[] stackTrace) { for (int i = 2; i < stackTrace.length; i++) |