public static Constructor getConstructor(Class type,
Class[] argTypes) {
if(null == type || null == argTypes) {
throw new NullPointerException();
}
Constructor ctor = null;
try {
ctor = type.getConstructor(argTypes);
} catch(Exception e) {
ctor = null;
}
if(null == ctor) {
// no directly declared matching constructor,
// look for something that will work
// XXX this should really be more careful to
// adhere to the jls mechanism for late binding
Constructor[] ctors = type.getConstructors();
for(int i=0;i< ctors.length;i++) {
Class[] paramtypes = ctors[i].getParameterTypes();
if(paramtypes.length == argTypes.length) {
boolean canuse = true;
for(int j=0;j< paramtypes.length;j++) {
if(paramtypes[j].isAssignableFrom(argTypes[j])) {
continue;
} else {
canuse = false;
break;
}
}
if(canuse == true) {
ctor = ctors[i];
break;
}
}
}
}
return ctor;
}
Returns a Constructor for the given method signature, or null
if no such Constructor can be found. |