注释源保留策略 [英] Annotation SOURCE Retention Policy

查看:33
本文介绍了注释源保留策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Java 文档:

From the Java doc:

课程:注释将由编译器记录在类文件中,但在运行时不需要由 VM 保留.

CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

运行时间:注解将被编译器记录在类文件中,并在运行时由 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.

来源:注解会被编译器丢弃.

SOURCE: Annotations are to be discarded by the compiler.

我了解 RUNTIME(为了使用带有反射的注释)和 CLASS(用于编译器)的用法,但我不明白什么时候可以使用

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

@Retention(RetentionPolicy.SOURCE)

@Retention(RetentionPolicy.SOURCE)

你能解释一下吗?

推荐答案

诸如@SuppressWarnings、@Override 之类的东西是编译器使用的注释——在运行时不需要.对于那些 RetentionPolicy.SOURCE 会有意义.也可以使用注解来生成代码(查看 Spring ROO)——在运行时也不需要这样的注解.

Things like @SuppressWarnings, @Override are annotations used by the compiler - not needed at runtime. For those RetentionPolicy.SOURCE would make sense. Also annotations can be used to generate code (look at Spring ROO) - such annotation are also not required at run time.

这篇关于注释源保留策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆