1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package util; 25 26 import java.lang.reflect.Constructor; 27 28 public class DynamicClassLoader { 29 30 /** 31 * This method returns a class created by its name 32 * created by call to <code>Class.forName()</code>.<p> 33 * This method must be overloaded if another loading 34 * policy is required for Component and Interface 35 * testing classes. 36 * @param className The name of the class to create. 37 * @return The created class. 38 */ forName(String className)39 public static Class forName(String className) 40 throws ClassNotFoundException { 41 42 return Class.forName(className) ; 43 } 44 45 /** 46 * Get an instance of a class. The empty constructor is used. 47 * @param className The class to instantiate. 48 * @return The instance of the class. 49 */ getInstance(String className)50 public Object getInstance(String className) 51 throws IllegalArgumentException { 52 try { 53 Class cls = DynamicClassLoader.forName(className); 54 return cls.newInstance(); 55 } catch ( ClassNotFoundException e ) { 56 throw new IllegalArgumentException("Couldn't find " + className 57 + " " + e); 58 } catch ( IllegalAccessException e ) { 59 throw new IllegalArgumentException("Couldn't access " + className 60 + " " + e); 61 } catch ( InstantiationException e ) { 62 throw new IllegalArgumentException("Couldn't instantiate " + 63 className + " " + e); 64 } 65 } 66 67 /** 68 * Get an instance of a class. The constructor matching to the 69 * arguments is used and the arguments are given to this constructor. 70 * @param className The class to instantiate. 71 * @param ctorArgs Arguments for the constructor. 72 * @return The instance of the class. 73 */ getInstance(String className, Object[] ctorArgs)74 public Object getInstance(String className, Object[] ctorArgs) 75 throws IllegalArgumentException { 76 Class[] ctorType = new Class[ctorArgs.length]; 77 for(int i=0; i<ctorType.length; i++) { 78 ctorType[i] = ctorArgs[i].getClass(); 79 } 80 return getInstance(className, ctorType, ctorArgs); 81 82 } 83 84 /** 85 * Get an instance of a class. The constructor matching to the 86 * given calss types is used and the instance is created using the arguments 87 * for the constructor. 88 * @param className The class to instantiate. 89 * @param ctorClassTypes The class types matching to the constructor. 90 * @param ctorArgs Arguments for the constructor. 91 * @return The instance of the class. 92 */ getInstance(String className, Class[]ctorClassTypes, Object[] ctorArgs)93 public Object getInstance(String className, Class[]ctorClassTypes, Object[] ctorArgs) 94 throws IllegalArgumentException { 95 try { 96 Class cls = DynamicClassLoader.forName(className); 97 Constructor ctor = cls.getConstructor(ctorClassTypes); 98 System.out.println("ctor: " + ctor.getName() + " " + ctor.getModifiers()); 99 100 return ctor.newInstance(ctorArgs); 101 } catch ( ClassNotFoundException e ) { 102 throw new IllegalArgumentException("Couldn't find " + className 103 + " " + e); 104 } catch ( IllegalAccessException e ) { 105 throw new IllegalArgumentException("Couldn't access " + className 106 + " " + e); 107 } catch ( NoSuchMethodException e ) { 108 throw new IllegalArgumentException("Couldn't find constructor for " + className 109 + " " + e); 110 } catch ( java.lang.reflect.InvocationTargetException e ) { 111 e.printStackTrace(); 112 throw new IllegalArgumentException("Couldn't invoke " + 113 className + " " + e); 114 } catch ( InstantiationException e ) { 115 throw new IllegalArgumentException("Couldn't instantiate " + 116 className + " " + e); 117 } 118 } 119 } 120