xref: /AOO41X/main/qadevOOo/runner/lib/DynamicClassLoader.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 lib ;
29 
30 import java.lang.reflect.Constructor;
31 
32 /**
33  * @deprecated: moved to util package.
34  */
35 public class DynamicClassLoader {
36 
37 	/**
38 	* This method returns a class created by it's name
39 	* created by call to <code>Class.forName()</code>.<p>
40 	* This method must be overloaded if another loading
41 	* policy is required for Component and Interface
42 	* testing classes.
43 	*/
44 	public static Class forName(String className)
45 		throws ClassNotFoundException {
46 
47 		return Class.forName(className) ;
48 	}
49 
50         public Object getInstance(String className)
51                                             throws IllegalArgumentException {
52             try {
53                 Class cls = DynamicClassLoader.forName(className);
54                 return cls.newInstance();
55             } catch ( ClassNotFoundException e ) {
56                 throw new IllegalArgumentException("Couldn't find " + className
57                         + " " + e);
58             } catch ( IllegalAccessException e ) {
59                 throw new IllegalArgumentException("Couldn't access " + className
60                         + " " + e);
61             } catch ( InstantiationException e ) {
62                 throw new IllegalArgumentException("Couldn't instantiate " +
63                                 className + " " + e);
64             }
65         }
66 
67         public Object getInstance(String className, Object[] ctorArgs)
68                                             throws IllegalArgumentException {
69             try {
70                 Class cls = DynamicClassLoader.forName(className);
71                 Class[] ctorType = new Class[ctorArgs.length];
72                 for(int i=0; i<ctorType.length; i++) {
73                     ctorType[i] = ctorArgs[i].getClass();
74                 }
75                 Constructor ctor = cls.getConstructor(ctorType);
76                 return ctor.newInstance(ctorArgs);
77             } catch ( ClassNotFoundException e ) {
78                 throw new IllegalArgumentException("Couldn't find " + className
79                         + " " + e);
80             } catch ( IllegalAccessException e ) {
81                 throw new IllegalArgumentException("Couldn't access " + className
82                         + " " + e);
83             } catch ( NoSuchMethodException e ) {
84                 throw new IllegalArgumentException("Couldn't find constructor for " + className
85                         + " " + e);
86             } catch ( java.lang.reflect.InvocationTargetException e ) {
87                 throw new IllegalArgumentException("Couldn't invoke " +
88                                 className + " " + e);
89             } catch ( InstantiationException e ) {
90                 throw new IllegalArgumentException("Couldn't instantiate " +
91                                 className + " " + e);
92             }
93         }
94 }
95