如何在类中查找带注释的方法?

时间:2016-06-02 08:30:04

标签: java reflection kotlin

给出注释

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyAnnotation

如何使用此注释查找方法?

这是我走了多远:

val cls = myObject.javaClass.kotlin
val found = cls.memberFunctions.filter { it.annotations.contains( ??? ) }

1 个答案:

答案 0 :(得分:7)

注释将是您的MyAnnotation课程的实例。因此,您需要做的就是:

cls.memberFunctions.filter { it.annotations.any { anno -> anno is MyAnnotation } }