Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventBus interacts poorly with generics #1431

Closed
gissuebot opened this issue Oct 31, 2014 · 3 comments
Closed

EventBus interacts poorly with generics #1431

gissuebot opened this issue Oct 31, 2014 · 3 comments
Assignees

Comments

@gissuebot
Copy link

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?

@gissuebot
Copy link
Author

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>.

BR,
Yuriy

@gissuebot gissuebot added type=defect Bug, not working as expected migrated package=eventbus labels Nov 1, 2014
@cgdecker cgdecker removed the migrated label Nov 1, 2014
@shmosel
Copy link

shmosel commented Dec 31, 2015

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.

@cgdecker
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants