【发布时间】:2018-06-15 10:47:10
【问题描述】:
我正在编写一个事件消息处理程序。为了处理反射,我使用了反射 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)
}
但是,在注册监听器时,对象被保存在元组中,但没有任何方法,有人知道为什么吗?
【问题讨论】:
标签: java scala reflection