-
1. Re: Event firing/matching and parameterized types
ljnelson Jun 17, 2018 9:34 PM (in response to ljnelson)Here's a test case using more simple constructs:
private final void collectionExtendsNumber(@Observes final Collection<? extends Number> payload) {
System.out.println("*** collection extends Number");
}
private final void collectionExtendsInteger(@Observes final Collection<? extends Integer> payload) {
System.out.println("*** collection extends Integer");
}
private final void collectionInteger(@Observes final Collection<Integer> payload) {
System.out.println("*** collection Integer");
}
private final void collectionNumber(@Observes final Collection<Number> payload) {
System.out.println("*** collection Number");
}
@Test
public void testContainerStartup() {
final SeContainerInitializer initializer = SeContainerInitializer.newInstance();
assertNotNull(initializer);
initializer.disableDiscovery();
initializer.addBeanClasses(this.getClass());
try (final SeContainer container = initializer.initialize()) {
assertNotNull(container);
final BeanManager beanManager = container.getBeanManager();
assertNotNull(beanManager);
final TypeLiteral<Collection<? extends Number>> literal = new TypeLiteral<Collection<? extends Number>>() {
private static final long serialVersionUID = 1L;
};
final Event<Collection<? extends Number>> broadcaster = beanManager.getEvent().select(literal);
assertNotNull(broadcaster);
final Collection<? extends Number> payload = Collections.singleton(Integer.valueOf(1));
broadcaster.fire(payload);
}
}
In this case, only the first observer method prints anything.
The main thing that I'm struggling with is that at the point where I would like to fire my payload, all I know is that it is, effectively, Collection<? extends Number>, even though it might "actually" be Set<Integer> (as in the example above).
So my TypeLiteral has to be TypeLiteral<Collection<? extends Number>>, unless I'm missing something.
And if it has to be TypeLiteral<Collection<? extends Number>>, then I don't know how to fire the payload in such a way that an observer of, say, Collection<? extends Integer> or Collection<Integer> would be notified.
-
2. Re: Event firing/matching and parameterized types
manovotn Jun 18, 2018 7:53 AM (in response to ljnelson)Hi Laird,
I've added an answer to your SO crosspost, so in order not to dulicate info, here is the link.
I am still puzzled by the last case and if I will try to get more information about that.