vendredi 15 juin 2018

Java/scala Reflections getting all methods within a specific class which are annotated

I am writing an event message handler. To handle the reflection, I am using Reflections API (https://github.com/ronmamo/reflections).

Each event listener is a method annotated by

public @interface ListenTo {}

and a listener would follow a theme such as:

class Example {
  EventHandler.get.registerListener(this)

  @ListenTo
  def onEvent(e: SomeEvent): Unit 
}

and the code to register the listeners is as follows:

private var listeners: mutable.ListBuffer[(Any, List[Method])] = ListBuffer()

def registerListener(obj: Any): Unit = {
  listeners += Tuple2(obj, getAllMethods(obj.getClass, withAnnotation(classOf[ListenTo])).asScala.toList)
}

However, on registering a listener, the object is saved within the tuple, but no methods with it, does anybody know why?





Aucun commentaire:

Enregistrer un commentaire