Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #57 #58

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/main/java/snw/kookbc/impl/launch/LaunchClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class LaunchClassLoader extends URLClassLoader implements MarkedClassLoad
private final Function<String, Package> packageProvider;

private static final MethodHandle GET_DEFINED_PACKAGE;
private static final MethodHandle GET_SYSTEM_RESOURCE;

private static final String[] RESERVED_NAMES = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5",
"COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
Expand All @@ -60,12 +61,15 @@ public class LaunchClassLoader extends URLClassLoader implements MarkedClassLoad
// Ignore this if you are working on Java 9 and later
// noinspection JavaReflectionMemberAccess
Method getDefinedPackageMethod = ClassLoader.class.getMethod("getDefinedPackage", String.class);
GET_DEFINED_PACKAGE = MethodHandles.lookup().unreflect(getDefinedPackageMethod);
MethodHandles.Lookup lookup = MethodHandles.lookup();
GET_DEFINED_PACKAGE = lookup.unreflect(getDefinedPackageMethod);
GET_SYSTEM_RESOURCE = lookup.unreflect(ClassLoader.class.getMethod("getSystemResource", String.class));
} catch (Throwable e) {
throw new Error("Unable to initialize LaunchClassLoader.getPackage0 environment.", e);
}
} else {
GET_DEFINED_PACKAGE = null;
GET_SYSTEM_RESOURCE = null;
}
}

Expand All @@ -80,6 +84,7 @@ public LaunchClassLoader(URL[] sources) {
addClassLoaderExclusion("org.apache.logging.");
addClassLoaderExclusion("org.apache.log4j.");
addClassLoaderExclusion("org.slf4j");
addClassLoaderExclusion("okhttp3.");
addClassLoaderExclusion("uk.org.lidalia.sysoutslf4j.");
addClassLoaderExclusion("org.spongepowered.asm.launch.");
addClassLoaderExclusion("ch.qos.logback");
Expand Down Expand Up @@ -227,6 +232,7 @@ public Class<?> findClass(final String name) throws ClassNotFoundException {
try {
final Class<?> clazz = ctxLoader.loadClass(name);
cachedClasses.put(name, clazz);
invalidClasses.remove(name);
return clazz;
} catch (ClassNotFoundException ignored) {
}
Expand Down Expand Up @@ -390,7 +396,7 @@ public byte[] getClassBytes(String name) throws IOException {
InputStream classStream = null;
try {
final String resourcePath = name.replace('.', '/').concat(".class");
final URL classResource = findResource(resourcePath);
URL classResource = findResource0(resourcePath);

if (classResource == null) {
if (DEBUG)
Expand All @@ -410,6 +416,21 @@ public byte[] getClassBytes(String name) throws IOException {
}
}

private URL findResource0(String name) {
URL resource = super.findResource(name);
if (resource != null) {
return resource;
}
if (GET_SYSTEM_RESOURCE != null) {
try {
return (URL) GET_SYSTEM_RESOURCE.invoke(name);
} catch (Throwable e) {
e.printStackTrace();
}
}
return null;
}

private static void closeSilently(Closeable closeable) {
if (closeable != null) {
try {
Expand Down