Java 注解使用

@Target 定义Annotation能够被应用于源码的哪些位置:

  1. ElementType.TYPE:类或接口
  2. ElementType.FIELD:字段
  3. ElementType.METHOD:方法
  4. ElementType.CONSTRUCTOR:构造方法
  5. ElementType.PARAMETER:方法参数

@Retention 定义了Annotation的生命周期:

  1. RetentionPolicy.SOURCE:仅编译期
  2. RetentionPolicy.CLASS:仅class文件
  3. RetentionPolicy.RUNTIME:运行期

自定义注解,可在类,接口;构造方法;方法;属性;方法参数上使用
@Retention(RetentionPolicy.RUNTIME)可以在运行期通过反射读取RUNTIME类型的注解,否则运行期无法读取到该注解

@Target({
        ElementType.TYPE,
        ElementType.CONSTRUCTOR,
        ElementType.METHOD,
        ElementType.FIELD,
        ElementType.PARAMETER
})
@Retention(RetentionPolicy.RUNTIME)
public @interface Alias {
    String name() default "";
    String value() default "";
    String comment() default "";
}

测试用学生类

@Alias(name = "student", comment = "学生类")
public class Student {

    @Alias(name = "Name", comment = "名称")
    public String name;

    @Alias("Age")
    private Integer age;

    @Alias(name = "student", comment = "student构造方法")
    public Student (String name) {
        this.name = name;
    }

    @Alias(name = "GetAge", comment = "获取age属性")
    public Integer getAge() {
        return age;
    }

    @Alias(name = "SetAge", comment = "赋值age属性")
    public void setAge(@Alias("Age") Integer age) {
        this.age = age;
    }
}

处理注解,获取注解值

@Test
public void getClassAlias() {
    try {
        Class<?> cls = Class.forName("com.shpun.annotation.Student");
        Annotation[] annotations = cls.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof Alias) {
                Alias alias = (Alias) annotation;
                String name = alias.name();
                String value = alias.value();
                String comment = alias.comment();

                System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void getConstructorAlias() {
    try {
        Class<?> cls = Class.forName("com.shpun.annotation.Student");
        Constructor<?> constructor = cls.getDeclaredConstructor(String.class);
        Annotation[] annotations = constructor.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof Alias) {
                Alias alias = (Alias) annotation;
                String name = alias.name();
                String value = alias.value();
                String comment = alias.comment();

                System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void getFieldAlias() {
    try {
        Class<?> cls = Class.forName("com.shpun.annotation.Student");
        Field[] fields = cls.getFields();
        for (Field field : fields) {
            Annotation[] annotations = field.getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof Alias) {
                    Alias alias = (Alias) annotation;
                    String name = alias.name();
                    String value = alias.value();
                    String comment = alias.comment();

                    System.out.println(field.getName() + " " + name + " " + value + " " + comment);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void getMethodAlias() {
    try {
        Class<?> cls = Class.forName("com.shpun.annotation.Student");
        Method[] methods = cls.getMethods();
        for (Method method : methods) {
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof Alias) {
                    Alias alias = (Alias) annotation;
                    String name = alias.name();
                    String value = alias.value();
                    String comment = alias.comment();

                    System.out.println(method.getName() + " " + name + " " + value + " " + comment);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void getParamAlias() {
    try {
        Class<?> cls = Class.forName("com.shpun.annotation.Student");
        Method[] methods = cls.getMethods();
        for (Method method : methods) {
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (Annotation[] annotations : parameterAnnotations) {
                for (Annotation annotation : annotations) {
                    if (annotation instanceof Alias) {
                        Alias alias = (Alias) annotation;
                        String name = alias.name();
                        String value = alias.value();
                        String comment = alias.comment();

                        System.out.println(cls.getName() + " " + name + " " + value + " " + comment);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

参考:
注解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值