EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Subscriber not recognized when using extension functions

Open darkyelox opened this issue 7 years ago • 2 comments

Hi, i tried to register a bus into an extension function and also hear for events into another extension function like this:

typealias SomeClass = SomeClassImpl //SomeClassImpl is a class

fun SomeClass.someExtendedFunction(){
   with(someProperty){
      EventBus.getDefault().register(this@someExtendedFunction) // Exception
  }
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun SomeClass.onMessage(message:Message){
 // Do something
}

The exception is:EventBusException: Subscriber class SomeClassImpl and its super classes have no public methods with the @Subscribe annotation

darkyelox avatar Nov 03 '17 14:11 darkyelox

I suspect the issue is that extension functions are compiled to static methods, but not actual class members. The method has to be a non-static member of the class you register with EventBus, otherwise it won't be found. -ut

greenrobot-team avatar Nov 06 '17 10:11 greenrobot-team

Thanks, you are right, i ended using it directly in the class and not in the extension function, but would be nice if you add this functionality for kotlin or create extension functions for eventbus

darkyelox avatar Nov 07 '17 18:11 darkyelox