Internal refactoring to allow to chain calls using

MemberUtils.setAccessibleWorkaround().
This commit is contained in:
Gary Gregory 2022-05-10 18:35:20 -04:00
parent 54f00795a3
commit a2ed219fb8
4 changed files with 7 additions and 13 deletions

View File

@ -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;

View File

@ -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));
}
/**

View File

@ -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;
}
/**

View File

@ -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