Remove unsafe reflection trying to access the modifiers field (#1517)

This commit is contained in:
Pasqual Koschmieder 2022-02-25 05:56:22 +01:00 committed by GitHub
parent 151d4a289f
commit baecaf4ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 15 deletions

View File

@ -395,20 +395,12 @@ public class FieldUtils {
writeStaticField(field, value); writeStaticField(field, value);
} }
/**
* @deprecated Use {@link #writeStaticField(Class, String, Object, boolean)} instead.
*/
@Deprecated
public static void writeStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess) throws Exception { public static void writeStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess) throws Exception {
Field field = getField(clazz, fieldName, forceAccess); writeStaticField(clazz, fieldName, value, forceAccess);
if (field == null) {
throw new IllegalArgumentException("Cannot locate field " + fieldName + " in " + clazz);
}
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.set(null, value);
} }
/** /**

View File

@ -308,7 +308,9 @@ public class StructureModifier<TField> {
* @param fieldIndex - index of the field. * @param fieldIndex - index of the field.
* @param value - TRUE if this field should be read only, FALSE otherwise. * @param value - TRUE if this field should be read only, FALSE otherwise.
* @throws FieldAccessException If we cannot modify the read-only status. * @throws FieldAccessException If we cannot modify the read-only status.
* @deprecated In recent java versions (starting at 9) the modifier field is secured and will not be writeable.
*/ */
@Deprecated
public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessException { public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessException {
if (fieldIndex < 0 || fieldIndex >= data.size()) if (fieldIndex < 0 || fieldIndex >= data.size())
throw new IllegalArgumentException("Index parameter is not within [0 - " + data.size() + ")"); throw new IllegalArgumentException("Index parameter is not within [0 - " + data.size() + ")");
@ -325,7 +327,9 @@ public class StructureModifier<TField> {
* @param field - the field to change. * @param field - the field to change.
* @param isReadOnly - TRUE if the field should be read only, FALSE otherwise. * @param isReadOnly - TRUE if the field should be read only, FALSE otherwise.
* @throws IllegalAccessException If an error occured. * @throws IllegalAccessException If an error occured.
* @deprecated In recent java versions (starting at 9) the modifier field is secured and will not be writeable.
*/ */
@Deprecated
protected static void setFinalState(Field field, boolean isReadOnly) throws IllegalAccessException { protected static void setFinalState(Field field, boolean isReadOnly) throws IllegalAccessException {
if (isReadOnly) if (isReadOnly)
FieldUtils.writeField((Object) field, "modifiers", field.getModifiers() | Modifier.FINAL, true); FieldUtils.writeField((Object) field, "modifiers", field.getModifiers() | Modifier.FINAL, true);

View File

@ -58,8 +58,6 @@ public abstract class CompiledStructureModifier extends StructureModifier<Object
throw new IllegalStateException("Cannot make compiled field " + fieldIndex + " read only."); throw new IllegalStateException("Cannot make compiled field " + fieldIndex + " read only.");
} }
} }
super.setReadOnly(fieldIndex, value);
} }
// Speed up the default writer // Speed up the default writer