如何阅读Kotlin注释
我有注释类,
annotation class Endpoint(val name: String)
@Target(AnnotationTarget.TYPE)
annotation class JsonObjectKey(val name: String)
使用
@Endpoint("my-endpoint")
fun myEndpoint(): @JsonObjectKey("key") String {
return "value"
}
这些都像方法一样 :
我能够访问
endpoint
- 通道,但是我无法访问 jsonobjectKey
-antatation。
method.annotations
仅包含 endpoint
method.antotatedReturntype.annotations
是空的
strong>(键
在这种情况下)?
意图是生成jsonobject {“键”:“ value”}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Kotlin反射API!
例如,此功能:
此输出:
如果您只有
java.lang.reflect.method
出于某种原因,则可以使用 kfunction =“ https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect.jvm/java.lang.reflect.reflect.-method.-method/kotlin-function.htcon.html” :看来,在Java中以此位置注释的注释与
Kotlin中该位置的注释的注释大不相同:
这些似乎是两个不同的位置。 Kotlin反射无法看到Java返回类型上的注释,Java反射无法看到Kotlin返回类型上的注释。
比较注释在类文件中的出现。对于Java方法,注释出现在
RuntimeVisibleTypeAntiations
属性:另一方面,Kotlin方法将注释存储为
kotlin.metadata
注释的一部分:Use the Kotlin reflection API!
For example, this works:
This outputs:
If you only have a
java.lang.reflect.Method
for some reason, you can convert it to aKFunction
usingkotlinFunction
:It appears that annotations annotated in this position in Java:
are very different from those annotated in this position in Kotlin:
These seem to be two different positions. Kotlin reflection cannot see the annotation on the Java return type, and Java reflection cannot see the annotation on the Kotlin return type.
Compare how the annotations appear in the class file. For the Java method, the annotation shows up in the
RuntimeVisibleTypeAnnotations
attribute:On the other hand, the Kotlin method instead has the annotation stored as part of the
kotlin.Metadata
annotation: