java元注解

一、注解

Annotation(注解)是 Java 提供的一种对元程序中元素关联信息和元数据(metadata)的途径和方法。

Annatation(注解)是一个接口,程序可以通过反射来获取指定程序中元素的 Annotation对象,然后通过该Annotation 对象来获取注解中的元数据信息。

二、元注解

元注解的作用是负责对其他注解进行说明的注解。自定义注解时可以使用元注解。Java 5 定义了 4 个注解,分别是 @Documented、@Target、@Retention 和 @Inherited;Java 8 又增加了 @Repeatable 和 @Native 两个注解。可以在java.lang.annotation包中找到。

三、各个元注解的作用

1、@Target 指定一个注解的使用范围

即被 @Target 修饰的注解可以用在什么地方

@Target源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
   
    ElementType[] value();
    
}

@Target 注解有一个成员变量(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注解不常使用,了解即可。

  • 16
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值