From 8bb2987e575bc0795e99889d5112a5a2305fb37c Mon Sep 17 00:00:00 2001 From: IzzelAliz Date: Sat, 1 Feb 2020 16:18:18 +0800 Subject: [PATCH] Fix compatibility with JDK 13 --- .../taboolib/module/db/local/LocalLoader.java | 3 +-- .../izzel/taboolib/module/event/EventNormal.java | 3 +-- .../izzel/taboolib/module/inject/THookLoader.java | 3 +-- .../izzel/taboolib/module/inject/TInjectAsm.java | 3 +-- .../taboolib/module/inject/TInjectCreator.java | 3 +-- src/main/scala/io/izzel/taboolib/util/Ref.java | 14 +++++++++++--- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/scala/io/izzel/taboolib/module/db/local/LocalLoader.java b/src/main/scala/io/izzel/taboolib/module/db/local/LocalLoader.java index 3410d31..42ae92b 100644 --- a/src/main/scala/io/izzel/taboolib/module/db/local/LocalLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/db/local/LocalLoader.java @@ -20,10 +20,9 @@ public class LocalLoader implements TabooLibLoader.Loader { if (annotation == null) { continue; } - Ref.forcedAccess(field); for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) { try { - field.set(instance, Local.get(plugin.getName()).get(annotation.value())); + Ref.putField(instance, field, Local.get(plugin.getName()).get(annotation.value())); } catch (Throwable t) { t.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/module/event/EventNormal.java b/src/main/scala/io/izzel/taboolib/module/event/EventNormal.java index 814217e..709f69b 100644 --- a/src/main/scala/io/izzel/taboolib/module/event/EventNormal.java +++ b/src/main/scala/io/izzel/taboolib/module/event/EventNormal.java @@ -32,8 +32,7 @@ public abstract class EventNormal extends Event { public T async(boolean value) { try { Field asyncField = Reflection.getField(Event.class, true, "async"); - Ref.forcedAccess(asyncField); - asyncField.setBoolean(this, value); + Ref.putField(this, asyncField, value); } catch (Throwable t) { t.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/module/inject/THookLoader.java b/src/main/scala/io/izzel/taboolib/module/inject/THookLoader.java index d617698..a97882e 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/THookLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/THookLoader.java @@ -27,10 +27,9 @@ public class THookLoader implements TabooLibLoader.Loader { if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) { THook hook = declaredField.getAnnotation(THook.class); if (Strings.nonEmpty(hook.plugin())) { - Ref.forcedAccess(declaredField); for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) { try { - declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin())); + Ref.putField(instance, declaredField, Bukkit.getPluginManager().getPlugin(hook.plugin())); } catch (Throwable t) { t.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java b/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java index c2b3e1d..48b93ab 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectAsm.java @@ -20,10 +20,9 @@ public class TInjectAsm implements TabooLibLoader.Loader { if (annotation == null || annotation.asm().isEmpty()) { continue; } - Ref.forcedAccess(declaredField); for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) { try { - declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance()); + Ref.putField(instance, declaredField, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance()); } catch (Throwable t) { t.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/module/inject/TInjectCreator.java b/src/main/scala/io/izzel/taboolib/module/inject/TInjectCreator.java index 5bdb0fa..00a5220 100644 --- a/src/main/scala/io/izzel/taboolib/module/inject/TInjectCreator.java +++ b/src/main/scala/io/izzel/taboolib/module/inject/TInjectCreator.java @@ -58,10 +58,9 @@ public class TInjectCreator implements TabooLibLoader.Loader { if (instance.isEmpty()) { continue; } - Ref.forcedAccess(declaredField); try { InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation); - declaredField.set(instance, instanceData.getInstance()); + Ref.putField(instance, declaredField, instanceData.getInstance()); instanceMap.put(new ClassData(loadClass, declaredField.getType()), instanceData); } catch (Throwable t) { TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage()); diff --git a/src/main/scala/io/izzel/taboolib/util/Ref.java b/src/main/scala/io/izzel/taboolib/util/Ref.java index ae61517..f2f83e4 100644 --- a/src/main/scala/io/izzel/taboolib/util/Ref.java +++ b/src/main/scala/io/izzel/taboolib/util/Ref.java @@ -32,7 +32,6 @@ 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 { @@ -41,7 +40,6 @@ 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"); } @@ -55,6 +53,17 @@ public class Ref { return LOOKUP; } + public static void putField(Object src, Field field, Object value) { + if (Modifier.isStatic(field.getModifiers())) { + Object base = getUnsafe().staticFieldBase(field); + long offset = getUnsafe().staticFieldOffset(field); + getUnsafe().putObject(base, offset, value); + } else { + long offset = getUnsafe().objectFieldOffset(field); + getUnsafe().putObject(src, offset, value); + } + } + public static List getDeclaredFields(Class clazz) { return getDeclaredFields(clazz, 0, true); } @@ -210,7 +219,6 @@ public class Ref { public static void forcedAccess(Field field) { try { field.setAccessible(true); - getUnsafe().putInt(field, modifiersOffset, field.getModifiers() & ~Modifier.FINAL); } catch (Throwable t) { t.printStackTrace(); }