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

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

Closed
pauledwardtang opened this issue Apr 9, 2014 · 2 comments
Closed

Comments

@pauledwardtang
Copy link

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! 👍

@greenrobot
Copy link
Owner

That's really weird. Could you provide a unit test to reproduce this behavior?

@greenrobot
Copy link
Owner

c6f999d

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

No branches or pull requests

2 participants