mysterymyster
mysterymyster

Reputation: 43

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:

import org.reflections.ReflectionUtils._
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?

Upvotes: 0

Views: 284

Answers (1)

mysterymyster
mysterymyster

Reputation: 43

Solved by doing

@Retention(RetentionPolicy.RUNTIME)
public @interface ListenTo

For anyone else who comes by this.

Upvotes: 1

Related Questions