Add dependency jdk 11 compat

This commit is contained in:
IzzelAliz 2020-02-01 15:05:01 +08:00
parent c070f08071
commit 80208eef6e
2 changed files with 17 additions and 5 deletions

View File

@ -1,10 +1,12 @@
package io.izzel.taboolib.module.dependency; package io.izzel.taboolib.module.dependency;
import io.izzel.taboolib.common.plugin.InternalPlugin; import io.izzel.taboolib.common.plugin.InternalPlugin;
import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit; 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.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -13,11 +15,22 @@ import java.net.URLClassLoader;
public class TDependencyLoader { public class TDependencyLoader {
private static final long ucpOffset;
static {
try {
Field ucp = Bukkit.class.getClassLoader().getClass().getDeclaredField("ucp");
ucpOffset = Ref.UNSAFE.objectFieldOffset(ucp);
} catch (Exception e) {
throw new IllegalStateException();
}
}
public static synchronized void addToPath(Plugin plugin, URL url) { public static synchronized void addToPath(Plugin plugin, URL url) {
try { try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); Object ucp = Ref.UNSAFE.getObject(plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(), ucpOffset);
method.setAccessible(true); Method addURL = ucp.getClass().getMethod("addURL", URL.class);
method.invoke(plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(), url); addURL.invoke(ucp, url);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -34,9 +34,8 @@ public class Ref {
try { try {
return (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe"); return (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe");
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); throw new IllegalStateException("Unsafe not found");
} }
return null;
} }
public static List<Field> getDeclaredFields(Class<?> clazz) { public static List<Field> getDeclaredFields(Class<?> clazz) {