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

Allow ModuleClassLoader to optionally delegate class-loading to parent #60

Merged
merged 3 commits into from
Jan 13, 2024
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/cpw/mods/cl/ModuleClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cpw.mods.cl;

import cpw.mods.util.LambdaExceptionUtils;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -64,10 +66,29 @@ private static void bindToLayer(ModuleClassLoader classLoader, ModuleLayer layer
private final Map<String, JarModuleFinder.JarModuleReference> resolvedRoots;
private final Map<String, ResolvedModule> packageLookup;
private final Map<String, ClassLoader> parentLoaders;
private ClassLoader fallbackClassLoader = ClassLoader.getPlatformClassLoader();
private ClassLoader fallbackClassLoader;

public ModuleClassLoader(final String name, final Configuration configuration, final List<ModuleLayer> parentLayers) {
super(name, null);
this(name, configuration, parentLayers, null);
}

/**
* This constructor allows setting the parent {@linkplain ClassLoader classloader}. Use this with caution since
* it will allow loading of classes from the classpath directly if the {@linkplain ClassLoader#getSystemClassLoader() system classloader}
* is reachable from the given parent classloader.
* <p>
* Generally classes that are in packages covered by reachable modules are preferably loaded from these modules.
* If a class-path entry is not shadowed by a module, specifying a parent class-loader may lead to those
* classes now being loadable instead of throwing a {@link ClassNotFoundException}.
* <p>
* This relaxed classloader isolation is used in unit-testing, where testing libraries are loaded on the
* system class-loader outside our control (by the Gradle test runner). We must not reload these classes
* inside the module layers again, otherwise tests throw incompatible exceptions or may not be found at all.
*/
@VisibleForTesting
public ModuleClassLoader(final String name, final Configuration configuration, final List<ModuleLayer> parentLayers, @Nullable ClassLoader parentLoader) {
shartte marked this conversation as resolved.
Show resolved Hide resolved
super(name, parentLoader);
this.fallbackClassLoader = Objects.requireNonNullElse(parentLoader, ClassLoader.getPlatformClassLoader());
this.configuration = configuration;
this.packageLookup = new HashMap<>();
this.resolvedRoots = configuration.modules().stream()
Expand Down