1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DynamicClassLoader.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.lang
.reflect
.Constructor
;
35 public class DynamicClassLoader
{
38 * This method returns a class created by its 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
43 * @param className The name of the class to create.
44 * @return The created class.
46 public static Class
forName(String className
)
47 throws ClassNotFoundException
{
49 return Class
.forName(className
) ;
53 * Get an instance of a class. The empty constructor is used.
54 * @param className The class to instantiate.
55 * @return The instance of the class.
57 public Object
getInstance(String className
)
58 throws IllegalArgumentException
{
60 Class cls
= DynamicClassLoader
.forName(className
);
61 return cls
.newInstance();
62 } catch ( ClassNotFoundException e
) {
63 throw new IllegalArgumentException("Couldn't find " + className
65 } catch ( IllegalAccessException e
) {
66 throw new IllegalArgumentException("Couldn't access " + className
68 } catch ( InstantiationException e
) {
69 throw new IllegalArgumentException("Couldn't instantiate " +
75 * Get an instance of a class. The constructor matching to the
76 * arguments is used and the arguments are given to this constructor.
77 * @param className The class to instantiate.
78 * @param ctorArgs Arguments for the constructor.
79 * @return The instance of the class.
81 public Object
getInstance(String className
, Object
[] ctorArgs
)
82 throws IllegalArgumentException
{
83 Class
[] ctorType
= new Class
[ctorArgs
.length
];
84 for(int i
=0; i
<ctorType
.length
; i
++) {
85 ctorType
[i
] = ctorArgs
[i
].getClass();
87 return getInstance(className
, ctorType
, ctorArgs
);
92 * Get an instance of a class. The constructor matching to the
93 * given calss types is used and the instance is created using the arguments
94 * for the constructor.
95 * @param className The class to instantiate.
96 * @param ctorClassTypes The class types matching to the constructor.
97 * @param ctorArgs Arguments for the constructor.
98 * @return The instance of the class.
100 public Object
getInstance(String className
, Class
[]ctorClassTypes
, Object
[] ctorArgs
)
101 throws IllegalArgumentException
{
103 Class cls
= DynamicClassLoader
.forName(className
);
104 Constructor ctor
= cls
.getConstructor(ctorClassTypes
);
105 System
.out
.println("ctor: " + ctor
.getName() + " " + ctor
.getModifiers());
107 return ctor
.newInstance(ctorArgs
);
108 } catch ( ClassNotFoundException e
) {
109 throw new IllegalArgumentException("Couldn't find " + className
111 } catch ( IllegalAccessException e
) {
112 throw new IllegalArgumentException("Couldn't access " + className
114 } catch ( NoSuchMethodException e
) {
115 throw new IllegalArgumentException("Couldn't find constructor for " + className
117 } catch ( java
.lang
.reflect
.InvocationTargetException e
) {
119 throw new IllegalArgumentException("Couldn't invoke " +
120 className
+ " " + e
);
121 } catch ( InstantiationException e
) {
122 throw new IllegalArgumentException("Couldn't instantiate " +
123 className
+ " " + e
);