atteo / classindex

Index classes, do not scan them!
Apache License 2.0
263 stars 43 forks source link

Java 9, modulepath and jrt URLs #42

Closed kaqqao closed 6 years ago

kaqqao commented 6 years ago

There's a talk about Java 9 support in all similar libraries. E.g. here's the relevant issue in Reflections, and this library was mentioned.

What's the status on Java 9 support here?

sentinelt commented 6 years ago

Hi, I haven't tested the Classindex with Java 9 yet. I will probably give it a try next week. I expect less compatibility issues with changes made in classloaders mechanics as Classindex is not based on runtime reflections and instead does compile time indexing.

I will add some unit tests to ensure the correctness. In the meantime let me know if you find and issues with it.

kaqqao commented 6 years ago

It's not only the classloader changes that worry me, but the fact that classpath as such is now kind of a legacy thing. Now each module has its own modulepath, and those can now contain not only file system paths to JAR or class files, but the newly specified jrt:// URLs. Without explicit support for these, I think the scanning would work but would be incomplete. This is currently the situation in most libraries...

There's some helpful hints in my SO thread.

kaqqao commented 6 years ago

Btw, pardon my ignorance here, how does ClassIndex decide what paths to scan? Since it all happens at compile time, there's no such thing as classpath yet to tell it.

sentinelt commented 6 years ago

Classindex analyses the classes as they are compiled. And so must be present on the classpath during compilation. For each compilation unit several indexes are created for all the classes which where compiled. Those indexes are then stored as a resources so they can be retrieved from the classpath at run-time. For instance, if you have a Maven project which builds a jar file then the indexes will take into account all the classes from this jar file, but not from any dependencies of this project. This means you will not get the classes from 3rd party libraries by default.

At runtime, when (for instance) you query for the classes implementing given interface, all the indexes which are reachable from the current context are analysed.

Using URLClassLoader, depending on its logic and assuming it's the only type of ClassLoader used was always a kind of hack IMHO. Instead Classindex uses annotation processing and reflection API. Although annotation processing is rough around the edges, especially when doing incremental compilation, it works pretty well with some workarounds applied.

kaqqao commented 6 years ago

Ah, I understand now. Thanks for the explanation!

Since it doesn't scan 3rd party classes (including JDK's own), it doesn't sound like it should be bothered much by the Java 9 changes.

ptahchiev commented 6 years ago

Any updates here? Is it safe to use classindex with java9?

sentinelt commented 6 years ago

I run it currently on Java 9. Looks OK. Although we do not use much of the new Jigsaw stuff.

lukehutch commented 6 years ago

Hi, I figured out how to scan modules, in case you are interested in adding support for this.

https://stackoverflow.com/a/45612376/3950982

bmarwell commented 3 years ago

hey everyone. This is closed, BUT I have some information for you. In Module 1, I am using the APT to generate the info files. In this module, just use require static org.atteo.classindex on your module-info.java. In Module 2, I only use it for tests – no additional requirements needed.

HTH