[LANG-961] org.apache.commons.lang3.reflect.FieldUtils.removeFinalModifier(Field) does not clean up after itself. Only call setAccessible if neccessary and tell the caller about it so it can undo the change if need be.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1563014 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2014-01-30 23:28:26 +00:00
parent 1bc33d2211
commit 0c7b32a211
1 changed files with 6 additions and 4 deletions

View File

@ -49,20 +49,22 @@ abstract class MemberUtils {
* sufficiently privileged code. Better workarounds would be gratefully * sufficiently privileged code. Better workarounds would be gratefully
* accepted. * accepted.
* @param o the AccessibleObject to set as accessible * @param o the AccessibleObject to set as accessible
* @return a boolean indicating whether the accessibility of the object was set to true.
*/ */
static void setAccessibleWorkaround(final AccessibleObject o) { static boolean setAccessibleWorkaround(final AccessibleObject o) {
if (o == null || o.isAccessible()) { if (o == null || o.isAccessible()) {
return; return false;
} }
final Member m = (Member) o; final Member m = (Member) o;
if (Modifier.isPublic(m.getModifiers()) if (!o.isAccessible() && Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
&& isPackageAccess(m.getDeclaringClass().getModifiers())) {
try { try {
o.setAccessible(true); o.setAccessible(true);
return true;
} catch (final SecurityException e) { // NOPMD } catch (final SecurityException e) { // NOPMD
// ignore in favor of subsequent IllegalAccessException // ignore in favor of subsequent IllegalAccessException
} }
} }
return false;
} }
/** /**