[cdi-dev] WithAnnotations with ProcessInjectionTarget events

John D. Ament john.d.ament at gmail.com
Fri Aug 14 09:17:16 EDT 2015


Hi Nigel,

Maybe you meant to send this to weld-dev, since its a problem with one of
the CDI impls, not cdi-dev?

Sure, there's some spec enhancement listed here, but realistically
WithAnnotations doesn't apply to an injection target the same way (I
wouldn't think).

John

On Fri, Aug 14, 2015 at 8:02 AM Nigel Deakin <nigel.deakin at oracle.com>
wrote:

> 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
>
>
> _______________________________________________
> cdi-dev mailing list
> cdi-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/cdi-dev
>
> Note that for all code provided on this list, the provider licenses the
> code under the Apache License, Version 2 (
> http://www.apache.org/licenses/LICENSE-2.0.html). For all other ideas
> provided on this list, the provider waives all patent and other
> intellectual property rights inherent in such information.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20150814/30b72405/attachment.html 


More information about the cdi-dev mailing list