Classloader that has higher priority than parent
I need to change the class loader that is in use when testing a mock framework, which is because the API I'm mocking needs specific class loader to instantiate a class.
Thankfully this is possible to do using LauncherInterceptors (JUnit) with custom claass loader implementation. But I have encountered some issues:
- Some classes has already been loaded by the default class loader. The custom class loader needs to be able to fetch these class references of the default class loader.
- Except for the above case and if the class is in a java standard library, the class needs to be loaded by the custom class loader. this is because the class loader that is selected when loading classes seems to be the same class loader that loaded the previous class (Not 100% sure on this, but purely based on behavior this seems to be the case)
Let me just write a workflow of what I think would be necessary for this to work:
1. First check default class loader if the class has already been loaded by it, if that's the case use that class.
2. Try to load the class with Bootstrap class loader (java standard libraries)
3. Try to load the class using the custom class loader implementation (Will use same resources as default class loader)
4. Load the class using child implementations.
The problem with that workflow is that I can't find a way do part 1. It does not seem to be possible to get only previously loaded classes from another class loader instance in java versions after java 9. There was a way previously where you could access a protected method in the class loader, but now that has been access restricted
3 Replies
⌛
This post has been reserved for your question.
Hey @Thorinwasher! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
If you don't know the answer on how to find a loaded class // find if it's been loaded previously, do you alternatively:
- Know where else to ask
- Know if it's possible at all to accomplish
I know this is a real edge case of a question, but any help is deeply appreciated. Hmm. I can actually show my current try for this, probably does a lot of stuff wrongfully. Here:
https://github.com/Thorinwasher/MockBukkit/blob/plugin-class-loader-fix/src/main/java/be/seeseemelk/mockbukkit/plugin/MockBukkitClassLoader.java
(this ignores point 1 in my workflow, and therefore does not work properly)
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.