diff options
author | Michael Bien <[email protected]> | 2010-06-18 04:39:16 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-06-18 04:39:16 +0200 |
commit | 1130eb884f5ae60471daa3bc049fa3f420a86b2c (patch) | |
tree | 2b210c5fa8b76703581046fbc4b08a00c1df10f5 /src | |
parent | 336ab1ae582753bac4e53e884124c28550a2b0dc (diff) | |
parent | 9fa4828ffb4ea0c01d349d5847e91c4b6fa6e511 (diff) |
Merge branch 'master' of github.com:mbien/gluegen
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/jogamp/common/util/ReflectionUtil.java | 36 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/RunnableTask.java | 90 |
2 files changed, 108 insertions, 18 deletions
diff --git a/src/java/com/jogamp/common/util/ReflectionUtil.java b/src/java/com/jogamp/common/util/ReflectionUtil.java index b9c6ef0..fc4352f 100644 --- a/src/java/com/jogamp/common/util/ReflectionUtil.java +++ b/src/java/com/jogamp/common/util/ReflectionUtil.java @@ -47,9 +47,9 @@ public final class ReflectionUtil { /** * Returns true only if the class could be loaded. */ - public static final boolean isClassAvailable(String clazzName) { + public static final boolean isClassAvailable(String clazzName, ClassLoader cl) { try { - return null != Class.forName(clazzName, false, ReflectionUtil.class.getClassLoader()); + return null != Class.forName(clazzName, false, cl); } catch (ClassNotFoundException e) { return false; } @@ -59,26 +59,26 @@ public final class ReflectionUtil { * Loads and returns the class or null. * @see Class#forName(java.lang.String, boolean, java.lang.ClassLoader) */ - public static final Class getClass(String clazzName, boolean initialize) + public static final Class getClass(String clazzName, boolean initialize, ClassLoader cl) throws JogampRuntimeException { try { - return getClassImpl(clazzName, initialize); + return getClassImpl(clazzName, initialize, cl); } catch (ClassNotFoundException e) { throw new JogampRuntimeException(clazzName + " not available", e); } } - private static Class getClassImpl(String clazzName, boolean initialize) throws ClassNotFoundException { - return Class.forName(clazzName, initialize, ReflectionUtil.class.getClassLoader()); + private static Class getClassImpl(String clazzName, boolean initialize, ClassLoader cl) throws ClassNotFoundException { + return Class.forName(clazzName, initialize, cl); } /** * @throws JogampRuntimeException if the constructor can not be delivered. */ - public static final Constructor getConstructor(String clazzName, Class[] cstrArgTypes) + public static final Constructor getConstructor(String clazzName, ClassLoader cl, Class[] cstrArgTypes) throws JogampRuntimeException { try { - return getConstructor(getClassImpl(clazzName, true), cstrArgTypes); + return getConstructor(getClassImpl(clazzName, true, cl), cstrArgTypes); } catch (ClassNotFoundException ex) { throw new JogampRuntimeException(clazzName + " not available", ex); } @@ -109,9 +109,9 @@ public final class ReflectionUtil { } } - public static final Constructor getConstructor(String clazzName) + public static final Constructor getConstructor(String clazzName, ClassLoader cl) throws JogampRuntimeException { - return getConstructor(clazzName, new Class[0]); + return getConstructor(clazzName, cl, new Class[0]); } /** @@ -147,30 +147,30 @@ public final class ReflectionUtil { return createInstance(clazz, cstrArgTypes, cstrArgs); } - public static final Object createInstance(String clazzName, Class[] cstrArgTypes, Object[] cstrArgs) + public static final Object createInstance(String clazzName, ClassLoader cl, Class[] cstrArgTypes, Object[] cstrArgs) throws JogampRuntimeException, RuntimeException { try { - return createInstance(getClassImpl(clazzName, true), cstrArgTypes, cstrArgs); + return createInstance(getClassImpl(clazzName, true, cl), cstrArgTypes, cstrArgs); } catch (ClassNotFoundException ex) { throw new JogampRuntimeException(clazzName + " not available", ex); } } - public static final Object createInstance(String clazzName, Object[] cstrArgs) + public static final Object createInstance(String clazzName, ClassLoader cl, Object[] cstrArgs) throws JogampRuntimeException, RuntimeException { Class[] cstrArgTypes = new Class[cstrArgs.length]; for(int i=0; i<cstrArgs.length; i++) { cstrArgTypes[i] = cstrArgs[i].getClass(); } - return createInstance(clazzName, cstrArgTypes, cstrArgs); + return createInstance(clazzName, cl, cstrArgTypes, cstrArgs); } - public static final Object createInstance(String clazzName) + public static final Object createInstance(String clazzName, ClassLoader cl) throws JogampRuntimeException, RuntimeException { - return createInstance(clazzName, new Class[0], null); + return createInstance(clazzName, cl, new Class[0], null); } public static final boolean instanceOf(Object obj, String clazzName) { @@ -214,12 +214,12 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the instance can not be created. */ - public static final Object callStaticMethod(String clazzName, String methodName, Class[] argTypes, Object[] args) + public static final Object callStaticMethod(String clazzName, ClassLoader cl, String methodName, Class[] argTypes, Object[] args) throws JogampRuntimeException, RuntimeException { Class clazz; try { - clazz = getClassImpl(clazzName, true); + clazz = getClassImpl(clazzName, true, cl); } catch (ClassNotFoundException ex) { throw new JogampRuntimeException(clazzName + " not available", ex); } diff --git a/src/java/com/jogamp/common/util/RunnableTask.java b/src/java/com/jogamp/common/util/RunnableTask.java new file mode 100644 index 0000000..2cf68a5 --- /dev/null +++ b/src/java/com/jogamp/common/util/RunnableTask.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2010, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Sven Gothel nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Sven Gothel BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jogamp.common.util; + +/** + * Helper class to provide a Runnable queue implementation with a Runnable wrapper + * which notifies after execution for the <code>invokeAndWait()</code> semantics. + */ +public class RunnableTask implements Runnable { + Runnable runnable; + Object notifyObject; + boolean catchExceptions; + + Throwable runnableException; + long ts0, ts1, ts2; + + public RunnableTask(Runnable runnable) { + this(runnable, null, false); + } + + public RunnableTask(Runnable runnable, Object notifyObject) { + this(runnable, notifyObject, false); + } + + public RunnableTask(Runnable runnable, Object notifyObject, boolean catchExceptions) { + this.runnable = runnable ; + this.notifyObject = notifyObject ; + this.catchExceptions = catchExceptions ; + ts0 = System.currentTimeMillis(); + ts1 = 0; + ts2 = 0; + } + + public void run() { + ts1 = System.currentTimeMillis(); + try { + runnable.run(); + } catch (Throwable t) { + runnableException = t; + if(!catchExceptions) { + throw new RuntimeException(runnableException); + } + } finally { + ts2 = System.currentTimeMillis(); + } + if(null != notifyObject) { + synchronized (notifyObject) { + notifyObject.notifyAll(); + } + } + } + + public boolean isExecuted() { return 0 != ts2 ; } + public Throwable getThrowable() { return runnableException; } + public long getTimestampCreate() { return ts0; } + public long getTimestampBeforeExec() { return ts1; } + public long getTimestampAfterExec() { return ts2; } + public long getDurationInQueue() { return ts1 - ts0; } + public long getDurationInExec() { return ts2 - ts1; } + public long getDurationTotal() { return ts2 - ts0; } + + public String toString() { + return "RunnableTask[executed "+isExecuted()+", t2-t0 "+getDurationTotal()+", t2-t1 "+getDurationInExec()+", t1-t0 "+getDurationInQueue()+", throwable "+getThrowable()+", Runnable "+runnable+"]"; + } +} + |