Skip to content

Subscribers are invoked for all events instead of registered events #76

@pauledwardtang

Description

@pauledwardtang

Hello,

I'm integrating this library into my own project and I'm running across a very bizarre issue that causes an event handler to be registered for all events without me specifying as such.

Specifically, I have a class Foo which is derived from some other class (which doesn't have any "onEvent" methods) which subscribes to events for SomeEnum.

public enum SomeEnum() {
    // Nothing fancy
}

public class Foo extends SomeClassWithNoOnEvent {
    ...
    public void onEvent(SomeEnum someEnum) { /* Handler logic */ }
    ...
}

On the same EventBus, I register another class which has nothing to do with Foo.

public class Bar {
    ...
    public void onEvent(SomeType someType) { /* Handler logic */}
}

When I post an instance of "SomeType", EventBus attempts to invoke Bar (correctly) and Foo's onEvent even though I never signed up for it.

Could not dispatch event: class com.foo.Bar to subscribing class class com.foo.Foo
java.lang.ClassCastException: com.foo.Bar cannot be cast to com.foo.Foo
    at com.foo.Foo.onEvent(Foo.java:1)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:569)
    at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:500)
    at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:475)
    at de.greenrobot.event.EventBus.post(EventBus.java:365)
    at com.foo.SomeClass(SomeClass.java:286)

After stepping through a debugger and researching online I found out some interesting tidbits:

It shouldn't be difficult to handle this, considering findSubscriberMethods is already using Modifier flags (just need to use Modifier.VOLATILE) in this block:

if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
...
}

As a work around, I suspect isolating Foo to it's own EventBus would prevent the nasty logs from showing up. I can follow up with results of that, if need be.

Anyways, this would certainly be a welcome fix for any users experiencing the same problem! 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions