+ " possible constructor"
+ (numConstructors == 1 ? "" : "s"));
}
for (int i = 0; i < numConstructors; i++) {
Constructor constructor = constructors[i];
Class[] arguments = constructor.getParameterTypes();
int numArgs = arguments.length;
// First, check the number of arguments for a match
if (numArgs != numArgClasses) {
if (DEBUG) {
Debug.output(" - constructor " + i + " with " + numArgs
+ " arguments not a match");
}
continue; // Nope, not it.
}
// OK, empty constructor desired, punch...
if (numArgs == 0) {
if (DEBUG) {
Debug.output(" - constructor " + i
+ " with no arguments is a match");
}
return constructor;
}
// Check to see if the argument classes of the Constructor
// are
// assignable to the desired argClasses being sought.
boolean good = false;
for (int j = 0; j < numArgs; j++) {
if (arguments[j] == argClasses[j]) {
if (DEBUG) {
Debug.output(" - direct arg class match, arg " + j);
}
good = true; // Maintain true...
} else if (arguments[j].isAssignableFrom(argClasses[j])) {
// Doesn't work quite yet. May have to check for
// super-super class,etc, and we still get an
// IllegalArgumentException due to argument type
// mismatch.
// Is this even necessary? Don't think so...
argClasses[j] = argClasses[j].getSuperclass();
if (DEBUG) {
Debug.output(" - superclass arg class match, arg " + j
+ " reassigning to " + argClasses[j].toString());
}
good = true; // Maintain true...
// } else if (constructorArgs[j] instanceof
// Number) {
// if (DEBUG) {
// Debug.output(" - Number type match, arg " + j);
// }
// good = true; // Maintain true...
} else {
if (DEBUG) {
Debug.output(" - arg class mismatch on arg " + j
+ ", bailing (" + arguments[j].getName()
+ " vs. " + argClasses[j].getName() + ")");
}
good = false; // Punch with false
break;
}
}
if (good) {
if (DEBUG) {
Debug.debugging(" - creating object");
}
Object obj = constructor.newInstance(constructorArgs);
if (DEBUG) {
Debug.debugging(" - created object");
}
return obj;
}