I want to fire an event in CDI whose type I can only determine on runtime. For instance, let's say there's some interface A
with implementing classes AA
and AB
. I have two observers:
public void observeAA(@Observes AA aa) {
}
public void observeAA(@Observes AB ab) {
}
Then some event producer:
@Inject @Any
private Event<A> event;
public A getPayload();
public void fire() {
this.event.fire(getPayload());
}
This doesn't work because A
is neither a subtype of AA
or AB
(it's the other way around). I've noticed there's a select
method that takes a subtype:
public <U extends T> Event<U> select(Class<U> subtype, Annotation... qualifiers);
However, it requires a correctly parameterized Class
object, which (correct if I'm wrong), I can't build at runtime.
Is there any solution or will I have to use qualifiers (possibly an annotation with a Class<?>
method)?
I ended up using a qualifier with a
Class<?>
member.EDIT
This is a simpler way of instantiating the annotation: