Skip to content

Commit

Permalink
Fix reflection issues with Java 16
Browse files Browse the repository at this point in the history
  • Loading branch information
ChachyDev committed Aug 4, 2021
1 parent ddbf00d commit f9a346d
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/main/java/org/bookmc/loader/api/ModResolver.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 10 additions & 8 deletions src/main/java/org/bookmc/loader/impl/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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.
*
* <p>
* 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<ModVessel> vessels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +59,7 @@ public boolean isResolvable() {
}

@Override
public void addToClasspath(ClassLoaderURLAppender appender) {
public void addToClasspath(IQuiltClassLoader classLoader) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,7 +23,7 @@ public boolean isResolvable() {
}

@Override
public void addToClasspath(ClassLoaderURLAppender appender) {
public void addToClasspath(IQuiltClassLoader classLoader) {

}
}

0 comments on commit f9a346d

Please sign in to comment.