【问题标题】:Reflections getting all methods within a specific class which are annotated反射获取特定类中带注释的所有方法
【发布时间】: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


    【解决方案1】:

    通过做来解决

    @Retention(RetentionPolicy.RUNTIME)
    public @interface ListenTo
    

    对于其他任何由此而来的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 2020-09-27
      相关资源
      最近更新 更多