Internal refactoring to allow to chain calls using
MemberUtils.setAccessibleWorkaround().
This commit is contained in:
parent
54f00795a3
commit
a2ed219fb8
|
@ -243,9 +243,7 @@ public class ConstructorUtils {
|
|||
// see if we can find the constructor directly
|
||||
// most of the time this works and it's much faster
|
||||
try {
|
||||
final Constructor<T> ctor = cls.getConstructor(parameterTypes);
|
||||
MemberUtils.setAccessibleWorkaround(ctor);
|
||||
return ctor;
|
||||
return MemberUtils.setAccessibleWorkaround(cls.getConstructor(parameterTypes));
|
||||
} catch (final NoSuchMethodException e) { // NOPMD - Swallow
|
||||
}
|
||||
Constructor<T> result = null;
|
||||
|
|
|
@ -62,9 +62,7 @@ public class FieldUtils {
|
|||
* if the class is {@code null}, or the field name is blank or empty
|
||||
*/
|
||||
public static Field getField(final Class<?> cls, final String fieldName) {
|
||||
final Field field = getField(cls, fieldName, false);
|
||||
MemberUtils.setAccessibleWorkaround(field);
|
||||
return field;
|
||||
return MemberUtils.setAccessibleWorkaround(getField(cls, fieldName, false));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,20 +52,20 @@ final class MemberUtils {
|
|||
* @param obj the AccessibleObject to set as accessible
|
||||
* @return a boolean indicating whether the accessibility of the object was set to true.
|
||||
*/
|
||||
static boolean setAccessibleWorkaround(final AccessibleObject obj) {
|
||||
static <T extends AccessibleObject> T setAccessibleWorkaround(final T obj) {
|
||||
if (obj == null || obj.isAccessible()) {
|
||||
return false;
|
||||
return obj;
|
||||
}
|
||||
final Member m = (Member) obj;
|
||||
if (!obj.isAccessible() && isPublic(m) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
|
||||
try {
|
||||
obj.setAccessible(true);
|
||||
return true;
|
||||
return obj;
|
||||
} catch (final SecurityException e) { // NOPMD
|
||||
// ignore in favor of subsequent IllegalAccessException
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -667,9 +667,7 @@ public class MethodUtils {
|
|||
public static Method getMatchingAccessibleMethod(final Class<?> cls,
|
||||
final String methodName, final Class<?>... parameterTypes) {
|
||||
try {
|
||||
final Method method = cls.getMethod(methodName, parameterTypes);
|
||||
MemberUtils.setAccessibleWorkaround(method);
|
||||
return method;
|
||||
return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes));
|
||||
} catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception
|
||||
}
|
||||
// search through all methods
|
||||
|
|
Loading…
Reference in New Issue