[Mulgara-svn] r348 - branches/nw-interface/src/jar/util/java/org/mulgara/util

pag at mulgara.org pag at mulgara.org
Sat Aug 18 18:30:17 UTC 2007


Author: pag
Date: 2007-08-18 13:30:16 -0500 (Sat, 18 Aug 2007)
New Revision: 348

Added:
   branches/nw-interface/src/jar/util/java/org/mulgara/util/Reflect.java
Log:
Utility methods for reflection use of classes.

Added: branches/nw-interface/src/jar/util/java/org/mulgara/util/Reflect.java
===================================================================
--- branches/nw-interface/src/jar/util/java/org/mulgara/util/Reflect.java	2007-08-10 21:12:09 UTC (rev 347)
+++ branches/nw-interface/src/jar/util/java/org/mulgara/util/Reflect.java	2007-08-18 18:30:16 UTC (rev 348)
@@ -0,0 +1,64 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.util;
+
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * Utility methods for reflection.
+ *
+ * @created Aug 15, 2007
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public class Reflect {
+
+  /**
+   * Create a new instance of the given class, using the supplied arguments.
+   * @param clazz The class to create an instance of.
+   * @param args The arguments to supply to the constructor of clazz.
+   * @return A new instance of clazz, constructed with the arguments of args.
+   */
+  public static <T> T newInstance(Class<T> clazz, Object... args) {
+    T result = null;
+    try {
+      result = clazz.getConstructor(getTypes(args)).newInstance(args);
+    } catch (SecurityException e) {
+      throw new RuntimeException("Not permitted to create " + clazz.getName(), e);
+    } catch (NoSuchMethodException e) {
+      throw new RuntimeException("No constructor of the request form for: " + clazz.getName(), e);
+    } catch (IllegalArgumentException e) {
+      throw new RuntimeException("Bad arguments supplied to constructor for: " + clazz.getName(), e);
+    } catch (InstantiationException e) {
+      throw new RuntimeException("Not legal to create objects of type: " + clazz.getName() + "(try using a subclass)", e);
+    } catch (IllegalAccessException e) {
+      throw new RuntimeException("Not permitted to access constructor for " + clazz.getName(), e);
+    } catch (InvocationTargetException e) {
+      // wrap the exception, since we don't know what type it is
+      throw new RuntimeException(e.getCause());
+    }
+    return result;
+  }
+
+  /**
+   * Get a type list of objects.
+   * @param args An array of objects to obtain types for.
+   * @return An array containing the types of the objects from args,
+   *         where args[x] instanceof return[x]
+   */
+  public static Class<?>[] getTypes(Object[] args) {
+    Class<?>[] types = new Class<?>[args.length];
+    for (int a = 0; a < args.length; a++) types[a] = args[a].getClass();
+    return types;
+  }
+}




More information about the Mulgara-svn mailing list