I know that with cdi, you can collect all types (classes/interfaces) annotated with a certain annotation (By observing ProcessAnnotatedType). However, I would like to collect all methods annotated with a certain annotation. Is this possible with pure CDI? I have an example doing this using the Reflections library (org.reflections), but I would like to do this with cdi if possible. Does anyone know how to do this? Thanks, Connor
-
I am not aware of any such mechanism. Use the reflections library. – mtj Mar 07 '20 at 06:24
2 Answers
Actually, an observer method like this:
private final <T> void typeFound(@Observes
@WithAnnotations(MyMethodAnnotation.class)
final ProcessAnnotatedType<T> event) {
}
…will be fired for any type that is discovered by CDI and that has @MyMethodAnnotation
present anywhere within it.
The "discovered by CDI" part means that the type in question has to be discoverable in the first place. Normally this happens when you put a bean defining annotation on the type.
(I say "normally" because a bean discovery mode of annotated
is the default and probably the most common "in the wild". There are others.)
Then you can get access to all the AnnotatedMethod
s from the AnnotatedType
object reachable from the AnnotatedType
object reachable from the ProcessAnnotatedType
event.

- 15,321
- 19
- 73
- 127
-
Hi Laird, thanks for your insight. This is what I had originally tried (with a bean discovery mode of all), and I couldn’t get it to work (same code as above in a class that implements Extension). Is there anything else I am forgetting? In the meantime, I will go ahead and try the code again. Thanks – Connor Butch Mar 09 '20 at 21:41
-
I can't think of anything. I generally try to use `annotated` and use bean-defining annotations so there is no question about what is discovered and what is not. – Laird Nelson Mar 09 '20 at 22:28
-
Actually, I did forget something that may be obvious, but maybe not: make sure your `Extension` is also registered in a `META-INF/services/javax.enterprise.inject.spi.Extension` file. – Laird Nelson Mar 09 '20 at 23:50
-
This won't work, you will only get notified for CDI bits. And certainly not for all *methods*. I am not sure what you'd get if you observed all PAT (in discovery mode `all`) and then check their methods, but that's very much brute force way anyway... – Siliarus Mar 17 '20 at 14:59
-
@Silarius, a `ProcessAnnotatedType` event together with `@WithAnnotations(MyMethodAnnotation.class)` will certainly find all discovered types that feature `@MyMethodAnnotation` somewhere within their code. As you say, it is then up to the user to go find where, exactly, within that type the annotation occurs. – Laird Nelson Mar 17 '20 at 16:02
No, you cannot do this with pure CDI, you should use some different means.
What you're trying to do is out of scope of CDI. Your original idea with CDI extension is a good one, however it won't trigger for all methods you'd want it to. It is tailored to hook into the CDI bootstrap process and alter/enrich that. Not to meddle with all classes in the deployment.
You'd be better off using some that focuses on inspecting all classes in the deployment. Jandex comes to my mind, it is super fast to search in once you build the index.

- 6,393
- 1
- 14
- 30