Fix compatibility with JDK 13

This commit is contained in:
IzzelAliz 2020-02-01 16:18:18 +08:00
parent 3ccdfb7abb
commit 8bb2987e57
6 changed files with 16 additions and 13 deletions

View File

@ -20,10 +20,9 @@ public class LocalLoader implements TabooLibLoader.Loader {
if (annotation == null) { if (annotation == null) {
continue; continue;
} }
Ref.forcedAccess(field);
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) { for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
try { 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) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -32,8 +32,7 @@ public abstract class EventNormal<T extends EventNormal> extends Event {
public T async(boolean value) { public T async(boolean value) {
try { try {
Field asyncField = Reflection.getField(Event.class, true, "async"); Field asyncField = Reflection.getField(Event.class, true, "async");
Ref.forcedAccess(asyncField); Ref.putField(this, asyncField, value);
asyncField.setBoolean(this, value);
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -27,10 +27,9 @@ public class THookLoader implements TabooLibLoader.Loader {
if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) { if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) {
THook hook = declaredField.getAnnotation(THook.class); THook hook = declaredField.getAnnotation(THook.class);
if (Strings.nonEmpty(hook.plugin())) { if (Strings.nonEmpty(hook.plugin())) {
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) { for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try { try {
declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin())); Ref.putField(instance, declaredField, Bukkit.getPluginManager().getPlugin(hook.plugin()));
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -20,10 +20,9 @@ public class TInjectAsm implements TabooLibLoader.Loader {
if (annotation == null || annotation.asm().isEmpty()) { if (annotation == null || annotation.asm().isEmpty()) {
continue; continue;
} }
Ref.forcedAccess(declaredField);
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) { for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
try { 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) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }

View File

@ -58,10 +58,9 @@ public class TInjectCreator implements TabooLibLoader.Loader {
if (instance.isEmpty()) { if (instance.isEmpty()) {
continue; continue;
} }
Ref.forcedAccess(declaredField);
try { try {
InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation); 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); instanceMap.put(new ClassData(loadClass, declaredField.getType()), instanceData);
} catch (Throwable t) { } catch (Throwable t) {
TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage()); TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage());

View File

@ -32,7 +32,6 @@ public class Ref {
public static final int ACC_SYNTHETIC = 0x1000; public static final int ACC_SYNTHETIC = 0x1000;
private static final Unsafe UNSAFE; private static final Unsafe UNSAFE;
private static final MethodHandles.Lookup LOOKUP; private static final MethodHandles.Lookup LOOKUP;
private static final long modifiersOffset;
static { static {
try { try {
@ -41,7 +40,6 @@ public class Ref {
Object lookupBase = UNSAFE.staticFieldBase(lookupField); Object lookupBase = UNSAFE.staticFieldBase(lookupField);
long lookupOffset = UNSAFE.staticFieldOffset(lookupField); long lookupOffset = UNSAFE.staticFieldOffset(lookupField);
LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(lookupBase, lookupOffset); LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(lookupBase, lookupOffset);
modifiersOffset = UNSAFE.objectFieldOffset(Field.class.getDeclaredField("modifiers"));
} catch (Throwable t) { } catch (Throwable t) {
throw new IllegalStateException("Unsafe not found"); throw new IllegalStateException("Unsafe not found");
} }
@ -55,6 +53,17 @@ public class Ref {
return LOOKUP; 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<Field> getDeclaredFields(Class<?> clazz) { public static List<Field> getDeclaredFields(Class<?> clazz) {
return getDeclaredFields(clazz, 0, true); return getDeclaredFields(clazz, 0, true);
} }
@ -210,7 +219,6 @@ public class Ref {
public static void forcedAccess(Field field) { public static void forcedAccess(Field field) {
try { try {
field.setAccessible(true); field.setAccessible(true);
getUnsafe().putInt(field, modifiersOffset, field.getModifiers() & ~Modifier.FINAL);
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }