KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > ObjectCreator


1 package com.jdon.util;
2
3 import java.lang.reflect.*;
4
5 /**
6  * Creates instance of any object, given its full qualified class name is given
7  * and it has a public accessable constructor. This class is a static class it
8  * can not be instantiated
9  * @author Nadia Nashi
10  */

11
12 public final class ObjectCreator {
13   /**
14    * Private constructor. This class can not be instantiated
15    */

16   private ObjectCreator() {
17   }
18
19   /**
20    * Instantaite an Object from a given class name
21    * @param className full qualified name of the class
22    * @return the instantaited Object
23    * @exception java.lang.Exception if instantiation failed
24    */

25   public static Object JavaDoc createObject(String JavaDoc className) throws Exception JavaDoc {
26     return createObject(Class.forName(className));
27   }
28
29   /**
30    * Instantaite an Object instance
31    * @param classObject Class object representing the object type to be instantiated
32    * @return the instantaied Object
33    * @exception java.lang.Exception if instantiation failed
34    */

35   public static Object JavaDoc createObject(Class JavaDoc classObject) throws Exception JavaDoc {
36     Object JavaDoc object = null;
37     return classObject.newInstance();
38   }
39
40   /**
41    * Instantaite an Object instance, requires a constructor with parameters
42    * @param className full qualified name of the class
43    * @param params an array including the required parameters to instantaite the object
44    * @return the instantaited Object
45    * @exception java.lang.Exception if instantiation failed
46    */

47   public static Object JavaDoc createObject(String JavaDoc className, Object JavaDoc[] params) throws
48       Exception JavaDoc {
49     return createObject(Class.forName(className), params);
50   }
51
52   /**
53    * Instantaite an Object instance, requires a constractor with parameters
54    * @param classObject, Class object representing the object type to be instantiated
55    * @param params an array including the required parameters to instantaite the object
56    * @return the instantaied Object
57    * @exception java.lang.Exception if instantiation failed
58    */

59   public static Object JavaDoc createObject(Class JavaDoc classObject, Object JavaDoc[] params) throws
60       Exception JavaDoc {
61     Constructor[] constructors = classObject.getConstructors();
62     Object JavaDoc object = null;
63     for (int counter = 0; counter < constructors.length; counter++) {
64       try {
65         object = constructors[counter].newInstance(params);
66       } catch (Exception JavaDoc e) {
67         if (e instanceof InvocationTargetException)
68           ( (InvocationTargetException) e).getTargetException().printStackTrace();
69         //do nothing, try the next constructor
70
}
71     }
72     if (object == null)
73       throw new InstantiationException JavaDoc();
74     return object;
75   }
76 }
Popular Tags