小编Ric*_*lho的帖子

如何从Java获取Kotlin属性的注释?

我有一个kotlin类,其属性具有Java注释,但我无法使用Java反射访问这些注释:

class TestClass(@A var myProperty : String)
Run Code Online (Sandbox Code Playgroud)

以下测试打印为null:

public class TestKotlinField {

    @Retention(RetentionPolicy.RUNTIME)
    public @interface A{}

    @Test
    public void test() throws NoSuchFieldException {
        System.out.println(TestClass.class.getDeclaredField("myProperty").getAnnotation(A.class));
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得给定kotlin属性的注释?

java reflection kotlin

8
推荐指数
2
解决办法
2904
查看次数

标签 统计

java ×1

kotlin ×1

reflection ×1