1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.lang.reflect.Constructor;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir public class DynamicClassLoader {
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 	/**
31cdf0e10cSrcweir 	* This method returns a class created by its name
32cdf0e10cSrcweir 	* created by call to <code>Class.forName()</code>.<p>
33cdf0e10cSrcweir 	* This method must be overloaded if another loading
34cdf0e10cSrcweir 	* policy is required for Component and Interface
35cdf0e10cSrcweir 	* testing classes.
36cdf0e10cSrcweir     * @param className The name of the class to create.
37cdf0e10cSrcweir     * @return The created class.
38cdf0e10cSrcweir 	*/
forName(String className)39cdf0e10cSrcweir 	public static Class forName(String className)
40cdf0e10cSrcweir 		throws ClassNotFoundException {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 		return Class.forName(className) ;
43cdf0e10cSrcweir 	}
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     /**
46cdf0e10cSrcweir      * Get an instance of a class. The empty constructor is used.
47cdf0e10cSrcweir      * @param className The class to instantiate.
48cdf0e10cSrcweir      * @return The instance of the class.
49cdf0e10cSrcweir      */
getInstance(String className)50cdf0e10cSrcweir     public Object getInstance(String className)
51cdf0e10cSrcweir                                         throws IllegalArgumentException {
52cdf0e10cSrcweir         try {
53cdf0e10cSrcweir             Class cls = DynamicClassLoader.forName(className);
54cdf0e10cSrcweir             return cls.newInstance();
55cdf0e10cSrcweir         } catch ( ClassNotFoundException e ) {
56cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't find " + className
57cdf0e10cSrcweir                     + " " + e);
58cdf0e10cSrcweir         } catch ( IllegalAccessException e ) {
59cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't access " + className
60cdf0e10cSrcweir                     + " " + e);
61cdf0e10cSrcweir         } catch ( InstantiationException e ) {
62cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't instantiate " +
63cdf0e10cSrcweir                             className + " " + e);
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /**
68cdf0e10cSrcweir      * Get an instance of a class. The constructor matching to the
69cdf0e10cSrcweir      * arguments is used and the arguments are given to this constructor.
70cdf0e10cSrcweir      * @param className The class to instantiate.
71cdf0e10cSrcweir      * @param ctorArgs Arguments for the constructor.
72cdf0e10cSrcweir      * @return The instance of the class.
73cdf0e10cSrcweir      */
getInstance(String className, Object[] ctorArgs)74cdf0e10cSrcweir     public Object getInstance(String className, Object[] ctorArgs)
75cdf0e10cSrcweir                                         throws IllegalArgumentException {
76cdf0e10cSrcweir         Class[] ctorType = new Class[ctorArgs.length];
77cdf0e10cSrcweir         for(int i=0; i<ctorType.length; i++) {
78cdf0e10cSrcweir             ctorType[i] = ctorArgs[i].getClass();
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         return getInstance(className, ctorType, ctorArgs);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /**
85cdf0e10cSrcweir      * Get an instance of a class. The constructor matching to the
86cdf0e10cSrcweir      * given calss types is used and the instance is created using the arguments
87cdf0e10cSrcweir      * for the constructor.
88cdf0e10cSrcweir      * @param className The class to instantiate.
89cdf0e10cSrcweir      * @param ctorClassTypes The class types matching to the constructor.
90cdf0e10cSrcweir      * @param ctorArgs Arguments for the constructor.
91cdf0e10cSrcweir      * @return The instance of the class.
92cdf0e10cSrcweir      */
getInstance(String className, Class[]ctorClassTypes, Object[] ctorArgs)93cdf0e10cSrcweir     public Object getInstance(String className, Class[]ctorClassTypes, Object[] ctorArgs)
94cdf0e10cSrcweir                                         throws IllegalArgumentException {
95cdf0e10cSrcweir         try {
96cdf0e10cSrcweir             Class cls = DynamicClassLoader.forName(className);
97cdf0e10cSrcweir             Constructor ctor = cls.getConstructor(ctorClassTypes);
98cdf0e10cSrcweir             System.out.println("ctor: " + ctor.getName() +  "  " + ctor.getModifiers());
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             return ctor.newInstance(ctorArgs);
101cdf0e10cSrcweir         } catch ( ClassNotFoundException e ) {
102cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't find " + className
103cdf0e10cSrcweir                     + " " + e);
104cdf0e10cSrcweir         } catch ( IllegalAccessException e ) {
105cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't access " + className
106cdf0e10cSrcweir                     + " " + e);
107cdf0e10cSrcweir         } catch ( NoSuchMethodException e ) {
108cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't find constructor for " + className
109cdf0e10cSrcweir                     + " " + e);
110cdf0e10cSrcweir         } catch ( java.lang.reflect.InvocationTargetException e ) {
111cdf0e10cSrcweir             e.printStackTrace();
112cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't invoke " +
113cdf0e10cSrcweir                             className + " " + e);
114cdf0e10cSrcweir         } catch ( InstantiationException e ) {
115cdf0e10cSrcweir             throw new IllegalArgumentException("Couldn't instantiate " +
116cdf0e10cSrcweir                             className + " " + e);
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir }
120