Hi, is there a way to retrieve all classes having ...
# getting-started
g
Hi, is there a way to retrieve all classes having a custom annotation?
e
at compile time? sure, see #kapt or #ksp (but Kotlin/JVM, and JVM ecosystem in general, assumes an open world, so "all classes" at compile time may not be the same as "all classes" at runtime)
at runtime? generally not. on JVM (which is the platform with the most reflective ability), a classloader could be unenumerable (e.g. loaded over the network or generated at runtime). in practice, on JVM, the system is typically using URLClassLoader with paths on disk, so they're enumerable but not through any official API.
1
what's your goal?
g
Thx @ephemient - I’m working on a distributed framework, where users defines task’s name using an annnotation. Currently a worker has to add the map « task name -> class » in a configuration file. I was hoping that we could introspect the provided classes to do that without setup
e
and you have to look up a class at runtime based on the name in the annotation? if they're always in local JAR files, it's doable, but not exactly easy… I'd say the typical way of doing that in the JVM world would be to have your clients register a provider via https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html
👍 1
1
m
Assuming you define the annotation in your framework, you can scan the classpath using tools like Reflections (https://github.com/ronmamo/reflections) or Classgraph (https://github.com/classgraph/classgraph). Both have support for finding classes annotated with some specific annotation.
1
👍 2
e
Reflections (and other similar tools like Guava's ClassPath) are grabbing the class path from URLClassLoader and enumerating it themselves
likely to work in most cases in practice, but not guaranteed in all environments
c
Classgraph is really good choice I've used a bit in the past. It parses through the classfiles directly (without actually loading them), which makes it considerably faster and more accurate than other runtime solutions
🆒 1
e
definitely better to not load unnecessary classes. downside of that approach is that it has to be updated for every Java release, though
m
I was also going to suggest using a library such as classgraph, but reading @Gilles Barbier requirements, that users need to declare a task class locally, I think the most direct solution is to use a SPI