精通Java注解(4)- 指示编译器如何处理注解

119 阅读1分钟

可以通过指定Retention指示Java编译器对待注解的行为。默认级别为:CLASS。 以下是所有Retention级别:

策略官方释义中文释义备注
SOURCEAnnotations are to be discarded by the compiler.编译时丢弃最低级别
CLASSAnnotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior.编译时保留,运行时丢弃默认级别
RUNTIMEAnnotations 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.运行时保留最高级别