Java 註解使用

@Target 定義Annotation能夠被應用於源碼的哪些位置:

  • 類或接口:ElementType.TYPE
  • 字段:ElementType.FIELD
  • 方法:ElementType.METHOD
  • 構造方法:ElementType.CONSTRUCTOR
  • 方法參數:ElementType.PARAMETER

@Retention 定義了Annotation的生命週期:

  • 僅編譯期:RetentionPolicy.SOURCE
  • 僅class文件:RetentionPolicy.CLASS
  • 運行期: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();
    }
}

參考:
註解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章