[cdi-dev] WithAnnotations with ProcessInjectionTarget events

Nigel Deakin nigel.deakin at oracle.com
Fri Aug 14 08:02:17 EDT 2015


I'm writing a portable extension, and my Extension class includes the following observer method

   public <X> void processInjectionTarget(
       @Observes ProcessInjectionTarget<X> event) {

When I start the server (Glassfish) I see this warning in the log:

   WELD-000411: Observer method [BackedAnnotatedMethod] public com.foo.MyExtension.processAnnotatedType(@Observes 
ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using 
@WithAnnotations or a generic type with bounds.]]

I thought "OK, fair enough. I only want to receive ProcessInjectionTarget events for classes that have a specific 
annotation. So I'll do what it suggests and @WithAnnotations to restrict the events received."

However if I look up the javadocs for WithAnnotations it says "WithAnnotations may be applied to any portable extension 
observer method with an event parameter type of ProcessAnnotatedType to filter the events delivered."

And if I try using WithAnnotations on the above method, I indeed get an error at runtime.

So the warning message WELD-000411 is incorrect, since it suggests doing something that is not allowed.

@WithAnnotations would have been perfect for my needs. Why is it not allowed when observing ProcessInjectionTarget events?

So it looks as if I will simply have to receive every such event and check whether it has the required annotation:

   public <X> void processInjectionTarget(
       @Observes ProcessInjectionTarget<X> event) {

      if (!event.getAnnotatedType().isAnnotationPresent(MyAnnotation.class)) {
         return;
     }

This doesn't look very efficient to me, since there will be a lot of irrelevant events. Is there a better way?

Nigel




More information about the cdi-dev mailing list