From baecaf4ca4cb17597d7c7ad09f8cd2ee4d21dab8 Mon Sep 17 00:00:00 2001 From: Pasqual Koschmieder Date: Fri, 25 Feb 2022 05:56:22 +0100 Subject: [PATCH] Remove unsafe reflection trying to access the modifiers field (#1517) --- .../comphenix/protocol/reflect/FieldUtils.java | 18 +++++------------- .../protocol/reflect/StructureModifier.java | 4 ++++ .../compiler/CompiledStructureModifier.java | 2 -- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/reflect/FieldUtils.java b/src/main/java/com/comphenix/protocol/reflect/FieldUtils.java index 2fcb821b..c4b13d5d 100644 --- a/src/main/java/com/comphenix/protocol/reflect/FieldUtils.java +++ b/src/main/java/com/comphenix/protocol/reflect/FieldUtils.java @@ -395,20 +395,12 @@ public class FieldUtils { 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 { - Field field = getField(clazz, fieldName, 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); + writeStaticField(clazz, fieldName, value, forceAccess); } /** diff --git a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 1577570f..523faf02 100644 --- a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -308,7 +308,9 @@ public class StructureModifier { * @param fieldIndex - index of the field. * @param value - TRUE if this field should be read only, FALSE otherwise. * @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 { if (fieldIndex < 0 || fieldIndex >= data.size()) throw new IllegalArgumentException("Index parameter is not within [0 - " + data.size() + ")"); @@ -325,7 +327,9 @@ public class StructureModifier { * @param field - the field to change. * @param isReadOnly - TRUE if the field should be read only, FALSE otherwise. * @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 { if (isReadOnly) FieldUtils.writeField((Object) field, "modifiers", field.getModifiers() | Modifier.FINAL, true); diff --git a/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java b/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java index 61ef12d1..ae02a806 100644 --- a/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java +++ b/src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java @@ -58,8 +58,6 @@ public abstract class CompiledStructureModifier extends StructureModifier