From 23879f873a30bcbf05b21990abb0ac49ca74be4a Mon Sep 17 00:00:00 2001 From: R0bbyYT Date: Tue, 18 May 2021 13:14:30 +0200 Subject: [PATCH 1/2] Add java9+ support, improve the performance for Windows (maybe Linux too) --- .../classloading/RootClassLoader.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java b/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java index c358319..e15004d 100644 --- a/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java +++ b/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java @@ -51,6 +51,8 @@ */ public class RootClassLoader extends URLClassLoader implements CommonClassLoader { + private static final Logger LOGGER = LogManager.getLogger(RootClassLoader.class); + static { ClassLoader.registerAsParallelCapable(); } @@ -71,7 +73,7 @@ public class RootClassLoader extends URLClassLoader implements CommonClassLoader * @param urls The new class path of this class loader */ public RootClassLoader(URL[] urls) { - super(urls, null); + super(urls, getPlatformClassloader()); this.currentlyLoading = new HashSet<>(); this.plugins = new HashSet<>(); this.children = new ArrayList<>(); @@ -91,6 +93,26 @@ public RootClassLoader(URL[] urls) { excludeFromModification("net.flintmc.launcher."); } + /** + * Retrieves the classloader from the platform. + * + * @return The platform classloader. + */ + private static ClassLoader getPlatformClassloader() { + String javaVersionProperty = System.getProperty("java.version"); + + if (!javaVersionProperty.startsWith("1.")) { + try { + return (ClassLoader) ClassLoader.class + .getDeclaredMethod("getPlatformClassLoader") + .invoke(null); + } catch (Throwable ignored) { + LOGGER.warn("No platform classloader found: {}", javaVersionProperty); + } + } + return null; + } + public void addModifiedClass(String name, byte[] byteCode) { this.modifiedClasses.put(name, byteCode); } @@ -142,7 +164,7 @@ public void excludeFromModification(String... names) { */ @Override public Class findClass(String name) throws ClassNotFoundException { - return findClass(name, null); + return findClass(name, (ChildClassLoader) null); } /** @@ -274,7 +296,15 @@ public Class findClass(String name, ChildClassLoader preferredLoader) */ @Override public URL findResource(String name) { - return findResource(name, true); + return super.findResource(name); + // The findResource(String, boolean) method slows down the startup + // by 10-20 seconds on the Windows operating system. + // + // I think we still need a better solution for this, + // but since there are currently no launcher plugins that could redirect the resource, + // the implementation is fine. + // + // return findResource(name, true); } /** From 0a78610aff77ffe79f764256270bd3530b04e854 Mon Sep 17 00:00:00 2001 From: R0bbyYT Date: Tue, 18 May 2021 13:49:13 +0200 Subject: [PATCH 2/2] Add ToDo --- .../java/net/flintmc/launcher/classloading/RootClassLoader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java b/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java index e15004d..bf378e1 100644 --- a/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java +++ b/src/main/java/net/flintmc/launcher/classloading/RootClassLoader.java @@ -297,6 +297,7 @@ public Class findClass(String name, ChildClassLoader preferredLoader) @Override public URL findResource(String name) { return super.findResource(name); + // TODO: 5/18/2021 // The findResource(String, boolean) method slows down the startup // by 10-20 seconds on the Windows operating system. //