diff options
author | Michael Bien <[email protected]> | 2010-04-18 00:31:12 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-04-18 00:31:12 +0200 |
commit | aa7084700bbf74d8bcc98cf0239f57cff2983423 (patch) | |
tree | 87ad03b14f3dbb989ffa8b3077096ecb75fcc28e /src | |
parent | 7deccdaa76305b7acae9dfc7a15f974387c6cd1a (diff) |
code review (http://jogamp.org/bugzilla/show_bug.cgi?id=396): improved exception handling in NWReflection and NativeLibLoaderBase.
goal was to prevent catch(Throwable) and to fix or document empty catch blocks where it makes sense.
Diffstat (limited to 'src')
-rw-r--r-- | src/nativewindow/classes/com/jogamp/nativewindow/impl/NWReflection.java | 125 | ||||
-rw-r--r-- | src/nativewindow/classes/com/jogamp/nativewindow/impl/NativeLibLoaderBase.java | 32 |
2 files changed, 86 insertions, 71 deletions
diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/impl/NWReflection.java b/src/nativewindow/classes/com/jogamp/nativewindow/impl/NWReflection.java index 22bdedac9..9bac9477a 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/impl/NWReflection.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/impl/NWReflection.java @@ -40,73 +40,83 @@ import java.lang.reflect.*; import javax.media.nativewindow.*; public final class NWReflection { + public static final boolean DEBUG = Debug.debug("NWReflection"); - public static final boolean isClassAvailable(String clazzName) { - try { - Class clazz = Class.forName(clazzName, false, NWReflection.class.getClassLoader()); - return null!=clazz; - } catch (Throwable e) { } - return false; - } + /** + * Returns true only if the class could be loaded. + */ + public static final boolean isClassAvailable(String clazzName) { + try { + return null != Class.forName(clazzName, false, NWReflection.class.getClassLoader()); + } catch (ClassNotFoundException e) { + return false; + } + } - public static final Class getClass(String clazzName, boolean initialize) { - try { - return Class.forName(clazzName, initialize, NWReflection.class.getClassLoader()); - } catch (Throwable e) { } - return null; - } + /** + * 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) { + try { + return getClassImpl(clazzName, initialize); + } catch (ClassNotFoundException e) { + return null; + } + } - public static final Constructor getConstructor(String clazzName, Class[] cstrArgTypes) { - Class factoryClass = null; - Constructor factory = null; + private static Class getClassImpl(String clazzName, boolean initialize) throws ClassNotFoundException { + return Class.forName(clazzName, initialize, NWReflection.class.getClassLoader()); + } - try { - factoryClass = getClass(clazzName, true); - if (factoryClass == null) { - throw new NativeWindowException(clazzName + " not available"); + /** + * @throws NativeWindowException if the constructor can not be delivered. + */ + public static final Constructor getConstructor(String clazzName, Class[] cstrArgTypes) { + try { + return getConstructor(getClassImpl(clazzName, true), cstrArgTypes); + } catch (ClassNotFoundException ex) { + throw new NativeWindowException(clazzName + " not available", ex); } - return getConstructor(factoryClass, cstrArgTypes); - } catch (Throwable e) { - if (DEBUG) { - e.printStackTrace(); - } - throw new NativeWindowException(e); } - } - - public static final Constructor getConstructor(Class clazz, Class[] cstrArgTypes) { - Constructor factory = null; - try { + /** + * @throws NativeWindowException if the constructor can not be delivered. + */ + public static final Constructor getConstructor(Class clazz, Class[] cstrArgTypes) { try { - factory = clazz.getDeclaredConstructor( cstrArgTypes ); - } catch(NoSuchMethodException nsme) { - throw new NativeWindowException("Constructor: '" + clazz + "("+cstrArgTypes+")' not found"); + 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 NativeWindowException("Constructor: '" + clazz + "(" + args + ")' not found", ex); } - return factory; - } catch (Throwable e) { - if (DEBUG) { - e.printStackTrace(); - } - throw new NativeWindowException(e); } - } public static final Constructor getConstructor(String clazzName) { return getConstructor(clazzName, new Class[0]); } - public static final Object createInstance(Class clazz, Class[] cstrArgTypes, Object[] cstrArgs) { - Constructor factory = null; - - try { - factory = getConstructor(clazz, cstrArgTypes); - return factory.newInstance( cstrArgs ) ; - } catch (Exception e) { - throw new NativeWindowException(e); + /** + * @throws NativeWindowException if the instance can not be created. + */ + public static final Object createInstance(Class clazz, Class[] cstrArgTypes, Object[] cstrArgs) { + try { + return getConstructor(clazz, cstrArgTypes).newInstance(cstrArgs); + } catch (InstantiationException ex) { + throw new NativeWindowException("can not create instance of class "+clazz, ex); + } catch (InvocationTargetException ex) { + throw new NativeWindowException("can not create instance of class "+clazz, ex); + } catch (IllegalAccessException ex) { + throw new NativeWindowException("can not create instance of class "+clazz, ex); + } } - } public static final Object createInstance(Class clazz, Object[] cstrArgs) { Class[] cstrArgTypes = new Class[cstrArgs.length]; @@ -116,16 +126,13 @@ public final class NWReflection { return createInstance(clazz, cstrArgTypes, cstrArgs); } - public static final Object createInstance(String clazzName, Class[] cstrArgTypes, Object[] cstrArgs) { - Constructor factory = null; - - try { - factory = getConstructor(clazzName, cstrArgTypes); - return factory.newInstance( cstrArgs ) ; - } catch (Exception e) { - throw new NativeWindowException(e); + public static final Object createInstance(String clazzName, Class[] cstrArgTypes, Object[] cstrArgs) { + try { + return createInstance(getClassImpl(clazzName, true), cstrArgTypes, cstrArgs); + } catch (ClassNotFoundException ex) { + throw new NativeWindowException(clazzName + " not available", ex); + } } - } public static final Object createInstance(String clazzName, Object[] cstrArgs) { Class[] cstrArgTypes = new Class[cstrArgs.length]; diff --git a/src/nativewindow/classes/com/jogamp/nativewindow/impl/NativeLibLoaderBase.java b/src/nativewindow/classes/com/jogamp/nativewindow/impl/NativeLibLoaderBase.java index 0670234d4..c4f1d7e08 100644 --- a/src/nativewindow/classes/com/jogamp/nativewindow/impl/NativeLibLoaderBase.java +++ b/src/nativewindow/classes/com/jogamp/nativewindow/impl/NativeLibLoaderBase.java @@ -64,8 +64,7 @@ public class NativeLibLoaderBase { } private static class DefaultAction implements LoaderAction { - public void loadLibrary(String libname, String[] preload, - boolean preloadIgnoreError) { + public void loadLibrary(String libname, String[] preload, boolean preloadIgnoreError) { if (null!=preload) { for (int i=0; i<preload.length; i++) { if(!isLoaded(preload[i])) { @@ -75,9 +74,10 @@ public class NativeLibLoaderBase { if(DEBUG) { System.err.println("NativeLibLoaderBase preloaded "+preload[i]); } - } - catch (UnsatisfiedLinkError e) { - if(DEBUG) e.printStackTrace(); + } catch (UnsatisfiedLinkError e) { + if(DEBUG) { + e.printStackTrace(); + } if (!preloadIgnoreError && e.getMessage().indexOf("already loaded") < 0) { throw e; } @@ -140,6 +140,7 @@ public class NativeLibLoaderBase { private static final Class customLauncherClass; private static final Method customLoadLibraryMethod; + // FIXME centralize logging static { Class launcherClass = null; Method loadLibraryMethod = null; @@ -148,12 +149,15 @@ public class NativeLibLoaderBase { try { launcherClass = Class.forName("org.jdesktop.applet.util.JNLPAppletLauncher"); loadLibraryMethod = launcherClass.getDeclaredMethod("loadLibrary", new Class[] { String.class }); - } catch (Throwable t) { + } catch (ClassNotFoundException ex) { + if(DEBUG) { + ex.printStackTrace(); + } + } catch (NoSuchMethodException ex) { if(DEBUG) { - t.printStackTrace(); + ex.printStackTrace(); } launcherClass = null; - loadLibraryMethod = null; } } @@ -163,15 +167,19 @@ public class NativeLibLoaderBase { try { launcherClass = Class.forName(launcherClassName); loadLibraryMethod = launcherClass.getDeclaredMethod("loadLibrary", new Class[] { String.class }); - } catch (Throwable t) { + } catch (ClassNotFoundException ex) { + if(DEBUG) { + ex.printStackTrace(); + } + } catch (NoSuchMethodException ex) { if(DEBUG) { - t.printStackTrace(); + ex.printStackTrace(); } launcherClass = null; - loadLibraryMethod = null; } } } + customLauncherClass = launcherClass; customLoadLibraryMethod = loadLibraryMethod; } @@ -192,7 +200,7 @@ public class NativeLibLoaderBase { throw (RuntimeException) t; } // Throw UnsatisfiedLinkError for best compatibility with System.loadLibrary() - throw (UnsatisfiedLinkError) new UnsatisfiedLinkError().initCause(e); + throw (UnsatisfiedLinkError)new UnsatisfiedLinkError("can not load library "+libraryName).initCause(e); } } else { // System.out.println("sun.boot.library.path=" + Debug.getProperty("sun.boot.library.path", false)); |