package org.stianloader.mrjmania; import java.net.URL; import java.net.URLClassLoader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * Polyfill for the {@link URLClassLoader} regarding the fact that classloaders * can be named in Java 9 and beyond. * *
In Java 8 this doesn't do much, but in Java 9+ the classloader's name will show * up in stacktraces, which is very helpful for diagnosing certain classloading errors. * *
This classloader is parallel-capable. */ public class MRJURLClassLoader extends URLClassLoader { /** * Returns the name of the given classloader. For Java 8, * this method only returns for classloaders that are an instance of {@link MRJURLClassLoader}. * Otherwise, it returns {@code null}. * *
For Java 9 and above, it returns a value for any {@link ClassLoader}, * provided said {@link ClassLoader} has been assigned a name. * * @param loader The {@link ClassLoader} instance to get the name of. * @return The name of the given loader. */ @Nullable public static final String getClassloaderName(ClassLoader loader) { return loader.getName(); } /** * Obtains the platform classloader under Java 9 or above. Under Java 8, {@code null} is returned. * *
Keep in mind that while the platform classloader contains the platform classes, it has also * access to modules on the modulepath. This means that when using JPMS/Jigsaw, it may load classes * that are part of the 'app' classloader. This behaviour contradicts the behaviour for classes * present on the classpath, which cannot be loaded through the platform classloader. * * @return The platform classloader, if present. */ @Nullable public static final ClassLoader getPlatformClassLoader() { return ClassLoader.getPlatformClassLoader(); } /** * Constructor. * * @param name The name of the {@link ClassLoader}. * @param urls The URLs for the {@link URLClassLoader}, see {@link URLClassLoader#getURLs()}. * @param parent The parent classloader, see {@link ClassLoader#getParent()}. */ public MRJURLClassLoader(@NotNull String name, URL @NotNull[] urls, @Nullable ClassLoader parent) { super(name, urls, parent); } static { ClassLoader.registerAsParallelCapable(); } }