Package net.neoforged.fml.classloading
Class ModuleClassLoader
java.lang.Object
java.lang.ClassLoader
net.neoforged.fml.classloading.ModuleClassLoader
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
TransformingClassLoader
This classloader implements child-first classloading for any module that is defined
locally.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classCaches the module reader for a module, including its protection domain. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final Configurationprivate ClassLoaderprivate static final MethodHandleprivate final Map<String, ModuleClassLoader.ModuleInfo> private final Map<String, ModuleClassLoader.ModuleInfo> private final Map<String, ClassLoader> -
Constructor Summary
ConstructorsConstructorDescriptionModuleClassLoader(String name, Configuration configuration, List<ModuleLayer> parentLayers) ModuleClassLoader(String name, Configuration configuration, List<ModuleLayer> parentLayers, @Nullable ClassLoader parentLoader) This constructor allows setting the parent classloader. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidbindToLayer(ModuleClassLoader classLoader, ModuleLayer layer) InvokesModuleLayer.bindToLoader(ClassLoader).voidclose()Closes this classloader and all cached ModuleReader instances.private Enumeration<URL> enumerateResources(String name) protected Class<?> protected Class<?> protected URLfindResource(String moduleName, String name) protected Enumeration<URL> findResources(String name) private static voidforLayerAndParents(ModuleLayer layer, Set<ModuleLayer> visited, Consumer<ModuleLayer> operation) private static byte[]getClassBytes(ModuleClassLoader.ModuleInfo moduleInfo, String name) protected byte[]getMaybeTransformedClassBytes(String name, String context) getResource(String name) getResources(String name) protected Class<?> protected byte[]maybeTransformClassBytes(byte[] bytes, String name, @Nullable String context) private static @Nullable StringpackageName(String className) private @Nullable Class<?> readerToClass(ModuleClassLoader.ModuleInfo moduleInfo, String name) Returns null if the class should be treated as if it doesn't exist.private URLreaderToURL(ModuleClassLoader.ModuleInfo moduleInfo, String name) voidsetFallbackClassLoader(ClassLoader fallbackClassLoader) private static Enumeration<URL> singletonEnumeration(URL url) private static URLMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResourceAsStream, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
LAYER_BIND_TO_LOADER
-
moduleInfoCache
-
packageLookup
-
parentLoaders
-
configuration
-
fallbackClassLoader
-
closed
private volatile boolean closed
-
-
Constructor Details
-
ModuleClassLoader
-
ModuleClassLoader
public ModuleClassLoader(String name, Configuration configuration, List<ModuleLayer> parentLayers, @Nullable @Nullable ClassLoader parentLoader) This constructor allows setting the parent classloader. Use this with caution since it will allow loading of classes from the classpath directly if the system classloader is reachable from the given parent classloader.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
ClassNotFoundException.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.
-
-
Method Details
-
bindToLayer
InvokesModuleLayer.bindToLoader(ClassLoader). -
forLayerAndParents
private static void forLayerAndParents(ModuleLayer layer, Set<ModuleLayer> visited, Consumer<ModuleLayer> operation) -
readerToURL
- Throws:
IOException
-
toURL
-
getClassBytes
private static byte[] getClassBytes(ModuleClassLoader.ModuleInfo moduleInfo, String name) throws IOException - Throws:
IOException
-
readerToClass
@Nullable private @Nullable Class<?> readerToClass(ModuleClassLoader.ModuleInfo moduleInfo, String name) throws ClassNotFoundException Returns null if the class should be treated as if it doesn't exist.- Returns:
- null if the class should be treated as if it doesn't exist
- Throws:
ClassNotFoundException
-
maybeTransformClassBytes
-
loadClass
- Overrides:
loadClassin classClassLoader- Throws:
ClassNotFoundException
-
getResource
- Overrides:
getResourcein classClassLoader
-
findResource
- Overrides:
findResourcein classClassLoader- Throws:
IOException
-
getResources
- Overrides:
getResourcesin classClassLoader- Throws:
IOException
-
findResources
- Overrides:
findResourcesin classClassLoader- Throws:
IOException
-
enumerateResources
- Throws:
IOException
-
singletonEnumeration
-
findClass
- Overrides:
findClassin classClassLoader
-
findClass
- Overrides:
findClassin classClassLoader- Throws:
ClassNotFoundException
-
getMaybeTransformedClassBytes
protected byte[] getMaybeTransformedClassBytes(String name, String context) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
setFallbackClassLoader
-
close
Closes this classloader and all cached ModuleReader instances. This method is thread-safe and idempotent.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if an I/O error occurs while closing module readers
-
packageName
-
getConfiguration
-