Search code examples
javacdijboss-weld

How to read non-binding attribute using java cdi's injection point


If I have an InjectPoint class instance, how do I read the attributes of the Annotation from it. i.e: annotated with Qualifier @MyCar(mpg="23")

How would I get the mpg and "23" if I have an injectionPoint when MyCar is injected?

@Inject
public void injectionTest(@MyCar(mpg="23") Car _car,InjectionPoint ip)
{
...
}


@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface MyCar
{
    @Nonbinding
    String mpg() default "30";
}

Solution

  • for(Annotation a : injectionPoint.getQualifiers())
    {
       if(a instanseof MyCar)
       {
         MyCar myCar = (MyCar) a;
         a.mpg();
       }
    }