You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original issue created by ekuefler on 2013-05-26 at 03:18 AM
When a generic type is posted to the event bus, type erasure means that handlers for the same type with different parameters are invoked unexpectedly. The following test causes a ClassCastException:
@Test
public void test() {
EventBus eventBus = new EventBus();
eventBus.register(this);
eventBus.post(ImmutableList.of("one", "two"));
}
@Subscribe
public void handle(List<Integer> e) {
System.out.println(e.get(0) + 1);
}
Language restrictions probably make it impossible to handle this case correctly, but maybe EventBus could do something like notice if two handlers are being registered for the same erased type (but different declared types) and display a warning?
The text was updated successfully, but these errors were encountered:
Original comment posted by Yuriy.Kulikov.87 on 2014-09-14 at 02:52 PM
Hi,
you are right, you will not be able to distinguish between different types of generic lists after compilation because Java generics are erasure type. I think the issue can be closed as there is no way to fix it.
Besides, I think it makes little sense to post List<Integer> to the event bus anyway. You will not be able to use this type for some other purpose and it contains no information on what kind of integers are inside. You will not be able to add a comment to it. You are much better of posting events containing the list. For example:
/** Magic numbers have changed! @see MagicNumbers */
class MagicNumbersChanged {
public final List<Integer> numbers;
}
In this case you will be able to access the numbers as a generic list and at the same time you will be able to have different event types using List<Integer>.
There's currently a TODO in SubscriberRegistry.getAnnotatedMethodsNotCached() to error out in case of a generic parameter. Maybe that should be extended to include parameterized types.
I'm closing this issue because we are no longer going to be making changes to EventBus other than important bug fixes. The reasons we're discouraging it and some alternatives are listed in the EventBus javadoc.
Original issue created by ekuefler on 2013-05-26 at 03:18 AM
When a generic type is posted to the event bus, type erasure means that handlers for the same type with different parameters are invoked unexpectedly. The following test causes a ClassCastException:
@Test
public void test() {
EventBus eventBus = new EventBus();
eventBus.register(this);
eventBus.post(ImmutableList.of("one", "two"));
}
@Subscribe
public void handle(List<Integer> e) {
System.out.println(e.get(0) + 1);
}
Language restrictions probably make it impossible to handle this case correctly, but maybe EventBus could do something like notice if two handlers are being registered for the same erased type (but different declared types) and display a warning?
The text was updated successfully, but these errors were encountered: