in src/main/java/org/apache/commons/beanutils2/ConstructorUtils.java [126:193]
private static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T> clazz, final Class<?>[] parameterTypes) {
// see if we can find the method directly
// most of the time this works and it's much faster
try {
final Constructor<T> ctor = clazz.getConstructor(parameterTypes);
try {
//
// XXX Default access superclass workaround
//
// When a public class has a default access superclass
// with public methods, these methods are accessible.
// Calling them from compiled code works fine.
//
// Unfortunately, using reflection to invoke these methods
// seems to (wrongly) to prevent access even when the method
// modifier is public.
//
// The following workaround solves the problem but will only
// work from sufficiently privileges code.
//
// Better workarounds would be gratefully accepted.
//
ctor.setAccessible(true);
} catch (final SecurityException se) {
/* SWALLOW, if workaround fails don't fret. */
}
return ctor;
} catch (final NoSuchMethodException e) { /* SWALLOW */
}
// search through all methods
final int paramSize = parameterTypes.length;
final Constructor<?>[] ctors = clazz.getConstructors();
for (final Constructor<?> ctor2 : ctors) {
// compare parameters
final Class<?>[] ctorParams = ctor2.getParameterTypes();
final int ctorParamSize = ctorParams.length;
if (ctorParamSize == paramSize) {
boolean match = true;
for (int n = 0; n < ctorParamSize; n++) {
if (!MethodUtils.isAssignmentCompatible(ctorParams[n], parameterTypes[n])) {
match = false;
break;
}
}
if (match) {
// get accessible version of method
final Constructor<?> ctor = getAccessibleConstructor(ctor2);
if (ctor != null) {
try {
ctor.setAccessible(true);
} catch (final SecurityException se) {
/*
* Swallow SecurityException TODO: Why?
*/
}
// Class.getConstructors() actually returns constructors
// of type T, so it is safe to cast.
return (Constructor<T>) ctor;
}
}
}
}
return null;
}