Skip to content

Commit

Permalink
Document the side-effects of using the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jan 12, 2024
1 parent ad8d7d7 commit 9c36d03
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/cpw/mods/cl/ModuleClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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 @@ -71,6 +72,20 @@ public ModuleClassLoader(final String name, final Configuration configuration, f
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) {
super(name, parentLoader);
this.fallbackClassLoader = Objects.requireNonNullElse(parentLoader, ClassLoader.getPlatformClassLoader());
Expand Down

0 comments on commit 9c36d03

Please sign in to comment.