Reputation:
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";
}
Upvotes: 1
Views: 860
Reputation: 721
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());
Upvotes: 1
Reputation: 28951
for(Annotation a : injectionPoint.getQualifiers())
{
if(a instanseof MyCar)
{
MyCar myCar = (MyCar) a;
a.mpg();
}
}
Upvotes: 2