summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/util/ReflectionUtil.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2011-11-22 12:23:07 +0100
committerSven Gothel <[email protected]>2011-11-22 12:23:07 +0100
commit43b7675259eb76c570b6cc3a44fec2b9f6410697 (patch)
treecc6529445c1d750d7d54388277cff13dedbf1c42 /src/java/com/jogamp/common/util/ReflectionUtil.java
parent9f14ad29bf2d652c11328479ccb11030408c0543 (diff)
RunnableTask: Add documentation, incl. unit test. Add ReflectionUtil.MethodAccess, a convenient Method instance accessor.
Diffstat (limited to 'src/java/com/jogamp/common/util/ReflectionUtil.java')
-rw-r--r--src/java/com/jogamp/common/util/ReflectionUtil.java28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/util/ReflectionUtil.java b/src/java/com/jogamp/common/util/ReflectionUtil.java
index 6345efa..c771544 100644
--- a/src/java/com/jogamp/common/util/ReflectionUtil.java
+++ b/src/java/com/jogamp/common/util/ReflectionUtil.java
@@ -331,5 +331,33 @@ public final class ReflectionUtil {
return callMethod(null, getMethod(clazzName, methodName, argTypes, cl), args);
}
+ /** Convenient Method access class */
+ public static class MethodAccessor {
+ Method m = null;
+
+ /** Check {@link #available()} before using instance. */
+ public MethodAccessor(Class<?> clazz, String methodName, Class<?> ... argTypes) {
+ try {
+ m = ReflectionUtil.getMethod(clazz, methodName, argTypes);
+ } catch (JogampRuntimeException jre) { /* method n/a */ }
+ }
+
+ /** Returns true if method is available, otherwise false. */
+ public boolean available() {
+ return null != m;
+ }
+
+ /**
+ * Check {@link #available()} before calling to avoid throwing a JogampRuntimeException.
+ * @throws JogampRuntimeException if method is not available
+ */
+ public Object callMethod(Object instance, Object ... args) {
+ if(null == m) {
+ throw new JogampRuntimeException("Method not available. Instance: "+instance);
+ }
+ return ReflectionUtil.callMethod(instance, m, args);
+ }
+ }
+
}