Question

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";
}
Was it helpful?

Solution

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

OTHER TIPS

You can avoid iterating through the annotations by using the getAnnotated method. You can also access other annotations this way, not just qualifiers.

MyCar myCar = ip.getAnnotated().getAnnotation(MyCar.class);
System.out.println(myCar.mpg());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top