Mikhail Chibel
Mikhail Chibel

Reputation: 1955

CDI, get value from the qualifier inside the observer

I have event with a qualifier

@Inject
@ChangeType(FOO)
private Event<SomeEventClass> event

And I want to get value of the qualifier (FOO) inside the observer method. I know how to get qualifier as annotation but not sure how to get instance of the qualifier and its value.

public void listen(@Observes SomeEventClass event, EventMetadata meta) {
    Set<Annotation> qualifiers = meta.getQualifiers();
    for (Annotation qualifier : qualifiers) {
        //qualifier.annotationType()
    }
}

Upvotes: 0

Views: 431

Answers (1)

Antoine Sabot-Durand
Antoine Sabot-Durand

Reputation: 4970

You only have to cast the Annotation to your qualifier class.

 public void listen(@Observes SomeEventClass event, EventMetadata meta) {
    Set<Annotation> qualifiers = meta.getQualifiers();
    ChangeType ct = null;
    for (Annotation qualifier : qualifiers) {
       if (qualifier.annotationType().equals(ChangeType.class)) {
                ct = (ChangeType) qualifier;
       }
    }
    if (ct != null)
        //do something with ct.value 
}

Upvotes: 1

Related Questions