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 lib ; 25 26 import java.lang.reflect.Constructor; 27 28 /** 29 * @deprecated: moved to util package. 30 */ 31 public class DynamicClassLoader { 32 33 /** 34 * This method returns a class created by it's name 35 * created by call to <code>Class.forName()</code>.<p> 36 * This method must be overloaded if another loading 37 * policy is required for Component and Interface 38 * testing classes. 39 */ forName(String className)40 public static Class forName(String className) 41 throws ClassNotFoundException { 42 43 return Class.forName(className) ; 44 } 45 getInstance(String className)46 public Object getInstance(String className) 47 throws IllegalArgumentException { 48 try { 49 Class cls = DynamicClassLoader.forName(className); 50 return cls.newInstance(); 51 } catch ( ClassNotFoundException e ) { 52 throw new IllegalArgumentException("Couldn't find " + className 53 + " " + e); 54 } catch ( IllegalAccessException e ) { 55 throw new IllegalArgumentException("Couldn't access " + className 56 + " " + e); 57 } catch ( InstantiationException e ) { 58 throw new IllegalArgumentException("Couldn't instantiate " + 59 className + " " + e); 60 } 61 } 62 getInstance(String className, Object[] ctorArgs)63 public Object getInstance(String className, Object[] ctorArgs) 64 throws IllegalArgumentException { 65 try { 66 Class cls = DynamicClassLoader.forName(className); 67 Class[] ctorType = new Class[ctorArgs.length]; 68 for(int i=0; i<ctorType.length; i++) { 69 ctorType[i] = ctorArgs[i].getClass(); 70 } 71 Constructor ctor = cls.getConstructor(ctorType); 72 return ctor.newInstance(ctorArgs); 73 } catch ( ClassNotFoundException e ) { 74 throw new IllegalArgumentException("Couldn't find " + className 75 + " " + e); 76 } catch ( IllegalAccessException e ) { 77 throw new IllegalArgumentException("Couldn't access " + className 78 + " " + e); 79 } catch ( NoSuchMethodException e ) { 80 throw new IllegalArgumentException("Couldn't find constructor for " + className 81 + " " + e); 82 } catch ( java.lang.reflect.InvocationTargetException e ) { 83 throw new IllegalArgumentException("Couldn't invoke " + 84 className + " " + e); 85 } catch ( InstantiationException e ) { 86 throw new IllegalArgumentException("Couldn't instantiate " + 87 className + " " + e); 88 } 89 } 90 } 91