Skip to content

Commit baecaf4

Browse files
authored
Remove unsafe reflection trying to access the modifiers field (#1517)
1 parent 151d4a2 commit baecaf4

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/main/java/com/comphenix/protocol/reflect/FieldUtils.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,12 @@ public static void writeStaticField(Class cls, String fieldName, Object value,
395395
writeStaticField(field, value);
396396
}
397397

398+
/**
399+
* @deprecated Use {@link #writeStaticField(Class, String, Object, boolean)} instead.
400+
*/
401+
@Deprecated
398402
public static void writeStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess) throws Exception {
399-
Field field = getField(clazz, fieldName, forceAccess);
400-
if (field == null) {
401-
throw new IllegalArgumentException("Cannot locate field " + fieldName + " in " + clazz);
402-
}
403-
404-
field.setAccessible(true);
405-
406-
Field modifiersField = Field.class.getDeclaredField("modifiers");
407-
modifiersField.setAccessible(true);
408-
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
409-
410-
field.setAccessible(true);
411-
field.set(null, value);
403+
writeStaticField(clazz, fieldName, value, forceAccess);
412404
}
413405

414406
/**

src/main/java/com/comphenix/protocol/reflect/StructureModifier.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ public boolean isPublic(int fieldIndex) {
308308
* @param fieldIndex - index of the field.
309309
* @param value - TRUE if this field should be read only, FALSE otherwise.
310310
* @throws FieldAccessException If we cannot modify the read-only status.
311+
* @deprecated In recent java versions (starting at 9) the modifier field is secured and will not be writeable.
311312
*/
313+
@Deprecated
312314
public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessException {
313315
if (fieldIndex < 0 || fieldIndex >= data.size())
314316
throw new IllegalArgumentException("Index parameter is not within [0 - " + data.size() + ")");
@@ -325,7 +327,9 @@ public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessExcepti
325327
* @param field - the field to change.
326328
* @param isReadOnly - TRUE if the field should be read only, FALSE otherwise.
327329
* @throws IllegalAccessException If an error occured.
330+
* @deprecated In recent java versions (starting at 9) the modifier field is secured and will not be writeable.
328331
*/
332+
@Deprecated
329333
protected static void setFinalState(Field field, boolean isReadOnly) throws IllegalAccessException {
330334
if (isReadOnly)
331335
FieldUtils.writeField((Object) field, "modifiers", field.getModifiers() | Modifier.FINAL, true);

src/main/java/com/comphenix/protocol/reflect/compiler/CompiledStructureModifier.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ public void setReadOnly(int fieldIndex, boolean value) throws FieldAccessExcepti
5858
throw new IllegalStateException("Cannot make compiled field " + fieldIndex + " read only.");
5959
}
6060
}
61-
62-
super.setReadOnly(fieldIndex, value);
6361
}
6462

6563
// Speed up the default writer

0 commit comments

Comments
 (0)