Skip to content
Permalink
Browse files
Internal refactoring to allow to chain calls using
MemberUtils.setAccessibleWorkaround().
  • Loading branch information
garydgregory committed May 10, 2022
1 parent 54f0079 commit a2ed219fb8ae0c5c453a069863359b4ba69ff230
Showing 4 changed files with 7 additions and 13 deletions.
@@ -243,9 +243,7 @@ public static <T> Constructor<T> getMatchingAccessibleConstructor(final Class<T>
// 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 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 @@ private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls,
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

0 comments on commit a2ed219

Please sign in to comment.