我正在编写一个事件消息处理程序。为了处理反射,我使用 Reflections API ( https://github.com/ronmamo/reflections )。
每个事件监听器都是一个用
注释的方法public @interface ListenTo {}
听众会遵循这样的主题:
class Example {
EventHandler.get.registerListener(this)
@ListenTo
def onEvent(e: SomeEvent): Unit
}
注册监听器的代码如下:
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)
}
但是,在注册监听器时,对象保存在元组中,但没有方法,有人知道为什么吗?
最佳答案
解决了
@Retention(RetentionPolicy.RUNTIME)
public @interface ListenTo
对于任何其他来此的人。
关于java - 反射获取特定类中带注释的所有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50874035/