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

ato*_*tok 3 java reflection kotlin

给出一个注释

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyAnnotation
Run Code Online (Sandbox Code Playgroud)

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

这是我得到了多远:

val cls = myObject.javaClass.kotlin
val found = cls.memberFunctions.filter { it.annotations.contains( ??? ) }
Run Code Online (Sandbox Code Playgroud)

yol*_*ole 7

注释将是您的MyAnnotation类的实例.因此,您需要做的就是:

cls.memberFunctions.filter { it.annotations.any { anno -> anno is MyAnnotation } }
Run Code Online (Sandbox Code Playgroud)