Java 不同的保留策略如何影响我的注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3107970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:30:38  来源:igfitidea点击:

How do different retention policies affect my annotations?

javaannotations

提问by xdevel2000

Can anyone explain in a clear way the practical differences between the java.lang.annotation.RetentionPolicyconstants SOURCE, CLASS, and RUNTIME?

以明确的方式有谁能解释之间的实际差别java.lang.annotation.RetentionPolicy常数SOURCECLASSRUNTIME

I'm also not exactly sure what the phrase "retaining annotation" means.

我也不确定“保留注释”这个词是什么意思。

采纳答案by Favonius

  • RetentionPolicy.SOURCE: Discard during the compile. These annotations don't make any sense after the compile has completed, so they aren't written to the bytecode.
    Example: @Override, @SuppressWarnings

  • RetentionPolicy.CLASS: Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default.

  • RetentionPolicy.RUNTIME: Do not discard. The annotation should be available for reflection at runtime. Example: @Deprecated

  • RetentionPolicy.SOURCE: 在编译过程中丢弃。编译完成后,这些注释没有任何意义,因此它们不会写入字节码。
    示例:@Override,@SuppressWarnings

  • RetentionPolicy.CLASS: 在类加载期间丢弃。在进行字节码级别的后处理时很有用。有点令人惊讶的是,这是默认设置。

  • RetentionPolicy.RUNTIME: 不要丢弃。注释应该在运行时可用于反射。例子:@Deprecated

Source:The old URL is dead now hunter_metaand replaced with hunter-meta-2-098036. In case even this goes down, I am uploading the image of the page.

来源:旧的 URL 现在已经失效了 hunter_meta并替换为hunter-meta-2-098036。万一出现这种情况,我正在上传页面的图像。

Image(Right Click and Select 'Open Image in New Tab/Window')Screenshot of Oracle website

图像(右键单击并选择“在新选项卡/窗口中打开图像”)Oracle 网站截图

回答by ewernli

According to your comments about class decompilation, here is how I think it should work:

根据您对类反编译的评论,我认为它应该如何工作:

  • RetentionPolicy.SOURCE: Won't appear in the decompiled class

  • RetentionPolicy.CLASS: Appear in the decompiled class, but can't be inspected at run-time with reflection with getAnnotations()

  • RetentionPolicy.RUNTIME: Appear in the decompiled class, and can be inspected at run-time with reflection with getAnnotations()

  • RetentionPolicy.SOURCE: 不会出现在反编译的类中

  • RetentionPolicy.CLASS: 出现在反编译的类中,但不能在运行时用反射进行检查 getAnnotations()

  • RetentionPolicy.RUNTIME: 出现在反编译的类中,可以在运行时用反射进行检查 getAnnotations()

回答by Ferdous Wahid

Retention Policy: A retention policy determines at what point an annotation is discarded. It is s specified using Java's built-in annotations: @Retention[About]

保留策略:保留策略确定在什么时候丢弃注释。它是使用 Java 的内置注释指定的:@Retention[关于]

1.SOURCE: annotation retained only in the source file and is discarded
          during compilation.
2.CLASS: annotation stored in the .class file during compilation,
         not available in the run time.
3.RUNTIME: annotation stored in the .class file and available in the run time.

回答by Michael Wong

  • CLASS:Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
  • RUNTIME:Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
  • SOURCE:Annotations are to be discarded by the compiler.
  • CLASS:注解会被编译器记录在类文件中,但在运行时不需要被VM保留。
  • RUNTIME:注解会被编译器记录在class文件中,并在运行时由VM保留,因此可以反射读取。
  • SOURCE:注释将被编译器丢弃。

Oracle Doc

甲骨文文档