From f9a346dce5b8a6a04eb396578eefb433e183e0f6 Mon Sep 17 00:00:00 2001 From: ChachyDev Date: Wed, 4 Aug 2021 10:46:46 +0100 Subject: [PATCH] Fix reflection issues with Java 16 --- .../org/bookmc/loader/api/ModResolver.java | 4 ---- .../loader/api/candidate/ModCandidate.java | 5 +++-- .../api/classloader/IQuiltClassLoader.java | 3 +++ .../loader/api/classloader/ModClassLoader.java | 5 +++++ .../loader/api/compat/CompatiblityLayer.java | 3 ++- .../java/org/bookmc/loader/impl/Loader.java | 18 ++++++++++-------- .../impl/candidate/DirectoryModCandidate.java | 5 +++-- .../LocalClassLoaderModCandidate.java | 3 ++- .../loader/impl/candidate/ZipModCandidate.java | 5 +++-- .../launch/transform/QuiltClassLoader.java | 5 +++++ .../vessel/dummy/candidate/DummyCandidate.java | 3 ++- 11 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/bookmc/loader/api/ModResolver.java b/src/main/java/org/bookmc/loader/api/ModResolver.java index 4b4e9e7..1ab5179 100644 --- a/src/main/java/org/bookmc/loader/api/ModResolver.java +++ b/src/main/java/org/bookmc/loader/api/ModResolver.java @@ -1,12 +1,8 @@ package org.bookmc.loader.api; -import com.google.gson.JsonParser; - import java.io.File; public interface ModResolver { - JsonParser parser = new JsonParser(); - /** * This non-default provides the discoverer with an array of files (if wanted). * It should after either resolve those files by registering them as candidates diff --git a/src/main/java/org/bookmc/loader/api/candidate/ModCandidate.java b/src/main/java/org/bookmc/loader/api/candidate/ModCandidate.java index aa6c4c8..d522eb1 100644 --- a/src/main/java/org/bookmc/loader/api/candidate/ModCandidate.java +++ b/src/main/java/org/bookmc/loader/api/candidate/ModCandidate.java @@ -1,6 +1,7 @@ package org.bookmc.loader.api.candidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.vessel.ModVessel; import org.bookmc.loader.impl.candidate.ZipModCandidate; import org.bookmc.loader.impl.resolve.ClasspathModResolver; @@ -45,7 +46,7 @@ public interface ModCandidate { * such as {@link DevelopmentModResolver} or {@link ClasspathModResolver} * this method may not be needed at all as the mod candidates should already be present on the classpath or something is going wrong! * - * @param appender A custom class to abstract appending to the current URLClassLoader. (Read more about this at {@link org.bookmc.loader.impl.Loader#sortClassLoaders(List)} + * @param classLoader A custom class to abstract appending to the current URLClassLoader. (Read more about this at {@link org.bookmc.loader.impl.Loader#sortClassLoaders(List)} */ - void addToClasspath(ClassLoaderURLAppender appender); + void addToClasspath(IQuiltClassLoader classLoader); } diff --git a/src/main/java/org/bookmc/loader/api/classloader/IQuiltClassLoader.java b/src/main/java/org/bookmc/loader/api/classloader/IQuiltClassLoader.java index 042ab52..510c7a8 100644 --- a/src/main/java/org/bookmc/loader/api/classloader/IQuiltClassLoader.java +++ b/src/main/java/org/bookmc/loader/api/classloader/IQuiltClassLoader.java @@ -1,9 +1,12 @@ package org.bookmc.loader.api.classloader; +import java.net.URL; import java.net.URLClassLoader; public interface IQuiltClassLoader { byte[] getClassBytes(String name, boolean transform); URLClassLoader getClassLoader(); + + void addURL(URL url); } diff --git a/src/main/java/org/bookmc/loader/api/classloader/ModClassLoader.java b/src/main/java/org/bookmc/loader/api/classloader/ModClassLoader.java index dffd606..bd8a3e6 100644 --- a/src/main/java/org/bookmc/loader/api/classloader/ModClassLoader.java +++ b/src/main/java/org/bookmc/loader/api/classloader/ModClassLoader.java @@ -50,4 +50,9 @@ public byte[] getClassBytes(String name, boolean transform) { public URLClassLoader getClassLoader() { return this; } + + @Override + public void addURL(URL url) { + super.addURL(url); + } } diff --git a/src/main/java/org/bookmc/loader/api/compat/CompatiblityLayer.java b/src/main/java/org/bookmc/loader/api/compat/CompatiblityLayer.java index d772b7f..07af953 100644 --- a/src/main/java/org/bookmc/loader/api/compat/CompatiblityLayer.java +++ b/src/main/java/org/bookmc/loader/api/compat/CompatiblityLayer.java @@ -1,7 +1,8 @@ package org.bookmc.loader.api.compat; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; public interface CompatiblityLayer { - void init(ClassLoaderURLAppender classLoader); + void init(IQuiltClassLoader classLoader); } diff --git a/src/main/java/org/bookmc/loader/impl/Loader.java b/src/main/java/org/bookmc/loader/impl/Loader.java index e4f0b58..78bf50c 100644 --- a/src/main/java/org/bookmc/loader/impl/Loader.java +++ b/src/main/java/org/bookmc/loader/impl/Loader.java @@ -5,6 +5,7 @@ import org.bookmc.loader.api.ModResolver; import org.bookmc.loader.api.candidate.ModCandidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.classloader.ModClassLoader; import org.bookmc.loader.api.compat.CompatiblityLayer; import org.bookmc.loader.api.exception.IllegalDependencyException; @@ -28,7 +29,6 @@ import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLClassLoader; import java.util.*; public class Loader { @@ -143,12 +143,13 @@ public static void loadCompatibilityLayers(QuiltClassLoader classLoader) { } } - public static void loadCompatibilityLayer(ModVessel vessel, URLClassLoader classLoader) { + public static void loadCompatibilityLayer(ModVessel vessel, IQuiltClassLoader classLoader) { Entrypoint[] entrypoints = vessel.getEntrypoints(); for (Entrypoint entrypoint : entrypoints) { try { - Class compatClass = classLoader.loadClass(CompatiblityLayer.class.getName()); - Class clazz = Class.forName(entrypoint.getOwner(), false, classLoader); + Class compatClass = classLoader.getClassLoader() + .loadClass(CompatiblityLayer.class.getName()); + Class clazz = Class.forName(entrypoint.getOwner(), false, classLoader.getClassLoader()); if (clazz.isAssignableFrom(compatClass)) { if (vessel.getDependsOn().length != 0) { @@ -157,7 +158,7 @@ public static void loadCompatibilityLayer(ModVessel vessel, URLClassLoader class loaded.add(vessel); // Trick BookModLoader#load to believe we have "loaded" our "mod". CompatiblityLayer layer = (CompatiblityLayer) clazz.newInstance(); - layer.init(new ClassLoaderURLAppender(classLoader)); + layer.init(classLoader); } } catch (ClassCastException e) { throw new IllegalStateException("The entrypoint (" + entrypoint + ") does not implement CompatibilityLayer"); @@ -189,7 +190,7 @@ public static void discoverAndLoad(File modsDirectory, Environment environment) loadCandidates(); for (ModVessel vessel : Loader.getModVessels()) { - Loader.loadCompatibilityLayer(vessel, vessel.getAbstractedClassLoader().getClassLoader()); + Loader.loadCompatibilityLayer(vessel, vessel.getAbstractedClassLoader()); Loader.loadMixin(vessel, environment); } } @@ -352,7 +353,7 @@ public static void loadCandidates() { sortClassLoaders(Loader.getModVessels()); for (ModCandidate candidate : Loader.getCandidates()) { for (ModVessel vessel : candidate.getVessels()) { - candidate.addToClasspath(new ClassLoaderURLAppender(vessel.getAbstractedClassLoader().getClassLoader())); + candidate.addToClasspath(vessel.getAbstractedClassLoader()); } } } @@ -372,8 +373,9 @@ public static boolean isModLoaded(String id) { * It recursively calls the method {@link Loader#sortClassLoader(ModVessel)} * to check if it has any dependencies and if it does add the dependencies and itself to the classpath * if not stay on it's own classpath. - * + *

* This was quite mentally exhausting to plan out how to make :) + * * @param vessels The vessels to have their classloaders sorted. */ public static void sortClassLoaders(List vessels) { diff --git a/src/main/java/org/bookmc/loader/impl/candidate/DirectoryModCandidate.java b/src/main/java/org/bookmc/loader/impl/candidate/DirectoryModCandidate.java index 15422dc..31a61cd 100644 --- a/src/main/java/org/bookmc/loader/impl/candidate/DirectoryModCandidate.java +++ b/src/main/java/org/bookmc/loader/impl/candidate/DirectoryModCandidate.java @@ -6,6 +6,7 @@ import com.google.gson.JsonParser; import org.bookmc.loader.api.candidate.ModCandidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.vessel.ModVessel; import org.bookmc.loader.impl.launch.Launcher; import org.bookmc.loader.impl.vessel.JsonModVessel; @@ -71,9 +72,9 @@ public boolean isResolvable() { } @Override - public void addToClasspath(ClassLoaderURLAppender appender) { + public void addToClasspath(IQuiltClassLoader classLoader) { try { - appender.add(file.toURI().toURL()); + classLoader.addURL(file.toURI().toURL()); } catch (MalformedURLException e) { e.printStackTrace(); } diff --git a/src/main/java/org/bookmc/loader/impl/candidate/LocalClassLoaderModCandidate.java b/src/main/java/org/bookmc/loader/impl/candidate/LocalClassLoaderModCandidate.java index 422003d..180cb0f 100644 --- a/src/main/java/org/bookmc/loader/impl/candidate/LocalClassLoaderModCandidate.java +++ b/src/main/java/org/bookmc/loader/impl/candidate/LocalClassLoaderModCandidate.java @@ -6,6 +6,7 @@ import com.google.gson.JsonParser; import org.bookmc.loader.api.candidate.ModCandidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.vessel.ModVessel; import org.bookmc.loader.impl.launch.Launcher; import org.bookmc.loader.impl.vessel.JsonModVessel; @@ -58,7 +59,7 @@ public boolean isResolvable() { } @Override - public void addToClasspath(ClassLoaderURLAppender appender) { + public void addToClasspath(IQuiltClassLoader classLoader) { } } diff --git a/src/main/java/org/bookmc/loader/impl/candidate/ZipModCandidate.java b/src/main/java/org/bookmc/loader/impl/candidate/ZipModCandidate.java index 7441671..09321f6 100644 --- a/src/main/java/org/bookmc/loader/impl/candidate/ZipModCandidate.java +++ b/src/main/java/org/bookmc/loader/impl/candidate/ZipModCandidate.java @@ -6,6 +6,7 @@ import com.google.gson.JsonParser; import org.bookmc.loader.api.candidate.ModCandidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.vessel.ModVessel; import org.bookmc.loader.impl.vessel.JsonModVessel; import org.bookmc.loader.shared.Constants; @@ -79,9 +80,9 @@ public boolean isResolvable() { } @Override - public void addToClasspath(ClassLoaderURLAppender appender) { + public void addToClasspath(IQuiltClassLoader classLoader) { try { - appender.add(file.toURI().toURL()); + classLoader.addURL(file.toURI().toURL()); } catch (MalformedURLException e) { e.printStackTrace(); } diff --git a/src/main/java/org/bookmc/loader/impl/launch/transform/QuiltClassLoader.java b/src/main/java/org/bookmc/loader/impl/launch/transform/QuiltClassLoader.java index ff3285f..6a4c37f 100644 --- a/src/main/java/org/bookmc/loader/impl/launch/transform/QuiltClassLoader.java +++ b/src/main/java/org/bookmc/loader/impl/launch/transform/QuiltClassLoader.java @@ -112,6 +112,11 @@ public URLClassLoader getClassLoader() { return this; } + @Override + public void addURL(URL url) { + super.addURL(url); + } + public ClassNode getClassNode(String name) { byte[] bytes = getClassBytes(name, true); ClassReader reader = new ClassReader(bytes); diff --git a/src/main/java/org/bookmc/loader/impl/vessel/dummy/candidate/DummyCandidate.java b/src/main/java/org/bookmc/loader/impl/vessel/dummy/candidate/DummyCandidate.java index 6b46adb..5885a46 100644 --- a/src/main/java/org/bookmc/loader/impl/vessel/dummy/candidate/DummyCandidate.java +++ b/src/main/java/org/bookmc/loader/impl/vessel/dummy/candidate/DummyCandidate.java @@ -2,6 +2,7 @@ import org.bookmc.loader.api.candidate.ModCandidate; import org.bookmc.loader.api.classloader.ClassLoaderURLAppender; +import org.bookmc.loader.api.classloader.IQuiltClassLoader; import org.bookmc.loader.api.vessel.ModVessel; public class DummyCandidate implements ModCandidate { @@ -22,7 +23,7 @@ public boolean isResolvable() { } @Override - public void addToClasspath(ClassLoaderURLAppender appender) { + public void addToClasspath(IQuiltClassLoader classLoader) { } }