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
;
36 * @deprecated: moved to util package.
38 public class DynamicClassLoader
{
41 * This method returns a class created by it's name
42 * created by call to <code>Class.forName()</code>.<p>
43 * This method must be overloaded if another loading
44 * policy is required for Component and Interface
47 public static Class
forName(String className
)
48 throws ClassNotFoundException
{
50 return Class
.forName(className
) ;
53 public Object
getInstance(String className
)
54 throws IllegalArgumentException
{
56 Class cls
= DynamicClassLoader
.forName(className
);
57 return cls
.newInstance();
58 } catch ( ClassNotFoundException e
) {
59 throw new IllegalArgumentException("Couldn't find " + className
61 } catch ( IllegalAccessException e
) {
62 throw new IllegalArgumentException("Couldn't access " + className
64 } catch ( InstantiationException e
) {
65 throw new IllegalArgumentException("Couldn't instantiate " +
70 public Object
getInstance(String className
, Object
[] ctorArgs
)
71 throws IllegalArgumentException
{
73 Class cls
= DynamicClassLoader
.forName(className
);
74 Class
[] ctorType
= new Class
[ctorArgs
.length
];
75 for(int i
=0; i
<ctorType
.length
; i
++) {
76 ctorType
[i
] = ctorArgs
[i
].getClass();
78 Constructor ctor
= cls
.getConstructor(ctorType
);
79 return ctor
.newInstance(ctorArgs
);
80 } catch ( ClassNotFoundException e
) {
81 throw new IllegalArgumentException("Couldn't find " + className
83 } catch ( IllegalAccessException e
) {
84 throw new IllegalArgumentException("Couldn't access " + className
86 } catch ( NoSuchMethodException e
) {
87 throw new IllegalArgumentException("Couldn't find constructor for " + className
89 } catch ( java
.lang
.reflect
.InvocationTargetException e
) {
90 throw new IllegalArgumentException("Couldn't invoke " +
92 } catch ( InstantiationException e
) {
93 throw new IllegalArgumentException("Couldn't instantiate " +