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 // see if we can find the constructor directly
// most of the time this works and it's much faster // most of the time this works and it's much faster
try { try {
final Constructor<T> ctor = cls.getConstructor(parameterTypes); return MemberUtils.setAccessibleWorkaround(cls.getConstructor(parameterTypes));
MemberUtils.setAccessibleWorkaround(ctor);
return ctor;
} catch (final NoSuchMethodException e) { // NOPMD - Swallow } catch (final NoSuchMethodException e) { // NOPMD - Swallow
} }
Constructor<T> result = null; 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 * if the class is {@code null}, or the field name is blank or empty
*/ */
public static Field getField(final Class<?> cls, final String fieldName) { public static Field getField(final Class<?> cls, final String fieldName) {
final Field field = getField(cls, fieldName, false); return MemberUtils.setAccessibleWorkaround(getField(cls, fieldName, false));
MemberUtils.setAccessibleWorkaround(field);
return field;
} }
/** /**

View File

@ -52,20 +52,20 @@ final class MemberUtils {
* @param obj the AccessibleObject to set as accessible * @param obj the AccessibleObject to set as accessible
* @return a boolean indicating whether the accessibility of the object was set to true. * @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()) { if (obj == null || obj.isAccessible()) {
return false; return obj;
} }
final Member m = (Member) obj; final Member m = (Member) obj;
if (!obj.isAccessible() && isPublic(m) && isPackageAccess(m.getDeclaringClass().getModifiers())) { if (!obj.isAccessible() && isPublic(m) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
try { try {
obj.setAccessible(true); obj.setAccessible(true);
return true; return obj;
} catch (final SecurityException e) { // NOPMD } catch (final SecurityException e) { // NOPMD
// ignore in favor of subsequent IllegalAccessException // 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, public static Method getMatchingAccessibleMethod(final Class<?> cls,
final String methodName, final Class<?>... parameterTypes) { final String methodName, final Class<?>... parameterTypes) {
try { try {
final Method method = cls.getMethod(methodName, parameterTypes); return MemberUtils.setAccessibleWorkaround(cls.getMethod(methodName, parameterTypes));
MemberUtils.setAccessibleWorkaround(method);
return method;
} catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception } catch (final NoSuchMethodException e) { // NOPMD - Swallow the exception
} }
// search through all methods // search through all methods