I have some strange edge case behavior I want to discuss and solve with you. And as a heads-up: Please do not ask why I want to do something :)
As far as my understanding goes, the JVM loads classes into the heap once they are needed (or forced to load) and once they are loaded, they stay there... That's also the reason why you have to overwrite bytecode though instrumentation to change e.g. a method of a class at runtime once it's loaded.
Now to my problem:
I have written a custom ClassLoader that works fine and is also able to load the class. I verified that by running:
CustomClassLoader classLoader = new CustomClassLoader();
Class<?> clazz = classLoader.loadClass("me.micartey.test.Core");
System.out.println(clazz);
Core core = new Core();
The output is following:
class me.micartey.test.core.Core
java.lang.NoClassDefFoundError: me/micartey/test/core/Core ...
So my suspicion is that classloader may not "share" all classes in the heap... :thinking: (Reflection works but not my goal)
I then tried to set the parent of the current classloader as the parent of my custom classloader and then my custom classloader as the parent of the current classloader. However, this results in a StackOverflowException when trying to load classes that my classloader uses
Preferably, I don't even need to call loadClass
and it is able to resolve the class without any reflection etc.