bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / util / DynamicClassLoader.java
blobdbb3dd7972c238aaae6692c08f8af2472bd1d887
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package util;
21 import java.lang.reflect.Constructor;
23 public class DynamicClassLoader {
25 /**
26 * This method returns a class created by its name
27 * created by call to <code>Class.forName()</code>.<p>
28 * This method must be overloaded if another loading
29 * policy is required for Component and Interface
30 * testing classes.
31 * @param className The name of the class to create.
32 * @return The created class.
34 public static Class<?> forName(String className)
35 throws ClassNotFoundException {
37 return Class.forName(className) ;
40 /**
41 * Get an instance of a class. The empty constructor is used.
42 * @param className The class to instantiate.
43 * @return The instance of the class.
45 public Object getInstance(String className)
46 throws IllegalArgumentException {
47 try {
48 Class<?> cls = DynamicClassLoader.forName(className);
49 return cls.newInstance();
50 } catch ( ClassNotFoundException e ) {
51 throw new IllegalArgumentException("Couldn't find " + className
52 + " " + e);
53 } catch ( IllegalAccessException e ) {
54 throw new IllegalArgumentException("Couldn't access " + className
55 + " " + e);
56 } catch ( InstantiationException e ) {
57 throw new IllegalArgumentException("Couldn't instantiate " +
58 className + " " + e);
62 /**
63 * Get an instance of a class. The constructor matching to the
64 * arguments is used and the arguments are given to this constructor.
65 * @param className The class to instantiate.
66 * @param ctorArgs Arguments for the constructor.
67 * @return The instance of the class.
69 public Object getInstance(String className, Object[] ctorArgs)
70 throws IllegalArgumentException {
71 Class<?>[] ctorType = new Class[ctorArgs.length];
72 for(int i=0; i<ctorType.length; i++) {
73 ctorType[i] = ctorArgs[i].getClass();
75 return getInstance(className, ctorType, ctorArgs);
79 /**
80 * Get an instance of a class. The constructor matching to the
81 * given calss types is used and the instance is created using the arguments
82 * for the constructor.
83 * @param className The class to instantiate.
84 * @param ctorClassTypes The class types matching to the constructor.
85 * @param ctorArgs Arguments for the constructor.
86 * @return The instance of the class.
88 public Object getInstance(String className, Class<?>[]ctorClassTypes, Object[] ctorArgs)
89 throws IllegalArgumentException {
90 try {
91 Class<?> cls = DynamicClassLoader.forName(className);
92 Constructor<?> ctor = cls.getConstructor(ctorClassTypes);
93 System.out.println("ctor: " + ctor.getName() + " " + ctor.getModifiers());
95 return ctor.newInstance(ctorArgs);
96 } catch ( ClassNotFoundException e ) {
97 throw new IllegalArgumentException("Couldn't find " + className
98 + " " + e);
99 } catch ( IllegalAccessException e ) {
100 throw new IllegalArgumentException("Couldn't access " + className
101 + " " + e);
102 } catch ( NoSuchMethodException e ) {
103 throw new IllegalArgumentException("Couldn't find constructor for " + className
104 + " " + e);
105 } catch ( java.lang.reflect.InvocationTargetException e ) {
106 e.printStackTrace();
107 throw new IllegalArgumentException("Couldn't invoke " +
108 className + " " + e);
109 } catch ( InstantiationException e ) {
110 throw new IllegalArgumentException("Couldn't instantiate " +
111 className + " " + e);