I'm attempting to create a custom implementation of java.nio.FileSystem
for an existing Application.
The implementation itself seems to work correctly within its context.
However, the parent Application doesn't load my implementation via regular ClassPath but via an additional URLClassLoader.
Therefore, the implementation is currently only availabel via ServiceLoader.load(FileSystemProvider.class, specificClassLoaderOfMyCode)
but it will not be used via APIs like java.nio.Files
.
I can modify the parent application but cannot remove the intermediate ClassLoader. I do have access to the parent ServiceLoader and the child's ServiceLoader. My goal is to get all implementations from the child and put them into the parent's ServiceLoader. The Application itself is written in Spring, and the parent's ClassLoader is, therefore, the default Spring ClassLoader.
The only way I can see is to use reflection in order to modify the internal cache and force the implementation into it, but java.base
doesn't open the relevant class.
Therefore, I'm not allowed to access the private fields of ServiceLoader.
I could add the relevant flags, but I'd like to avoid this approach.
Is there any alternative to dynamically add an implementation to a ServiceLoader, that is not known to the ClassLoader (not present in META-INF/services
)?