java Annotation

  • java提供了Annotation功能,用于类、构造方法、成员变量、方法、参数的声明中
  • 定义Annotation
// 无参数的Annotation
public @interface NoAnnotationDemo {
}

// 一个参数的Annotation
public @interface OneAnnotationDemo {
    // String为成员类型,一般常用的有String,Class、primitive、enumerated、annotation
    // value()为成员名称,一般在只有一个成员时用value()
    String value();
}

// 多个参数的Annotation
public @interface MoreAnnotationDemo {
    // 为成员设置默认值
    String describe() default "默认值";
    Class type() default void.class;
}
  • Annotation元注解
1.@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
首 先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override@SuppressWarnings,则可选用 SOURCE 注解。


2.@Target:定义注解的作用目标
源码为:
@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.ANNOTATION_TYPE)  
// 多个Target时用数组
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
public @interface Target {  
    ElementType[] value();  
}  
@Target(ElementType.TYPE)   //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR)  //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包  

3.@Document:说明该注解将被包含在javadoc中
4.@Inherited:说明子类可以继承父类中的该注解
  • Annotation的使用
// 首先构建Annotation
@Target({ElementType.FIELD, ElementType.PARAMETER , ElementType.METHOD})
// 这里设置成为RUNTIME可以通过反射实现获取Annotation的信息
@Retention(RetentionPolicy.RUNTIME)
public @interface MoreAnnotationDemo {

    String describe() default "默认值";
    Class type() default void.class;
}

// 构建对象
public class Record {

    @MoreAnnotationDemo(describe = "编号", type = Integer.class)
    private int id;
    @MoreAnnotationDemo(describe = "姓名", type = String.class)
    private String name;
    @MoreAnnotationDemo(describe = "获取姓名", type = String.class)
    public String getName(){
        return this.name;
    }
    @MoreAnnotationDemo(describe = "获取id", type = Integer.class)
    public int getSumId(@MoreAnnotationDemo(describe = "编号", type = Integer.class) int num) {
        return id + num;
    }
}

// 反射获取Annotation相关信息
public class RecordDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        Record record = new Record();
        Class clazz = record.getClass();
        Constructor[] constructors = clazz.getDeclaredConstructors();
        Field[] fields = clazz.getDeclaredFields();

        for (Constructor constructor: constructors){
            if (constructor.isAnnotationPresent(ConstructorAnnotation.class)){
                ConstructorAnnotation constructorAnnotation = (ConstructorAnnotation) constructor.getAnnotation(ConstructorAnnotation.class);
                System.out.println(constructorAnnotation.describe());
            }
        }
        for (Field field: fields) {
            Annotation[] a = field.getAnnotations();
            for (int i = 0; i < a.length; i++) {
                MoreAnnotationDemo moreAnnotationDemo = (MoreAnnotationDemo) a[i];
                System.out.println(moreAnnotationDemo.describe());
                System.out.println(moreAnnotationDemo.type());

            }
        }
    }
}

// out
构造器
编号
class java.lang.Integer
姓名
class java.lang.String
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值