greenrobot / EventBus

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
http://greenrobot.org/eventbus/
Apache License 2.0
24.66k stars 4.66k forks source link

Abstract subscriber classes and Subscriber Index #428

Open jverne opened 7 years ago

jverne commented 7 years ago

I think this is probably a Java annotations/language question. However, in the interest of discussion and to satisfy my doubts in the context of EventBus my assumption is:

When creating a Subscriber Index on an Abstract class of annotated Subscribe methods we will not get the benefits of the index for overridden methods.

e.g. Given this Abstract class in an Android library:

public abstract class AbstractEventHandlers {
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSubmitJob(JobStartedEvent event) {
       ...
    }
}

and this extending class in an Android Application:

public final class EventHandlers extends AbstractEventHandlers {
    @Override
    public void onSubmitJob(JobStartedEvent event) {
        ...
    }

If the library project has the incantations to create the SubscriberIndex class, my assumption is that the index will apply to the Abstract class only. The generated Index class seems to suggest this, but I'm just checking here in case there is some magic I'm unaware of.

Note that the Android library has all the code necessary to create the EventBus, provide accessors for registration, etc. That is, the Application doesn't really know it is subscribing to an EventBus. It just knows it is overriding some event handling methods and asking for those to be run when it has expressed interest in the publicly available Event that the library offers.

greenrobot-team commented 7 years ago

I do not see any reason why this should not work. As long as you make sure to add the index to the EventBus instance that is used by the library/app. -ut

greenrobot-team commented 7 years ago

Update sorry, you are right: the EventBus index annotation processor does no magic to find methods in subclasses. So on runtime the lookup will be done using reflection for subclasses. -u

jverne commented 7 years ago

Yes, I am creating an index, but only in the library code that uses EventBus. That is, I want to hide the implementation details that EventBus is being used, and have the library handle all pub/sub activities.

However, if we want an index this has to happen at the app level, which does not know about 'org.greenrobot:eventbus-annotation-processor', which violates the encapsulation I'm trying to enforce via an Abstract class and generics (I don't show the generics in the code snippet above).

Thanks for the confirmation.