diff options
author | Sven Gothel <[email protected]> | 2010-05-05 15:31:25 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-05-05 15:31:25 +0200 |
commit | 9300b0615f554f5904c93d757e4a9edbd1897538 (patch) | |
tree | f20418a63b7fd4251545f4d4a15e33c551dc517d /src/java/com/jogamp/common | |
parent | 512a7fb820fc82e6960e0a493c6127f0ec800649 (diff) |
ReflectionUtil: Added callStaticMethod(..)
Diffstat (limited to 'src/java/com/jogamp/common')
-rw-r--r-- | src/java/com/jogamp/common/util/ReflectionUtil.java | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/src/java/com/jogamp/common/util/ReflectionUtil.java b/src/java/com/jogamp/common/util/ReflectionUtil.java index 9ba39ed..b9c6ef0 100644 --- a/src/java/com/jogamp/common/util/ReflectionUtil.java +++ b/src/java/com/jogamp/common/util/ReflectionUtil.java @@ -84,6 +84,19 @@ public final class ReflectionUtil { } } + static final String asString(Class[] argTypes) { + StringBuffer args = new StringBuffer(); + boolean coma = false; + for (int i = 0; i < argTypes.length; i++) { + if(coma) { + args.append(", "); + } + args.append(argTypes[i].getName()); + coma = true; + } + return args.toString(); + } + /** * @throws JogampRuntimeException if the constructor can not be delivered. */ @@ -92,14 +105,7 @@ public final class ReflectionUtil { try { return clazz.getDeclaredConstructor(cstrArgTypes); } catch (NoSuchMethodException ex) { - String args = ""; - for (int i = 0; i < cstrArgTypes.length; i++) { - args += cstrArgTypes[i].getName(); - if(i != cstrArgTypes.length-1) { - args+= ", "; - } - } - throw new JogampRuntimeException("Constructor: '" + clazz + "(" + args + ")' not found", ex); + throw new JogampRuntimeException("Constructor: '" + clazz + "(" + asString(cstrArgTypes) + ")' not found", ex); } } @@ -205,5 +211,39 @@ public final class ReflectionUtil { return instanceOf(clazz, "java.awt.Component"); } + /** + * @throws JogampRuntimeException if the instance can not be created. + */ + public static final Object callStaticMethod(String clazzName, String methodName, Class[] argTypes, Object[] args) + throws JogampRuntimeException, RuntimeException + { + Class clazz; + try { + clazz = getClassImpl(clazzName, true); + } catch (ClassNotFoundException ex) { + throw new JogampRuntimeException(clazzName + " not available", ex); + } + Method method; + try { + method = clazz.getDeclaredMethod(methodName, argTypes); + } catch (NoSuchMethodException ex) { + throw new JogampRuntimeException("Method: '" + clazz + "." + methodName + "(" + asString(argTypes) + ")' not found", ex); + } + try { + return method.invoke(null, args); + } catch (Exception e) { + Throwable t = e; + if (t instanceof InvocationTargetException) { + t = ((InvocationTargetException) t).getTargetException(); + } + if (t instanceof Error) { + throw (Error) t; + } + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + throw new JogampRuntimeException("calling "+method+" of "+clazz+" failed", t); + } + } } |