If you're prepared to do the scanning in build time rather than at runtime, then bnd has a rather nice macro you can use:
MyAnnotated-Classes: ${classes;CONCRETE;ANNOTATION;org.example.MyAnnotation}
...which will generate a MyAnnotated-Classes
head in the manifest that lists all of the classes in your bundle that have @MyAnnotation
. Scanning this header at runtime is now trivial.
This approach is a significant optimisation over runtime classpath scanning. Also runtime scanning can be unreliable, since you need to catch the contents of Bundle-ClassPath
, but avoid any imported/required classes.