From 43b7675259eb76c570b6cc3a44fec2b9f6410697 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 22 Nov 2011 12:23:07 +0100 Subject: RunnableTask: Add documentation, incl. unit test. Add ReflectionUtil.MethodAccess, a convenient Method instance accessor. --- .../com/jogamp/common/util/ReflectionUtil.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/java/com/jogamp/common/util/ReflectionUtil.java') 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); + } + } + } -- cgit v1.2.3