-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
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:
- My compiler, Java 1.7.0_45 generates a function for Foo "public volatile void onEvent(java.lang.Object object)". This is a totally invalid function; there's no such thing as a volatile method. Issue seems to happen with "bridge" methods like Foo's onEvent
(http://stackoverflow.com/questions/10370033/java-volatile-in-method-signature, http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6868371 ) - SubscriberMethodFinder.findSubscriberMethods does not skip this invalid function
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! 👍