https://kotlinlang.org logo
#getting-started
Title
# getting-started
g

Gilles Barbier

10/08/2021, 8:29 AM
Hi, is there a way to retrieve all classes having a custom annotation?
e

ephemient

10/08/2021, 8:41 AM
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

Gilles Barbier

10/08/2021, 8:49 AM
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

ephemient

10/08/2021, 8:54 AM
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

Mranders

10/08/2021, 9:00 AM
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

ephemient

10/08/2021, 9:08 AM
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

Casey Brooks

10/08/2021, 2:34 PM
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

ephemient

10/08/2021, 11:55 PM
definitely better to not load unnecessary classes. downside of that approach is that it has to be updated for every Java release, though
m

Matteo Mirk

10/11/2021, 4:54 PM
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
4 Views