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 .
21 import java
.lang
.reflect
.Constructor
;
23 public class DynamicClassLoader
{
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 overridden if another loading
29 * policy is required for Component and Interface
31 * @param className The name of the class to create.
32 * @return The created class.
34 private static Class
<?
> forName(String className
)
35 throws ClassNotFoundException
{
37 return Class
.forName(className
) ;
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
{
48 Class
<?
> cls
= DynamicClassLoader
.forName(className
);
49 return cls
.newInstance();
50 } catch ( ClassNotFoundException e
) {
51 throw new IllegalArgumentException("Couldn't find " + className
53 } catch ( IllegalAccessException e
) {
54 throw new IllegalArgumentException("Couldn't access " + className
56 } catch ( InstantiationException e
) {
57 throw new IllegalArgumentException("Couldn't instantiate " +
63 * Get an instance of a class. The constructor matching to the
64 * given calls types is used and the instance is created using the arguments
65 * for the constructor.
66 * @param className The class to instantiate.
67 * @param ctorClassTypes The class types matching to the constructor.
68 * @param ctorArgs Arguments for the constructor.
69 * @return The instance of the class.
71 public Object
getInstance(String className
, Class
<?
>[]ctorClassTypes
, Object
[] ctorArgs
)
72 throws IllegalArgumentException
{
74 Class
<?
> cls
= DynamicClassLoader
.forName(className
);
75 Constructor
<?
> ctor
= cls
.getConstructor(ctorClassTypes
);
76 System
.out
.println("ctor: " + ctor
.getName() + " " + ctor
.getModifiers());
78 return ctor
.newInstance(ctorArgs
);
79 } catch ( ClassNotFoundException e
) {
80 throw new IllegalArgumentException("Couldn't find " + className
82 } catch ( IllegalAccessException e
) {
83 throw new IllegalArgumentException("Couldn't access " + className
85 } catch ( NoSuchMethodException e
) {
86 throw new IllegalArgumentException("Couldn't find constructor for " + className
88 } catch ( java
.lang
.reflect
.InvocationTargetException e
) {
90 throw new IllegalArgumentException("Couldn't invoke " +
92 } catch ( InstantiationException e
) {
93 throw new IllegalArgumentException("Couldn't instantiate " +