152 lines
No EOL
4.5 KiB
Java
152 lines
No EOL
4.5 KiB
Java
package lol.pyr.znpcsplus.libraries;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.net.URL;
|
|
import java.net.URLClassLoader;
|
|
import java.util.Collection;
|
|
|
|
/**
|
|
* Provides access to {@link URLClassLoader}#addURL.
|
|
* From https://github.com/lucko/helper/blob/master/helper/src/main/java/me/lucko/helper/maven/URLClassLoaderAccess.java
|
|
*/
|
|
public abstract class UrlClassLoaderAccess {
|
|
|
|
/**
|
|
* Creates a {@link UrlClassLoaderAccess} for the given class loader.
|
|
*
|
|
* @param classLoader the class loader
|
|
* @return the access object
|
|
*/
|
|
static UrlClassLoaderAccess create(URLClassLoader classLoader) {
|
|
if (Reflection.isSupported()) {
|
|
return new Reflection(classLoader);
|
|
} else if (Unsafe.isSupported()) {
|
|
return new Unsafe(classLoader);
|
|
} else {
|
|
return Noop.INSTANCE;
|
|
}
|
|
}
|
|
|
|
private final URLClassLoader classLoader;
|
|
|
|
protected UrlClassLoaderAccess(URLClassLoader classLoader) {
|
|
this.classLoader = classLoader;
|
|
}
|
|
|
|
|
|
/**
|
|
* Adds the given URL to the class loader.
|
|
*
|
|
* @param url the URL to add
|
|
*/
|
|
public abstract void addURL(@Nonnull URL url);
|
|
|
|
/**
|
|
* Accesses using reflection, not supported on Java 9+.
|
|
*/
|
|
private static class Reflection extends UrlClassLoaderAccess {
|
|
private static final Method ADD_URL_METHOD;
|
|
|
|
static {
|
|
Method addUrlMethod;
|
|
try {
|
|
addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
|
addUrlMethod.setAccessible(true);
|
|
} catch (Exception e) {
|
|
addUrlMethod = null;
|
|
}
|
|
ADD_URL_METHOD = addUrlMethod;
|
|
}
|
|
|
|
private static boolean isSupported() {
|
|
return ADD_URL_METHOD != null;
|
|
}
|
|
|
|
Reflection(URLClassLoader classLoader) {
|
|
super(classLoader);
|
|
}
|
|
|
|
@Override
|
|
public void addURL(@Nonnull URL url) {
|
|
try {
|
|
ADD_URL_METHOD.invoke(super.classLoader, url);
|
|
} catch (ReflectiveOperationException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Accesses using sun.misc.Unsafe, supported on Java 9+.
|
|
*
|
|
* @author Vaishnav Anil (https://github.com/slimjar/slimjar)
|
|
*/
|
|
private static class Unsafe extends UrlClassLoaderAccess {
|
|
private static final sun.misc.Unsafe UNSAFE;
|
|
|
|
static {
|
|
sun.misc.Unsafe unsafe;
|
|
try {
|
|
Field unsafeField = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
|
unsafeField.setAccessible(true);
|
|
unsafe = (sun.misc.Unsafe) unsafeField.get(null);
|
|
} catch (Throwable t) {
|
|
unsafe = null;
|
|
}
|
|
UNSAFE = unsafe;
|
|
}
|
|
|
|
private static boolean isSupported() {
|
|
return UNSAFE != null;
|
|
}
|
|
|
|
private final Collection<URL> unopenedURLs;
|
|
private final Collection<URL> pathURLs;
|
|
|
|
@SuppressWarnings("unchecked")
|
|
Unsafe(URLClassLoader classLoader) {
|
|
super(classLoader);
|
|
|
|
Collection<URL> unopenedURLs;
|
|
Collection<URL> pathURLs;
|
|
try {
|
|
Object ucp = fetchField(URLClassLoader.class, classLoader, "ucp");
|
|
unopenedURLs = (Collection<URL>) fetchField(ucp.getClass(), ucp, "unopenedUrls");
|
|
pathURLs = (Collection<URL>) fetchField(ucp.getClass(), ucp, "path");
|
|
} catch (Throwable e) {
|
|
unopenedURLs = null;
|
|
pathURLs = null;
|
|
}
|
|
this.unopenedURLs = unopenedURLs;
|
|
this.pathURLs = pathURLs;
|
|
}
|
|
|
|
private static Object fetchField(final Class<?> clazz, final Object object, final String name) throws NoSuchFieldException {
|
|
Field field = clazz.getDeclaredField(name);
|
|
long offset = UNSAFE.objectFieldOffset(field);
|
|
return UNSAFE.getObject(object, offset);
|
|
}
|
|
|
|
@Override
|
|
public void addURL(@Nonnull URL url) {
|
|
this.unopenedURLs.add(url);
|
|
this.pathURLs.add(url);
|
|
}
|
|
}
|
|
|
|
private static class Noop extends UrlClassLoaderAccess {
|
|
private static final Noop INSTANCE = new Noop();
|
|
|
|
private Noop() {
|
|
super(null);
|
|
}
|
|
|
|
@Override
|
|
public void addURL(@Nonnull URL url) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
}
|
|
|
|
} |