classloaders: Java class loaders module (three built-in loaders and which modules each owns, parent delegation traced across a two-loader chain, child-first PluginLoader, class identity and the failing (Plugin) cast, Class.forName vs loadClass initialisation, ClassNotFoundException vs NoClassDefFoundError incl. 'Could not initialize class', hot reload, unloading and a Registry leak, context class loader, jcmd VM.classloaders), 18 contract tests
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5pUjAyJuUaNEQqzGJ
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
74da9cd899
commit
cca173d104
@@ -0,0 +1,26 @@
|
||||
|
||||
== Which loader defined each class ==
|
||||
java.lang.String -> null (the bootstrap loader)
|
||||
java.util.ArrayList -> null (the bootstrap loader)
|
||||
java.sql.Driver -> 'platform' (jdk.internal.loader.ClassLoaders$PlatformClassLoader)
|
||||
javax.tools.JavaCompiler -> 'platform' (jdk.internal.loader.ClassLoaders$PlatformClassLoader)
|
||||
HierarchyDemo (this class) -> 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
int[] -> null (the bootstrap loader)
|
||||
HierarchyDemo[] -> 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
|
||||
== The parent chain, starting from the system (application) loader ==
|
||||
-> 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
-> 'platform' (jdk.internal.loader.ClassLoaders$PlatformClassLoader)
|
||||
-> null (the bootstrap loader has no parent object)
|
||||
|
||||
getPlatformClassLoader() == getSystemClassLoader().getParent(): true
|
||||
Thread context class loader of main: 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
|
||||
== Modules by defining loader (JDK 25) ==
|
||||
app 19 modules, e.g. [jdk.attach, jdk.compiler, jdk.editpad, jdk.internal.ed]
|
||||
bootstrap 21 modules, e.g. [java.base, java.datatransfer, java.desktop, java.instrument]
|
||||
platform 21 modules, e.g. [java.compiler, java.net.http, java.scripting, java.security.jgss]
|
||||
|
||||
== Named module vs unnamed module ==
|
||||
String.class.getModule() -> module java.base
|
||||
HierarchyDemo.class.getModule() -> named? false (class-path classes live in the loader's unnamed module)
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
== child.loadClass("com.acme.UsesLib") ==
|
||||
[child] loadClass(com.acme.UsesLib)
|
||||
[shared] loadClass(com.acme.UsesLib)
|
||||
[shared] findClass(com.acme.UsesLib) -> not in my directory
|
||||
[child] findClass(com.acme.UsesLib) -> defineClass
|
||||
defined by: 'child' (com.ankurm.classloaders.DirLoader)
|
||||
|
||||
== Running it: UsesLib needs Lib, which triggers a second load ==
|
||||
[child] loadClass(com.acme.shared.Lib)
|
||||
[shared] loadClass(com.acme.shared.Lib)
|
||||
[shared] findClass(com.acme.shared.Lib) -> defineClass
|
||||
result: UsesLib called Lib
|
||||
|
||||
== Where did Lib end up? ==
|
||||
[child] loadClass(com.acme.shared.Lib)
|
||||
Lib defined by: 'shared' (com.ankurm.classloaders.DirLoader) (the parent, not the loader that was asked)
|
||||
|
||||
== A JDK class never reaches the child's own directory ==
|
||||
java.lang.String defined by: null (the bootstrap loader)
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
== loadClass: loads and links, does NOT initialise ==
|
||||
loaded com.acme.Noisy, nothing printed above this line
|
||||
|
||||
== Class.forName(name, false, loader): same, explicitly ==
|
||||
loaded com.acme.Noisy, nothing printed above this line
|
||||
|
||||
== First real use triggers initialisation (static method call) ==
|
||||
>> Noisy static initialiser ran
|
||||
|
||||
== Class.forName(name, true, loader): loads and initialises immediately ==
|
||||
>> Noisy static initialiser ran
|
||||
done
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
== 1. ClassNotFoundException: asking by name for a class nobody has ==
|
||||
java.lang.ClassNotFoundException: com.acme.DoesNotExist
|
||||
checked exception? true
|
||||
|
||||
== 2. NoClassDefFoundError: compiled against Dep, Dep.class missing at run time ==
|
||||
with Dep.class present: Dep is here
|
||||
...Dep.class deleted from disk...
|
||||
same loader, Dep already loaded: Dep is here (the loader cached it)
|
||||
fresh loader, Dep never loaded:
|
||||
java.lang.NoClassDefFoundError: com/acme/missing/Dep
|
||||
caused by: java.lang.ClassNotFoundException: com.acme.missing.Dep
|
||||
is an Error, not an Exception: true
|
||||
|
||||
== 3. NoClassDefFoundError: "Could not initialize class" (the class file is fine) ==
|
||||
attempt 1: java.lang.ExceptionInInitializerError, cause java.lang.NumberFormatException: For input string: "not-a-number"
|
||||
attempt 2: java.lang.NoClassDefFoundError: Could not initialize class com.acme.initfail.Broken
|
||||
cause: java.lang.ExceptionInInitializerError: Exception java.lang.NumberFormatException: For input string: "not-a-number" [in thread "main"]
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
== Class identity = name + defining loader ==
|
||||
same name: true
|
||||
same Class object: false
|
||||
c1 loader: 'plugin-v1' (com.ankurm.classloaders.PluginLoader)
|
||||
c2 loader: 'plugin-v2' (com.ankurm.classloaders.PluginLoader)
|
||||
both usable as Plugin (the interface is shared via the parent):
|
||||
p1.run() = Hello from GreeterPlugin v1
|
||||
p2.run() = Hello from GreeterPlugin v2
|
||||
c2.cast(p1): Cannot cast com.acme.GreeterPlugin to com.acme.GreeterPlugin
|
||||
|
||||
== The cast that fails even though the names match ==
|
||||
ClassCastException: class com.acme.GreeterPlugin cannot be cast to class com.ankurm.classloaders.api.Plugin (com.acme.GreeterPlugin is in unnamed module of loader 'plugin-bad' @<hash>; com.ankurm.classloaders.api.Plugin is in unnamed module of loader 'app')
|
||||
|
||||
== Hot reload: a fresh loader per version ==
|
||||
Hello from GreeterPlugin v1
|
||||
Hello from GreeterPlugin v2
|
||||
Hello from GreeterPlugin v1
|
||||
(the same class name, three times, three different Class objects)
|
||||
|
||||
== Unloading: a loader is collectable only when nothing reachable points into it ==
|
||||
plugin dropped, nothing else references it -> loader collected: true
|
||||
plugin instance stored in static Registry -> loader collected: false
|
||||
Registry cleared -> (new loader) collected: true
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
== ServiceLoader.load(Plugin.class) uses the thread context loader (the app loader) ==
|
||||
context loader: 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
providers found: 0
|
||||
|
||||
== ServiceLoader.load(Plugin.class, pluginLoader) names the loader explicitly ==
|
||||
found com.acme.GreeterPlugin via 'plugin-ctx' (com.ankurm.classloaders.PluginLoader)
|
||||
providers found: 1
|
||||
|
||||
== Or set the context loader for the duration of a call, then restore it ==
|
||||
context loader: 'plugin-ctx' (com.ankurm.classloaders.PluginLoader)
|
||||
found com.acme.GreeterPlugin via 'plugin-ctx' (com.ankurm.classloaders.PluginLoader)
|
||||
providers found: 1
|
||||
restored: 'app' (jdk.internal.loader.ClassLoaders$AppClassLoader)
|
||||
@@ -0,0 +1,5 @@
|
||||
java.lang.String source: shared objects file
|
||||
java.util.ArrayList source: shared objects file
|
||||
com.ankurm.classloaders.HierarchyDemo source: file:<module>/target/classes/
|
||||
java.sql.Driver source: jrt:/java.sql
|
||||
javax.tools.JavaCompiler source: jrt:/java.compiler
|
||||
@@ -0,0 +1 @@
|
||||
9 unloading class com.acme.GreeterPlugin
|
||||
@@ -0,0 +1,11 @@
|
||||
<pid>:
|
||||
+-- <bootstrap>
|
||||
|
|
||||
+-- "platform", jdk.internal.loader.ClassLoaders$PlatformClassLoader
|
||||
|
|
||||
+-- "app", jdk.internal.loader.ClassLoaders$AppClassLoader
|
||||
|
|
||||
+-- "plugin-v1", com.ankurm.classloaders.PluginLoader
|
||||
|
|
||||
+-- "plugin-v2", com.ankurm.classloaders.PluginLoader
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
[INFO] Tests run: 18, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.027 s -- in com.ankurm.classloaders.ClassLoaderContractTest
|
||||
[INFO] Tests run: 18, Failures: 0, Errors: 0, Skipped: 0
|
||||
[INFO] BUILD SUCCESS
|
||||
Reference in New Issue
Block a user