Skip to content

EventBus does not support generics very well #6024

Closed
@NoSugarIce

Description

@NoSugarIce

code

public class EventbusDemo {

    public static void main(String[] args) {

        EventBus eventBus = new EventBus();
        eventBus.register(new EventListenerInteger());
        eventBus.register(new EventListenerString());

        eventBus.post("Hello");
    }

    public static abstract class AbstractEventListener<T> {

        @Subscribe
        @AllowConcurrentEvents
        public void onEvent(T event) {
            System.out.println(Thread.currentThread().getName() + "-" + this.getClass().getSimpleName() +"-onEvent-"
                    + "->EventType:" + event.getClass().getName() + "-EventValue:" + event);
        }

    }

    public static class EventListenerInteger extends AbstractEventListener<Integer> {
    }

    public static class EventListenerString extends AbstractEventListener<String> {
    }

}

Run the result

main-EventListenerInteger-onEvent-->EventType:java.lang.String-EventValue:Hello

main-EventListenerString-onEvent-->EventType:java.lang.String-EventValue:Hello

hope

Distribute different tasks based on subclass's qualified generics

hope result

main-EventListenerString-onEvent-->EventType:java.lang.String-EventValue:Hello

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions