From 3ccdfb7abb2ae07052187ed0289f86c0313c2e7f Mon Sep 17 00:00:00 2001 From: IzzelAliz Date: Sat, 1 Feb 2020 15:33:16 +0800 Subject: [PATCH] Remove illegal reflection access warnings on Field#modifiers --- src/main/scala/io/izzel/taboolib/util/Ref.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/scala/io/izzel/taboolib/util/Ref.java b/src/main/scala/io/izzel/taboolib/util/Ref.java index 9ef6676..ae61517 100644 --- a/src/main/scala/io/izzel/taboolib/util/Ref.java +++ b/src/main/scala/io/izzel/taboolib/util/Ref.java @@ -32,6 +32,7 @@ public class Ref { public static final int ACC_SYNTHETIC = 0x1000; private static final Unsafe UNSAFE; private static final MethodHandles.Lookup LOOKUP; + private static final long modifiersOffset; static { try { @@ -40,6 +41,7 @@ public class Ref { Object lookupBase = UNSAFE.staticFieldBase(lookupField); long lookupOffset = UNSAFE.staticFieldOffset(lookupField); LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(lookupBase, lookupOffset); + modifiersOffset = UNSAFE.objectFieldOffset(Field.class.getDeclaredField("modifiers")); } catch (Throwable t) { throw new IllegalStateException("Unsafe not found"); } @@ -208,9 +210,7 @@ public class Ref { public static void forcedAccess(Field field) { try { field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + getUnsafe().putInt(field, modifiersOffset, field.getModifiers() & ~Modifier.FINAL); } catch (Throwable t) { t.printStackTrace(); }