[Schmitzm-commits] r2235 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sat Feb 16 14:08:13 CET 2013


Author: mojays
Date: 2013-02-16 14:08:13 +0100 (Sat, 16 Feb 2013)
New Revision: 2235

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil: new method instantiateObject(Class<E> clazz, Object... constrArg)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-02-16 13:07:32 UTC (rev 2234)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-02-16 13:08:13 UTC (rev 2235)
@@ -1562,28 +1562,43 @@
 	 */
 	public static Object instantiateObject(String className, boolean fallback, Object... constrArg) {
 		try {
-			Class<?> clazz = Class.forName(className);
-			for (Constructor constructor : clazz.getConstructors())
-				try {
-					return constructor.newInstance(constrArg);
-				} catch (IllegalArgumentException err) {
-				}
-			throw new UnsupportedOperationException("No matching constructor found...");
+			return instantiateObject(Class.forName(className), constrArg);
 		} catch (ClassNotFoundException err) {
 			// if we want to fall back, we do nothing but
 			// return NULL
 			if (fallback)
 				return null;
 			throw new RuntimeException(err);
-		} catch (Exception err) {
-			// LOGGER.error("Unexpected error when trying to instantiate "+className,err);
-			// do not fall back, because in general the
-			// the class is supported (class present)!
-			throw new RuntimeException(err);
 		}
 	}
 
-	/**
+    /**
+     * Instantiates an object by {@link Class#forName(String)}. The class must be present in runtime classpath.
+     * 
+     * @param className
+     *            full name of class (incl. package)
+     * @param constrArg
+     *            arguments for the constructor
+     * @exception UnsupportedOperationException
+     *                if class could be loaded, but no matching constructor can be found
+     */
+    public static <E> E instantiateObject(Class<E> clazz, Object... constrArg) {
+        try {
+            for (Constructor constructor : clazz.getConstructors())
+                try {
+                    return (E)constructor.newInstance(constrArg);
+                } catch (IllegalArgumentException err) {
+                }
+            throw new UnsupportedOperationException("No matching constructor found...");
+        } catch (Exception err) {
+            // LOGGER.error("Unexpected error when trying to instantiate "+className,err);
+            // do not fall back, because in general the
+            // the class is supported (class present)!
+            throw new RuntimeException(err);
+        }
+    }
+
+    /**
 	 * Ruft die Java Garbage Collection auf.
 	 * 
 	 * @return Anzahl an Bytes, die durch den Aufruf der Garbage Collection freigegeben wurden



More information about the Schmitzm-commits mailing list