注解和反射笔记

Dear 丶 2021-09-28 12:20 472阅读 0赞
  • 自定义两个注解

    @Target({ElementType.CONSTRUCTOR, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)//运行时加载Annotation 到JVM 中,只有此状态可通过反射获取注解的信息。
    @interface A {

    1. String value() default "默认构造方法";//成员

    }

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @interface B {

    1. String describe();
    2. Class<?> type() default void.class;

    }

  • 把以上两个注解来注解一个类

    @A
    public class Record {

    1. @B(describe="编号", type=int.class)
    2. int id;
    3. @B(describe="姓名", type=String.class)
    4. String name;
    5. @A()
    6. public Record() {
    7. }
    8. @A("立即初始化构造函数")
    9. public Record(
    10. @B(describe="编号", type=int.class) int id,
    11. @B(describe="姓名", type=String.class) String name) {
    12. this.id = id;
    13. this.name = name;
    14. }
    15. @B(describe="获取id", type=int.class)
    16. public int getId() {
    17. return id;
    18. }
    19. @B(describe="设置id", type=int.class)
    20. public void setId(@B(describe="编号", type=int.class) int id) {
    21. this.id = id;
    22. }
    23. @B(describe="获取姓名", type=String.class)
    24. public String getName() {
    25. return name;
    26. }
    27. @B(describe="设置姓名", type=String.class)
    28. public void setName(@B(describe="姓名", type=String.class) String name) {
    29. this.name = name;
    30. }

    }

  • 测试类

    public class TestAnnotation {

    1. public static void main(String[] args) throws Exception {
    2. Class<?> clazz = Record.class;
    3. System.out.println("类上是否有注解:" + clazz.isAnnotationPresent(A.class));
    4. A a = clazz.getAnnotation(A.class);
    5. System.out.println("A注解的value值:" + a.value());
    6. System.out.println("---------");
    7. System.out.println("属性上的注解:");
    8. Field[] fields = clazz.getDeclaredFields();
    9. for(Field f: fields) {
    10. System.out.println(f.getName() + ": ");
    11. Annotation[] annotations = f.getAnnotations();//与注解本身名字有何不同呢?其实就是多态
    12. for(Annotation annotation: annotations) {
    13. System.out.println(" " + annotation);
    14. }
    15. }
    16. System.out.println("---------");
    17. System.out.println("构造方法上的注解:");
    18. Constructor<?>[] constructors = clazz.getDeclaredConstructors();
    19. for (Constructor<?> constructor : constructors) {
    20. constructor.setAccessible(true);
    21. System.out.println(constructor);
    22. Annotation[] annotations = constructor.getAnnotations();
    23. for (Annotation annotation : annotations) {
    24. System.out.println(" " + annotation);
    25. }
    26. }
    27. System.out.println("---------");
    28. System.out.println("方法上的注解:");
    29. Method[] methods = clazz.getDeclaredMethods();
    30. for(Method m: methods) {
    31. System.out.println(m.getName() + ":");
    32. Annotation[] annotations = m.getDeclaredAnnotations();
    33. for(Annotation annotation: annotations) {
    34. System.out.println(" " + annotation);
    35. }
    36. }
    37. }

    }

  • 运行结果

    类上是否有注解:true

    A注解的value值:默认构造方法

    属性上的注解:
    id:

    1. @com.qhf.annotation.B(type=int, describe=编号)

    name:

    1. @com.qhf.annotation.B(type=class java.lang.String, describe=姓名)

    构造方法上的注解:
    public com.qhf.annotation.Record()

    1. @com.qhf.annotation.A(value=默认构造方法)

    public com.qhf.annotation.Record(int,java.lang.String)

    1. @com.qhf.annotation.A(value=立即初始化构造函数)

    方法上的注解:
    getName:

    1. @com.qhf.annotation.B(type=class java.lang.String, describe=获取姓名)

    getId:

    1. @com.qhf.annotation.B(type=int, describe=获取id)

    setName:

    1. @com.qhf.annotation.B(type=class java.lang.String, describe=设置姓名)

    setId:

    1. @com.qhf.annotation.B(type=int, describe=设置id)

发表评论

表情:
评论列表 (有 0 条评论,472人围观)

还没有评论,来说两句吧...

相关阅读

    相关 反射注解

    目录 一、反射——框架设计的灵魂 二、反射获取字节码Class对象的三种方式 三、Class对象的功能概述 四、注解 4.1 JDK中预定义的注解 1.限定父类重