1 16 17 package org.apache.commons.jocl; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.InvocationTargetException ; 21 22 28 public class ConstructorUtil { 29 39 public static Constructor getConstructor(Class type, Class [] argTypes) { 40 if(null == type || null == argTypes) { 41 throw new NullPointerException (); 42 } 43 Constructor ctor = null; 44 try { 45 ctor = type.getConstructor(argTypes); 46 } catch(Exception e) { 47 ctor = null; 48 } 49 if(null == ctor) { 50 Constructor [] ctors = type.getConstructors(); 55 for(int i=0;i<ctors.length;i++) { 56 Class [] paramtypes = ctors[i].getParameterTypes(); 57 if(paramtypes.length == argTypes.length) { 58 boolean canuse = true; 59 for(int j=0;j<paramtypes.length;j++) { 60 if(paramtypes[j].isAssignableFrom(argTypes[j])) { 61 continue; 62 } else { 63 canuse = false; 64 break; 65 } 66 } 67 if(canuse == true) { 68 ctor = ctors[i]; 69 break; 70 } 71 } 72 } 73 } 74 return ctor; 75 } 76 77 92 public static Object invokeConstructor(Class type, Class [] argTypes, Object [] argValues) throws InstantiationException , IllegalAccessException , InvocationTargetException { 93 return ConstructorUtil.getConstructor(type,argTypes).newInstance(argValues); 94 } 95 } 96 97 98 | Popular Tags |