zoukankan      html  css  js  c++  java
  • 自定义注解

    一、注解的基础

    1.注解的定义:Java文件叫做Annotation,用@interface表示。

    2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。

    3.注解的保留策略:

      @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含

      @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

      @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

    4.注解的作用目标:

      @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)               // 包

    5.注解包含在javadoc中:

      @Documented

    6.注解可以被继承:

      @Inherited

    7.注解解析器:用来解析自定义注解。

    二、自定义注解

    package com.anntation.demo;
    
    import java.lang.annotation.*;
    
    /**
     * 注解@Inteface标识
     */
    @Retention(RetentionPolicy.RUNTIME) // 指定作用域
    @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) // 指定标注的位置
    public @interface MyAnnotation {
        public String value() default ""; // 在属性上面赋值,也可称为方法声明,如果是单个属性值可以不用写
        public double version(); // 版本号
    }
    package com.anntation.demo;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME) // 指定作用域
    @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
    public @interface RequestParam {
        public String value() default ""; // 在属性上面赋值,也可称为方法声明,如果是单个属性值可以不用写
        public double version(); // 版本号
    }
    package com.anntation.demo;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    @MyAnnotation(value = "我的类", version =2.4 )
    public class MyClass {
    
        @MyAnnotation(version = 1.1,value = "我的字段")
        private String name;
    
        @MyAnnotation(version = 1.0, value = "我的方法")
        public void setName(int age,
            @RequestParam(version = 1.1,value = "张三")
            @MyAnnotation(value = "我的参数", version = 3.2) String name
        ) {
    
        }
    
        @MyAnnotation(version = 1.0, value = "我的方法")
        public void setName(float age,
                            @RequestParam(version = 1.1,value = "张三")
                            @MyAnnotation(value = "我的参数", version = 3.2) String name
        ) {
    
        }
    
        public static void main(String[] args) {
            // 注意注解必须结合反射使用
            Class cl = MyClass.class;
            System.out.println(cl);
            // 获取到类上面的注解
            MyAnnotation myAnnotation = (MyAnnotation) cl.getAnnotation(MyAnnotation.class);
            System.out.println(myAnnotation.value());
            System.out.println(myAnnotation.version());
            System.out.println("-----------------------------------------");
            try {
    
                String fieldName = "name";
                // 获取字段上的注解
                Field nameField = cl.getDeclaredField(fieldName);
                // 判断是否有MyAnnotation注解
                if (nameField.isAnnotationPresent(MyAnnotation.class)) {
                    // 获取到字段上面的注解
                    MyAnnotation myAnnotation1 = nameField.getAnnotation(MyAnnotation.class);
                    System.out.println(myAnnotation1.value());
                    System.out.println(myAnnotation1.version());
                } else {
                    System.out.println("name字段上没有MyAnnotation注解");
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            System.out.println("---------------------获取方法的注解--------------------");
            try {
                // 获取一个方法
                Method nameMethod = cl.getDeclaredMethod("setName",int.class,String.class);
                // 判断是否有MyAnnotation方法
                MyAnnotation annotation = nameMethod.getAnnotation(MyAnnotation.class);
    
                if (annotation != null) {
                    System.out.println(annotation.version());
                    System.out.println(annotation.value());
                } else {
                    System.out.println("方法上面不存在MyAnnotation注解");
                }
                System.out.println("====================获取参数的注解=====================");
    
                // 获取参数上面的注解
                // 把参数个数作为行来存储, 把参数上面注解个数作为列数来存储
                Annotation[][] annotations = nameMethod.getParameterAnnotations();
                /*for (Annotation[] an : annotations) {
                    System.out.println(Arrays.toString(an));
                   for (Annotation annotation1 : an) {
                      if (annotation1 instanceof RequestParam) {
                          RequestParam  requestParam= (RequestParam) annotation1;
                          System.out.println(requestParam.value());
                          System.out.println(requestParam.version());
                      }
    
                       if (annotation1 instanceof MyAnnotation) {
                           MyAnnotation  myAnnotation1= (MyAnnotation) annotation1;
                           System.out.println(myAnnotation1.value());
                           System.out.println(myAnnotation1.version());
                       }
                       System.out.println("____________________________________________");
                   }
                }*/
                System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                for (int i = 0; i < annotations.length; i++) {
                    System.out.println(Arrays.toString(annotations[i])+"*********"+annotations.length);
                    for (int j =0 ; j <annotations[i].length; j++) {
                        Annotation annotation1 = annotations[i][j];
                        System.out.println(annotation1+"====================");
                           /*if (annotation1 instanceof RequestParam) {
                               RequestParam  requestParam= (RequestParam) annotation1;
                               System.out.println(requestParam.value());
                               System.out.println(requestParam.version());
                           }
                           System.out.println("------------------------------------");
                           if (annotation1 instanceof MyAnnotation) {
                               MyAnnotation  myAnnotation1= (MyAnnotation) annotation1;
                               System.out.println(myAnnotation1.value());
                               System.out.println(myAnnotation1.version());
                           }*/
                    }
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    标准C程序设计七---17
    标准C程序设计七---16
    标准C程序设计七---15
    标准C程序设计七---14
    标准C程序设计七---13
    标准C程序设计七---12
    标准C程序设计七---11
    标准C程序设计七---10
    标准C程序设计七---07
    java常见文件操作
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10473410.html
Copyright © 2011-2022 走看看