[英]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.