It seems that everyone had an unpleasant brush with the Java Service Provider , this thing you can do with a file called META-INF / services / com.example.Interface but no one uses, except for trying to load the correct XML parser . I am trying to work with a library using the service provider's API, and tricking it so that I can provide some advanced runtime classes (using cglib) that actually do not implement the interface, but can be done so easily.
Basically, I think the steps I need to follow are as follows:
- Create a custom classloader that will respond to getResources (...) and return an "extra" URL
- You also have a getResourceAsStream (...) class loader hooker to return a list of classes that I will manipulate cglib when you are asked for an βextraβ resource
- Finally, if this classloader loads these classes upon request
But here where I got lost. For example, when a library tries to determine which developers are there, it calls getResources (...), which returns a bunch of URLs. But getResourceAsStream (...) does not accept URLs, it accepts "names". Names that seem classpath-relative and therefore the same everywhere. So META-INF / services / com.example.Interface in has the same "name" as META-INF / services / com.example.Interface in its JAR, right? Also, somehow it works with these hacked XML parsers ...
Of course, all this suggests that they were smart enough / enough to call ClassLoader.getSystemClassLoader (), and not use ClassLoader.getSystemResources (...), ClassLoader.getSystemResourceAsStream (...), etc., as in in the latter case, it is not possible to connect ClassLoader and provide a fake file.
I assume that in this case, I could use BCEL to manipulate class files when my code is packaged by Maven, and not wait until execution is completed using cglib?
source
share