Class identity, loaders and leaks
Definition:
JVM class identity combines the binary name with the defining class loader.
Explanation:
Class identity is the pair of name and defining loader.
Illustration image:
Example:
System.out.println(plugin.getClass().getName());
System.out.println(plugin.getClass().getClassLoader());
System.out.println(Api.class.getClassLoader());Expected behavior. Compare defining loaders when identical names still fail an interface cast.
Internal working:
A plugin can fail a cast even when names match if private copies are loaded by different loaders. Thread context loaders, global registries and application-created threads can keep an old deployment alive. Establish shared API loading and explicitly unregister resources during unload.
Interview questions:
Why does a class with the right name still fail a cast?
Two class loaders may have defined independent classes with that binary name.