如何阅读Kotlin注释

发布于 2025-02-06 03:39:04 字数 631 浏览 1 评论 0 原文

我有注释类,

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”}

I have annotation classes

annotation class Endpoint(val name: String)

@Target(AnnotationTarget.TYPE)
annotation class JsonObjectKey(val name: String)

These are used like so

@Endpoint("my-endpoint")
fun myEndpoint(): @JsonObjectKey("key") String {
    return "value"
}

With a method: java.lang.reflect.Method object representing myEndpoint,
I am able to access the Endpoint-Annotation, but I fail to access the JsonObjectKey-Annotatation.

method.annotations only contains Endpoint

method.annotatedReturnType.annotations is empty

How to read JsonObjectKey (key in this scenario)?
Intention is to generate JsonObject {"key": "value"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2025-02-13 03:39:04

使用Kotlin反射API!

例如,此功能:

fun main() {
    println(::myEndpoint.returnType.annotations)
}

@Target(AnnotationTarget.TYPE)
annotation class JsonObjectKey(val name: String)

fun myEndpoint(): @JsonObjectKey("key") String {
    return "value"
}

此输出:

[@JsonObjectKey(name=key)]

如果您只有 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”

println(someJavaMethod?.kotlinFunction?.returnType?.annotations)

看来,在Java中以此位置注释的注释与

public static @JsonObjectKey(name = "foo") String foo()

Kotlin中该位置的注释的注释大不相同:

fun foo(): @JsonObjectKey("key") String = ""

这些似乎是两个不同的位置。 Kotlin反射无法看到Java返回类型上的注释,Java反射无法看到Kotlin返回类型上的注释。

比较注释在类文件中的出现。对于Java方法,注释出现在 RuntimeVisibleTypeAntiations 属性:

RuntimeVisibleTypeAnnotations:
  0: #23(#24=s#20): METHOD_RETURN
    JsonObjectKey(
      name="foo"
    )

另一方面,Kotlin方法将注释存储为 kotlin.metadata 注释的一部分:

RuntimeVisibleAnnotations:
  0: #86(#87=[I#88,I#89,I#90],#91=I#92,#93=I#94,#95=[s#96],#97=[s#5,s#98,s#76,s#98,s#99,s#100,s#101,s#102])
    kotlin.Metadata(
      mv=[1,6,0]
      k=2
      xi=48
      d1=["..."]
      d2=["main","","myEndpoint","","LJsonObjectKey;","name","key","myproject"]
    )

Use the Kotlin reflection API!

For example, this works:

fun main() {
    println(::myEndpoint.returnType.annotations)
}

@Target(AnnotationTarget.TYPE)
annotation class JsonObjectKey(val name: String)

fun myEndpoint(): @JsonObjectKey("key") String {
    return "value"
}

This outputs:

[@JsonObjectKey(name=key)]

If you only have a java.lang.reflect.Method for some reason, you can convert it to a KFunction using kotlinFunction:

println(someJavaMethod?.kotlinFunction?.returnType?.annotations)

It appears that annotations annotated in this position in Java:

public static @JsonObjectKey(name = "foo") String foo()

are very different from those annotated in this position in Kotlin:

fun foo(): @JsonObjectKey("key") String = ""

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:

RuntimeVisibleTypeAnnotations:
  0: #23(#24=s#20): METHOD_RETURN
    JsonObjectKey(
      name="foo"
    )

On the other hand, the Kotlin method instead has the annotation stored as part of the kotlin.Metadata annotation:

RuntimeVisibleAnnotations:
  0: #86(#87=[I#88,I#89,I#90],#91=I#92,#93=I#94,#95=[s#96],#97=[s#5,s#98,s#76,s#98,s#99,s#100,s#101,s#102])
    kotlin.Metadata(
      mv=[1,6,0]
      k=2
      xi=48
      d1=["..."]
      d2=["main","","myEndpoint","","LJsonObjectKey;","name","key","myproject"]
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文