Skip to content

EventBus interacts poorly with generics #1431

Closed
@gissuebot

Description

@gissuebot

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions