简体   繁体   English

注释源保留策略

[英]Annotation SOURCE Retention Policy

From the Java doc:来自 Java 文档:

CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. CLASS:注释由编译器记录在 class 文件中,但不需要由 VM 在运行时保留。

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. RUNTIME:注释由编译器记录在 class 文件中,并在运行时由 VM 保留,因此可以反射式读取。

SOURCE: Annotations are to be discarded by the compiler. SOURCE:注释将被编译器丢弃。

I understand the usages of RUNTIME (in order to use annotation with reflection) and CLASS (for the compiler) but I don't understand when it can be usefull to use我了解 RUNTIME 的用法(以便使用带有反射的注释)和 CLASS(对于编译器)但我不明白什么时候可以使用它

@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)

Can you explain?你可以解释吗?

Things like @SuppressWarnings, @Override are annotations used by the compiler - not needed at runtime. 像@SuppressWarnings,@ Override这样的东西是编译器使用的注释 - 在运行时不需要。 For those RetentionPolicy.SOURCE would make sense. 对于那些RetentionPolicy.SOURCE会有意义。 Also annotations can be used to generate code (look at Spring ROO) - such annotation are also not required at run time. 注释也可用于生成代码(查看Spring ROO) - 在运行时也不需要这样的注释。

This answer makes perfect sense - https://stackoverflow.com/a/43910948/3009968 .这个答案非常有意义 - https://stackoverflow.com/a/43910948/3009968

You would not like to include a dependency, the desired effects of which are realized even before the code is compiled.您不希望包含一个依赖项,它的预期效果甚至在代码编译之前就已实现。 Eg @SuppressWarnings例如@SuppressWarnings

You would not like to include a dependency which is used by compiler to let's say generate code but not at all required during runtime.您不希望包含编译器用来生成代码但在运行时根本不需要的依赖项。 Eg as mentioned already in previous answer - spring roo .例如,如前一个答案中所述 - spring roo

RetentionPolicy.CLASS - The defined annotation will be stored in the .class file, but not available at runtime. RetentionPolicy.CLASS - 定义的注释将存储在.class文件中,但在运行时不可用。 This is the default retention policy if you do not specify any retention policy at all.如果您根本不指定任何保留策略,则这是默认保留策略。

RetentionPolicy.SOURCE - The defined annotation will be ignored by the compiler when building the code. RetentionPolicy.SOURCE - 编译器在构建代码时将忽略定义的注解。 So the annotation is only available in the source code, and not in the .class files, and not at runtime too.因此注释仅在源代码中可用,在.class文件中不可用,在运行时也不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注释保留策略:声明“SOURCE”或“CLASS”有什么真正的好处? - Annotation retention policy: what real benefit is there in declaring `SOURCE` or `CLASS`? 如果覆盖的保留策略是源,那么运行时多态如何工作 - How runtime polymorphism work if retention policy of override is source Java 编译器不允许在使用 JPMS 的项目中使用来自 `compileOnly` 模块的具有 `Source` 保留的注释 - The Java compiler does not allow usage of an annotation with `Source` retention from a `compileOnly` module for a project using JPMS 在具有运行时保留的对象上找不到注释 - Annotation not found on object with Runtime retention @Retention(RetentionPolicty.Source)和@Inherited - @Retention(RetentionPolicty.Source) and @Inherited 递归使用@Retention注释,怎么可能? - Recursive usage of @Retention annotation , how is it possible? 为什么@Documented注释具有运行时保留? - Why does @Documented annotation have runtime retention? 如何在Java中实现构建特定的注释保留 - How to implement build specific annotation retention in Java Java 源码中设置Kafka保留时间 - Java set Kafka retention time in source code 如何在使用 Spring 创建期间配置 kafka 主题保留策略? - How to configure kafka topic retention policy during creation with Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM