gpt4 book ai didi

kotlin - Annotation Retention.BINARY 的用例是什么?

转载 作者:行者123 更新时间:2023-12-04 13:26:42 27 4
gpt4 key购买 nike

根据Kotlin language spec ,有三种类型的注释保留:

  • Source retention (accessible by source-processing tools);
  • Binary retention (retained in compilation artifacts);
  • Runtime retention (accessible at runtime).

查看 JVM 上 kotlin-stdlib 的 KDoc,我们得到以下信息:
public enum class AnnotationRetention {
/** Annotation isn't stored in binary output */
SOURCE,
/** Annotation is stored in binary output, but invisible for reflection */
BINARY,
/** Annotation is stored in binary output and visible for reflection (default retention) */
RUNTIME
}
我能理解 SOURCE 的用例(可供注释处理器检查)和 RUNTIME (可使用反射进行检查)但我无法理解 BINARY 的用例.
有人可以解释一下这种类型的保留的用例吗?我为什么会选择 BINARYSOURCE如果我不需要 RUNTIME ?

最佳答案

根据 Alexey Romanov 的评论,有一个注释处理和运行时反射之外的注释用例,如 Java Annotations - looking for an example of RetentionPolicy.CLASS 的答案中所示。
例如,Proguard 需要 .jar文件并按照 Proguard website 中的图表对它们进行混淆和缩小。以下:
Proguard pipeline including input jars, shrinking, then output jars@Keep注释告诉 Proguard 在收缩期间不要移除目标。由于 Proguard 在 .jar 上运行,因此二进制文件中必须存在此类注释。文件。自 @Keep不打算在运行时使用反射进行检查,这将是 AnnotationRetention.BINARY 的合适用例.

关于kotlin - Annotation Retention.BINARY 的用例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68027227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com