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
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