元注解(meta-annotation)的作用就是负责注解其他注解,Java5定义了元注解类型,他们被用来提供对其它annotation类型做说明。
- @Target@Retention@documented@Inherited
这些类型和它们所支持的类在java.lang.annotation包中可以找到。
- @Target(用于描述注解的适用范围 {被描述的注解可以用在什么地方})
@Target说明了Annotation所修饰的对象范围:Annotation可用于 packages、type(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。
取值(ElementType):
ElementType.CONSTRUCTOR:用于描述构造器。ElementType.FLELD:用于描述域ElementType.LOCAL_VARIABLE:用于描述局部变量ElementType.METHOD:用于描述方法ElementType.PACKAGE:用于描述包ElementType.PARAMETER:用于描述参数ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明
使用示例:
@Target({ ElementType.PARAMETER, ElementType.METHOD }) public @interface Log { String value() default ""; }
- @Retention
@Retention表示需要在什么级别保存该注释信息,被描述的注解在什么范围内有效。
取值(RetentionPoicy):(来自java.lang.annotation.RetentionPolicy的枚举类型值)
RetentionPoicy.SOURCE:源码级别保留,编译后即丢弃。RetentionPoicy.CLASS:编译级别保留,编译后的class文件中存在,在jvm运行时丢弃,这是默认值。RetentionPoicy:运行级别保留,编译后的class文件中存在,在jvm运行时保留,可以被反射调用。
@Target({ ElementType.PARAMETER, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface Log { String value() default ""; }
- @documented
@documented 可以被例如 javadoc此类的工具文档化,documented是一个标注注解,没有成员。
@Target({ ElementType.PARAMETER, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @documented public @interface Log { String value() default ""; }
- @Inherited
@Inherited允许其他注解继承该注解。
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @documented @Inherited public @interface Log { String value() default ""; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)