1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package util;
29 
30 import java.lang.reflect.Constructor;
31 
32 public class DynamicClassLoader {
33 
34 	/**
35 	* This method returns a class created by its name
36 	* created by call to <code>Class.forName()</code>.<p>
37 	* This method must be overloaded if another loading
38 	* policy is required for Component and Interface
39 	* testing classes.
40     * @param className The name of the class to create.
41     * @return The created class.
42 	*/
43 	public static Class forName(String className)
44 		throws ClassNotFoundException {
45 
46 		return Class.forName(className) ;
47 	}
48 
49     /**
50      * Get an instance of a class. The empty constructor is used.
51      * @param className The class to instantiate.
52      * @return The instance of the class.
53      */
54     public Object getInstance(String className)
55                                         throws IllegalArgumentException {
56         try {
57             Class cls = DynamicClassLoader.forName(className);
58             return cls.newInstance();
59         } catch ( ClassNotFoundException e ) {
60             throw new IllegalArgumentException("Couldn't find " + className
61                     + " " + e);
62         } catch ( IllegalAccessException e ) {
63             throw new IllegalArgumentException("Couldn't access " + className
64                     + " " + e);
65         } catch ( InstantiationException e ) {
66             throw new IllegalArgumentException("Couldn't instantiate " +
67                             className + " " + e);
68         }
69     }
70 
71     /**
72      * Get an instance of a class. The constructor matching to the
73      * arguments is used and the arguments are given to this constructor.
74      * @param className The class to instantiate.
75      * @param ctorArgs Arguments for the constructor.
76      * @return The instance of the class.
77      */
78     public Object getInstance(String className, Object[] ctorArgs)
79                                         throws IllegalArgumentException {
80         Class[] ctorType = new Class[ctorArgs.length];
81         for(int i=0; i<ctorType.length; i++) {
82             ctorType[i] = ctorArgs[i].getClass();
83         }
84         return getInstance(className, ctorType, ctorArgs);
85 
86     }
87 
88     /**
89      * Get an instance of a class. The constructor matching to the
90      * given calss types is used and the instance is created using the arguments
91      * for the constructor.
92      * @param className The class to instantiate.
93      * @param ctorClassTypes The class types matching to the constructor.
94      * @param ctorArgs Arguments for the constructor.
95      * @return The instance of the class.
96      */
97     public Object getInstance(String className, Class[]ctorClassTypes, Object[] ctorArgs)
98                                         throws IllegalArgumentException {
99         try {
100             Class cls = DynamicClassLoader.forName(className);
101             Constructor ctor = cls.getConstructor(ctorClassTypes);
102             System.out.println("ctor: " + ctor.getName() +  "  " + ctor.getModifiers());
103 
104             return ctor.newInstance(ctorArgs);
105         } catch ( ClassNotFoundException e ) {
106             throw new IllegalArgumentException("Couldn't find " + className
107                     + " " + e);
108         } catch ( IllegalAccessException e ) {
109             throw new IllegalArgumentException("Couldn't access " + className
110                     + " " + e);
111         } catch ( NoSuchMethodException e ) {
112             throw new IllegalArgumentException("Couldn't find constructor for " + className
113                     + " " + e);
114         } catch ( java.lang.reflect.InvocationTargetException e ) {
115             e.printStackTrace();
116             throw new IllegalArgumentException("Couldn't invoke " +
117                             className + " " + e);
118         } catch ( InstantiationException e ) {
119             throw new IllegalArgumentException("Couldn't instantiate " +
120                             className + " " + e);
121         }
122     }
123 }
124