简体   繁体   中英

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?

Solved by doing

@Retention(RetentionPolicy.RUNTIME)
public @interface ListenTo

For anyone else who comes by this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM