1. 程式人生 > >java成神之——註釋修飾符

java成神之——註釋修飾符

錯誤 list ide pac 修飾符 文件中 ati extend value

  • 註釋修飾符
    • 自定義註釋
      • 元註釋
      • 通過反射在runtime訪問註釋
    • 內置註釋
    • 多註釋實例
      • 錯誤寫法
      • 使用容器改寫
      • 使用@Repeatable元註釋
    • 註釋繼承
    • 使用反射獲取註釋
      • 獲取類的註釋
      • 獲取方法的註釋
    • 結語

註釋修飾符

自定義註釋

元註釋

用來註釋自定義註釋的註釋

@Target 限定註釋允許加載的目標
    @Target(ElementType.METHOD)                     只能用於方法
    @Target({ElementType.FIELD, ElementType.TYPE})  可以用於字段和類型

    ANNOTATION_TYPE                                 註解其他的註釋
    CONSTRUCTOR                                     註解構造函數
    FIELD                                           註解字段和枚舉常量
    LOCAL_VARIABLE                                  註解局部變量
    PACKAGE
    METHOD                                          註解方法
    PARAMETER                                       註解方法參數
    TYPE                                            註解class,interface,enum
    TYPE_PARAMETER                                  註解泛型參數
    TYPE_USE                                        註解強轉類型

@Retention 設置註解在程序runtime時期能否被反射訪問到 
    @Retention(RetentionPolicy.RUNTIME)             運行反射訪問

    CLASS                                           值運行在class文件中訪問,而不是runtime
    RUNTIME
    SOURCE                                          能夠在compiletime配訪問到,但是不會添加到class文件中

@Documented 讓文檔生成器能夠識別

@Inherited 這個註釋用來改變註釋查詢方式,從而只用來檢查父類,直到發現目標

@Repeatable 多個註釋實例能夠被附加到註釋目標

通過反射在runtime訪問註釋

聲明註釋
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
    String param1() default "someValue";
    boolean param2() default true;
    int[] param3() default {};
}

使用註釋
@MyAnnotation
public static void Test() throws Exception {
    Annotation[] annotationsByType = MyAnnotation.class.getAnnotations();
    System.out.println("----------" + Arrays.toString(annotationsByType));
}

內置註釋

@Override                           重寫父類方法
@Deprecated                         廢棄api 
@SuppressWarnings("unchecked")      取消系統的某些警告信息,盡量少用
@SafeVarargs                        抑制可變參數警告
    @SafeVarargs
    public static<T> void Test(T... lists) throws Exception {
    }
@FunctionalInterface                用來聲明函數式接口,也就是只能包含一個方法
    @FunctionalInterface
    public interface Test<T> {
        boolean test(T t);
    }

多註釋實例

錯誤寫法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
    String param();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    String param() default "someValue";
}

報錯
@MyAnnotation1(param="someValue")
@MyAnnotation1(param="someValue") 

使用容器改寫

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
    String param();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    MyAnnotation1[] param();
}

@MyAnnotation2(param={
    @MyAnnotation1(param="someValue1"),
    @MyAnnotation1(param="someValue2"),
})

獲取註釋的值
MyAnnotation1[] MyAnnotations = TestController.class.getAnnotation(MyAnnotation2.class).param();
           
for (MyAnnotation1 item : MyAnnotations) {
    System.out.println(item.param());
}

使用@Repeatable元註釋

@Repeatable(MyAnnotation2.class)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    MyAnnotation1[] value();
}

@MyAnnotation1(value="someValue1")
@MyAnnotation1(value="someValue2")

獲取註釋值

MyAnnotation1[] MyAnnotations = TestController.class.getAnnotationsByType(MyAnnotation1.class);
for (MyAnnotation1 item : MyAnnotations) {
    System.out.println(item.value());
}

註釋繼承

添加了@Inherited,繼承的子類可以訪問父類的註釋

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

@MyAnnotation1(value="someValue1")
class Father {

}

public class Test extends Father{
    MyAnnotation1[] MyAnnotations = Test.class.getAnnotationsByType(MyAnnotation1.class);
    for (MyAnnotation1 item : MyAnnotations) {
        System.out.println(item.value()); 
    }
}

使用反射獲取註釋

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

獲取類的註釋

@MyAnnotation1(value="someValue1")
public class Test{}

MyAnnotation1 annotation = Test.class.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

獲取方法的註釋

@MyAnnotation1(value="someValue1")
public Object Test() throws Exception {}

Method method = TestController.class.getMethod("Test", null);
MyAnnotation1 annotation = (MyAnnotation1)method.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

結語

本文章是java成神的系列文章之一

如果你想知道,但是本文沒有的,請下方留言

我會第一時間總結出來並發布填充到本文

java成神之——註釋修飾符