java元注解
一、注解
反射来获取指定程序中元素的 Annotation对象,然后通过该Annotation 对象来获取注解中的元数据信息。二、元注解
三、各个元注解的作用
1、@Target 指定一个注解的使用范围
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
   
    ElementType[] value();
    
}public enum ElementType {
    /** 类、接口(包括注解类型),或者枚举声明 */
    TYPE,
    /** 字段声明(包括枚举常量) */
    FIELD,
    /** 方法声明 */
    METHOD,
    /** 形参声明 */
    PARAMETER,
    /** 构造方法声明 */
    CONSTRUCTOR,
    /** 局部变量声明 */
    LOCAL_VARIABLE,
    /** 注解类型声明 */
    ANNOTATION_TYPE,
    /** 包声明 */
    PACKAGE,
    /**  类型参数声明 */
    TYPE_PARAMETER,
    
    /** 类型的使用 */
    TYPE_USE
}2、@Retention 描述注解保留的时间范围
即被描述的注解在它所修饰的类中可以被保留到何时
@Retention注解源码:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
   
    RetentionPolicy value();
    
}他的value是 java.lang.annotation.RetentionPolicy 枚举类型。
public enum RetentionPolicy {
   
   	//源文件保留
    SOURCE,
  	//编译期保留,默认值
    CLASS,
  	//运行期保留,可通过反射去获取注解信息
    RUNTIME
    
} 生命周期大小排序为 SOURCE < CLASS < RUNTIME,前者能使用的地方后者一定也能使用。
如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解,如 @Documented 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码,就用 CLASS 注解,如 @NonNull 注解;如果只是做一些检查性的操作,则可选用 SOURCE 注解,如 @Override 和 @SuppressWarnings 注解。
3、@Documented 描述javadoc
描述在使用javadoc工具为类生成帮助文档时是否要保留其注解信息。
@Documented注解源码:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}@Documented 是一个标记注解,没有成员变量。用 @Documented 注解修饰的注解类会被 JavaDoc 工具提取成文档。默认情况下,JavaDoc 是不包括注解的,但如果声明注解时指定了 @Documented,就会被 JavaDoc 之类的工具处理,所以注解类型信息就会被包括在生成的帮助文档中。
4、@Inherited 阐述了某个被标注的类型是被继承的
@Inherited 是一个标记注解,用来指定该注解可以被继承。如果某个类使用了被@Inherited修饰的注解,则其子类将自动具有该注解。
举个例子:
注解
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyInheritedAnnotation {
    public String name() default "liuliu";
}父类
@MyInheritedAnnotation(name = "parent")
public class Parent {
}子类
public class Child extends Parent {
    public static void main(String[] args) {
        Class<Child> child = Child.class;
        MyInheritedAnnotation annotation = child.getAnnotation(MyInheritedAnnotation.class);
        System.out.println(annotation.name());
    }
}输出:
parent
5、@Repeatable注解
@Repeatable 是Java 8 新增,@Repeatable注解用于声明标记的注解为可重复类型注解,可以在同一个地方多次使用。Java 8 版本以前,同一个程序元素前最多只能有一个相同类型的注解,如果需要在同一个元素前使用多个相同类型的注解,则必须使用注解“容器”。
@Repeatable 注解源码:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
    
    Class<? extends Annotation> value();
    
}举个例子
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRepeatableAnnotation {
    RepeatableAnnotationTest[] value();
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyRepeatableAnnotation.class)
public @interface RepeatableAnnotationTest {
    String key();
    String value();
}@RepeatableAnnotationTest(key = "aa",value = "1a")
@RepeatableAnnotationTest(key = "bb",value = "2b")
public class RepeatableTest {
    public static void main(String[] args) {
        RepeatableAnnotationTest[] annotation = RepeatableTest.class.getAnnotation(MyRepeatableAnnotation.class).value();
        for (RepeatableAnnotationTest a : annotation) {
            System.out.println(a);
        }
    }
}输出:
@annotation.RepeatableAnnotationTest(key=aa, value=1a)
@annotation.RepeatableAnnotationTest(key=bb, value=2b)
6.@Native注解
@Native注解修饰成员变量,则表示这个变量可以被本地代码引用,常常被代码生成工具使用。对于@Native注解不常使用,了解即可。
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号