Add dependency jdk 11 compat

This commit is contained in:
IzzelAliz 2020-02-01 15:24:08 +08:00
parent 69b98c180f
commit e50bede493
3 changed files with 28 additions and 11 deletions

View File

@ -6,6 +6,8 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -19,11 +21,11 @@ public class TDependencyLoader {
try { try {
ClassLoader loader = plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(); ClassLoader loader = plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader();
Field ucpField = loader.getClass().getDeclaredField("ucp"); Field ucpField = loader.getClass().getDeclaredField("ucp");
long ucpOffset = Ref.UNSAFE.objectFieldOffset(ucpField); long ucpOffset = Ref.getUnsafe().objectFieldOffset(ucpField);
Object ucp = Ref.UNSAFE.getObject(loader, ucpOffset); Object ucp = Ref.getUnsafe().getObject(loader, ucpOffset);
Method addURL = ucp.getClass().getMethod("addURL", URL.class); MethodHandle methodHandle = Ref.lookup().findVirtual(ucp.getClass(), "addURL", MethodType.methodType(void.class, java.net.URL.class));
addURL.invoke(ucp, url); methodHandle.invoke(ucp, url);
} catch (Exception e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -65,7 +65,7 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
method.invoke(instance); method.invoke(instance);
} catch (Throwable t) { } catch (Throwable t) {
try { try {
method.invoke(Ref.UNSAFE.allocateInstance(pluginClass)); method.invoke(Ref.getUnsafe().allocateInstance(pluginClass));
} catch (Throwable t2) { } catch (Throwable t2) {
t.printStackTrace(); t.printStackTrace();
t2.printStackTrace(); t2.printStackTrace();

View File

@ -12,6 +12,7 @@ import sun.misc.Unsafe;
import sun.reflect.Reflection; import sun.reflect.Reflection;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -19,6 +20,7 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@SuppressWarnings("restriction")
@ThreadSafe @ThreadSafe
public class Ref { public class Ref {
@ -28,16 +30,29 @@ public class Ref {
public static final int ACC_BRIDGE = 0x0040; public static final int ACC_BRIDGE = 0x0040;
public static final int ACC_SYNTHETIC = 0x1000; public static final int ACC_SYNTHETIC = 0x1000;
public static final Unsafe UNSAFE = getUnsafe(); private static final Unsafe UNSAFE;
private static final MethodHandles.Lookup LOOKUP;
static Unsafe getUnsafe() { static {
try { try {
return (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe"); UNSAFE = (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe");
Field lookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
Object lookupBase = UNSAFE.staticFieldBase(lookupField);
long lookupOffset = UNSAFE.staticFieldOffset(lookupField);
LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(lookupBase, lookupOffset);
} catch (Throwable t) { } catch (Throwable t) {
throw new IllegalStateException("Unsafe not found"); throw new IllegalStateException("Unsafe not found");
} }
} }
public static Unsafe getUnsafe() {
return UNSAFE;
}
public static MethodHandles.Lookup lookup() {
return LOOKUP;
}
public static List<Field> getDeclaredFields(Class<?> clazz) { public static List<Field> getDeclaredFields(Class<?> clazz) {
return getDeclaredFields(clazz, 0, true); return getDeclaredFields(clazz, 0, true);
} }
@ -73,7 +88,7 @@ public class Ref {
} catch (Exception | Error e) { } catch (Exception | Error e) {
try { try {
List<Field> list = Arrays.stream(clazz.getDeclaredFields()) List<Field> list = Arrays.stream(clazz.getDeclaredFields())
.filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList()); .filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList());
cachedFields.putIfAbsent(clazz.getName(), list); cachedFields.putIfAbsent(clazz.getName(), list);
return list; return list;
} catch (Error err) { } catch (Error err) {
@ -117,7 +132,7 @@ public class Ref {
} catch (Exception | Error e) { } catch (Exception | Error e) {
try { try {
List<Method> list = Arrays.stream(clazz.getDeclaredMethods()) List<Method> list = Arrays.stream(clazz.getDeclaredMethods())
.filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList()); .filter(field -> (field.getModifiers() & excludeModifiers) == 0).collect(Collectors.toList());
cacheMethods.putIfAbsent(clazz.getName(), list); cacheMethods.putIfAbsent(clazz.getName(), list);
return list; return list;
} catch (Error err) { } catch (Error err) {