Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / util / DynamicClassLoader.java
blob659593e693462d2ebd834e00577446934ac4a9dd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package util;
30 import java.lang.reflect.Constructor;
32 public class DynamicClassLoader {
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.
43 public static Class forName(String className)
44 throws ClassNotFoundException {
46 return Class.forName(className) ;
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.
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);
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.
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();
84 return getInstance(className, ctorType, ctorArgs);
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.
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());
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);