深入理解Java注解

越大的項目,使用注解就越清晰,代碼可讀性越高,維護起來就越簡單。簡單來說,通過注解,可以使我們的開發(fā)更方便簡潔,通過規(guī)范約束我們的編程,避免一些不必要的錯誤,增強代碼的描述性等。缺點就是理解起來有一定的難度,別人前期需要花費一些時間來理解這些注解。

總體概覽
注解的作用或者意義是什么

注解本身沒有任何的意義,單獨的注解就是一種注釋,他需要結(jié)合其他如反射、注解處理器、插樁等技術(shù)才有意義。

注解的分類

根據(jù)注解的使用場景,主要分為三類,元注解、內(nèi)置注解和自定義注解。

元注解

用于定義注解的注解,通常用于注解的定義上,標(biāo)明該注解的使用范圍、生效范圍等。

元數(shù)據(jù)Annotation:包含成員變量的Annotation。默認(rèn)值可用default設(shè)置。
public @interface MyAno {
    String name() default "angela";
    int age() default 18;
}

使用元數(shù)據(jù)需要設(shè)置默認(rèn)值

@MyAno(name = "范冰冰", age = 15)
public class UserAno {

}
@Retention

指定注解信息保留到哪個階段,分別為源代碼階段、編譯Class階段、運行階段。

@Retention包含一個名為“value”的成員變量,該value成員變量是RetentionPolicy枚舉類型。

1.RetentionPolicy.SOURCE:Annotation只保留在源代碼中,編譯器編譯時,直接丟棄這種Annotation。
2.RetentionPolicy.CLASS:編譯器把Annotation記錄在class文件中。在編譯時保存,當(dāng)運行Java程序時,JVM中不再保留該Annotation。
3.RetentionPolicy.RUNTIME:編譯器把Annotation記錄在class文件中。當(dāng)運行Java程序時,JVM會保留該Annotation,程序可以通過反射獲取該Annotation的信息。

三種保留級別應(yīng)用場景:
1.APT處理注解發(fā)生在編譯器。
聲明注解,限定參數(shù)傳入類型,注解設(shè)置為源碼級別。
@IntDef(value = {1, 2, 3, 4, 5})
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
@interface Type {

}

public void setType(@Type int type) {

}
2.字節(jié)碼增強

說人話就是在字節(jié)碼中寫代碼。

@Target

不設(shè)置target可用于各種類型作為注解,也可同時設(shè)置多個target。
指定Annotation用于修飾哪些程序元素。@Target也包含一個名為”value“的成員變量ElementType為枚舉類型.

ElementType.TYPE:能修飾類、接口或枚舉類型
ElementType.FIELD:能修飾成員變量
ElementType.METHOD:能修飾方法
ElementType.PARAMETER:能修飾參數(shù)
ElementType.CONSTRUCTOR:能修飾構(gòu)造器
ElementType.LOCAL_VARIABLE:能修飾局部變量
ElementType.ANNOTATION_TYPE:能修飾注解
ElementType.PACKAGE:能修飾包

內(nèi)置注解
######@Documented 
將此注解包含在 javadoc 中 ,它代表著此注解會被javadoc工具提取成文檔。
 
######@Inherited
指定Annotation具有繼承性。

######@Deprecated 
用于表示某個程序元素(類、方法等)已過時 

######@SuppressWarning
用來關(guān)閉編譯器輸出的警告信息
示例一:自定義注解簡單例子:

自定義注解,執(zhí)行在運行階段,該注解用于修飾方法

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String name();
    int age() default 20;
    String phone();
}

在Person類中的方法里使用自定義注解,并設(shè)置初始值

public class Person {

    @MyAnnotation(name = "小王",age = 25,phone = "110")
    public void getInfo() {

    }

    public static void getAnnoValue() {
        try {
            MyAnnotation myAnnotation = Person.class.getMethod("getInfo").getAnnotation(MyAnnotation.class);
            System.out.println(myAnnotation.name() + "  " + myAnnotation.age() + "   " + myAnnotation.phone());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
使用反射操作注解實戰(zhàn)

定義一個注解的注解,再定義一個能用在各種元素上的注解

/**
 * 定義target是注解的注解
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@interface AnnoTest {
    String value() default "anno";
}

/**
 * 定義一個幾乎全量信息的注解
 */
@AnnoTest("annotest")
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.FIELD, ElementType.TYPE, ElementType.METHOD,
        ElementType.PACKAGE, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Documented
@interface FullAnnoTest {
    String value() default "FullAnnoTest";
}

測試類和反射代碼:在各種元素前添加FullAnnoTest注解和對應(yīng)的value值,然后通過反射獲取各處的注解value。

class ParentObj {
}

@FullAnnoTest("class")
public class TestAnnoReflect<@FullAnnoTest("parameter") T> extends @FullAnnoTest("parent") ParentObj {

    //注解字段域
    private @FullAnnoTest("name") String name;

    //注解泛型字段域
    private @FullAnnoTest("value") T value;

    //注解通配符
    private @FullAnnoTest("list") List<@FullAnnoTest("generic") ?> list;

    @FullAnnoTest("constructor")
    public TestAnnoReflect() {
    }

    //注解方法
    @FullAnnoTest("method")             //注解方法參數(shù)
    public String hello(@FullAnnoTest("methodParameter") String name) throws @FullAnnoTest("Exception") Exception {  //注解異常拋出
        //注解局部變量
        @FullAnnoTest("result") String result;
        result = "result";
        System.out.println(result);
        return result;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void testAnnotation() throws NoSuchMethodException {
        TestAnnoReflect<String> testAnnoReflect = new TestAnnoReflect<>();
        Class<TestAnnoReflect<Object>> clazz = (Class<TestAnnoReflect<Object>>) testAnnoReflect.getClass();

        FullAnnoTest annotation;
        Field[] fields = clazz.getDeclaredFields();
        Annotation[] annotations = clazz.getAnnotations();

        //獲取class的注解
        annotation = (FullAnnoTest)annotations[0];
        System.out.println("修飾TestAnnoReflect類的注解value: " + annotation.value());

        //獲取構(gòu)造器的 注解
        Constructor<TestAnnoReflect<Object>> constructor = (Constructor<TestAnnoReflect<Object>>) clazz.getDeclaredConstructors()[0];
        annotation = constructor.getAnnotation(FullAnnoTest.class);
        System.out.println("構(gòu)造器的注解value: " + annotation.value());

        //獲取注解的 注解
        Class<? extends Annotation> annotationType = annotation.annotationType();
        AnnoTest annoTest = annotationType.getAnnotation(AnnoTest.class);
        System.out.println("修飾注解的注解AnnoTest-value: " + annoTest.value());

        //獲取方法的 注解
        Method method = clazz.getDeclaredMethod("hello", String.class);
        annotation = method.getAnnotation(FullAnnoTest.class);
        System.out.println("修飾方法的注解value: " + annotation.value());

        //獲取方法參數(shù)的 注解
        Parameter parameter = method.getParameters()[0];
        annotation = parameter.getAnnotation(FullAnnoTest.class);
        System.out.println("方法參數(shù)的注解value: " + annotation.value());

        //獲取方法拋出的異常上的 注解
        Class<?>[] exceptionTypes = method.getExceptionTypes();
        for (Class<?> exceptionType: exceptionTypes) {
            annotation = exceptionType.getAnnotation(FullAnnoTest.class);
            if (annotation != null) {
                System.out.println("方法拋出的異常上的注解value: " + annotation.value());
            }
        }

        //獲取包的 注解
        Package p = Package.getPackage("com.example.genericannotaionreflect.annotate");
        annotation = p.getAnnotation(FullAnnoTest.class);
        if (annotation != null) {
            System.out.println("修飾package的注解value: " + annotation.value());
        }

        //獲取各個屬性的注解 和 屬性類型的通配符注解,即 List<@FullAnnoTest("generic") ?>的注解
        for (Field field : fields) {
            FullAnnoTest fieldAnnotation = field.getAnnotation(FullAnnoTest.class);
            if (fieldAnnotation != null) {
                System.out.println("Field " + field.getName() + " Annotation Value: " + fieldAnnotation.value());

                // 獲取字段的泛型類型
                Type genericType = field.getGenericType();
                if (genericType instanceof ParameterizedType) {
                    ParameterizedType parameterizedType = (ParameterizedType) genericType;

                    // 獲取泛型參數(shù)的實際類型
                    Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                    for (Type typeArgument : actualTypeArguments) {
                        if (typeArgument instanceof Class) {
                            Class<?> genericClass = (Class<?>) typeArgument;

                            // 獲取泛型參數(shù)上的注解
                            FullAnnoTest genericAnnotation = genericClass.getAnnotation(FullAnnoTest.class);
                            if (genericAnnotation != null) {
                                System.out.println("Generic Type Annotation Value: " + genericAnnotation.value());
                            }
                        }
                    }
                }
            }
        }
    }

}

打?。?/p>

修飾TestAnnoReflect類的注解value: class
構(gòu)造器的注解value: constructor
修飾注解的注解AnnoTest-value: annotest
修飾方法的注解value: method
方法參數(shù)的注解value: methodParameter
Field list Annotation Value: list
Field name Annotation Value: name
Field value Annotation Value: value
Generic Type Annotation Value: generic
其中用到field的泛型獲取技術(shù):

field.getGenericType()方法:
getGenericType()是Field接口的一個方法,它返回一個Type對象,表示字段的通用類型,包括泛型信息。
可能是Class、ParameterizedType、GenericArrayType等,例如List<String> list,list屬性的getGenericType()得到的是List<String>類型的Type。

  • Class類型 表示普通類型

  • ParameterizedType 表示一個參數(shù)化類型,即帶有實際類型參數(shù)的泛型類型。 對于 List<String>,Map<Integer, String> 等,它們都屬于 ParameterizedType。

  • GenericArrayType 表示數(shù)組類型,其中的元素類型是參數(shù)化類型或者是類型變量。對于 List<String>[] 或者 T[](其中 T 是類型變量),它們都屬于 GenericArrayType。

getActualTypeArguments()方法
是ParameterizedType接口的一個方法,用于獲取泛型類型的實際類型參數(shù)。在Java中,泛型類型通常指的是參數(shù)化類型,比如List<String>,其中List是泛型類型,而String是它的實際類型參數(shù),該方法返回的是數(shù)組。

  • 使用注解來代替枚舉類

雖然推薦使用枚舉類替代一些int常量,但是在android中,官方還是不建議使用枚舉類的:

Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.

由官網(wǎng)的描述可知,枚舉類還是很耗費內(nèi)存的,所以更加優(yōu)雅的方式應(yīng)該是使用注解的形式。官方使用兩個注解類,IntDef和StringDef,用來提供枚舉的替代方案。

class OrderState {

    companion object {
        //Android端制定訂單狀態(tài),分別為全部,待支付, 待提貨, 已完成, 已取消
        const val ALL = 0
        const val WAIT_PAY = 1
        const val WAIT_DILIVERY = 2
        const val COMPLETED = 3
        const val CANCELLED = 4

        /**
         * 根據(jù)訂單列表頁獲取訂單頁請求狀態(tài)
         */
        fun getOrderListStatus(@OrderState status: Int): Int = when (status) {
            ALL ->
                ...
            WAIT_PAY ->
                ...
            WAIT_DILIVERY ->
                ...
            COMPLETED ->
                ...
            CANCELLED ->
                ...
            else -> STATUS_ALL
        }
    }

    @IntDef(ALL, WAIT_PAY, WAIT_DILIVERY, COMPLETED, CANCELLED)
    @Retention(RetentionPolicy.SOURCE)
    internal annotation class OrderState
}
注解的本質(zhì)
public interface Annotation {
    boolean equals(Object obj);
    int hashCode();
    String toString();
    /**
     * Returns the annotation type of this annotation.
     * @return the annotation type of this annotation
     */
    Class<? extends Annotation> annotationType();
}

注解本質(zhì)上它是繼承自Annotation的一個接口。但我們是可以通過反射拿到Annotation實例的,實際上,我們在運行期獲取到的注解,都是接口的代理類。

注解的實際過程:

? 注解實質(zhì)上會被編譯器編譯為接口,并且繼承java.lang.annotation.Annotation接口。
? 注解的成員變量會被編譯器編譯為同名的抽象方法。
? 根據(jù)Java的class文件規(guī)范,class文件中會在程序元素的屬性位置記錄注解信息。

Kotlin注解與Java注解有什么不同?

Kotlin 的注解完全兼容 Java 的注解。

參考:
https://developer.aliyun.com/article/1114687

Github代碼地址:

https://github.com/running-libo/GenericAnnotationReflect

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是注解(Annotation):Annotation(注解)就是Java提供了一種元程序中的元素關(guān)聯(lián)任何信息和...
    九尾喵的薛定諤閱讀 3,428評論 0 2
  • 本文章涉及代碼已放到github上annotation-study 1.Annotation為何而來 What:A...
    zlcook閱讀 29,797評論 15 116
  • 從JDK5開始,Java增加了Annotation(注解),Annotation是代碼里的特殊標(biāo)記,這些標(biāo)記可以在...
    CarlosLynn閱讀 667評論 0 2
  • 從JDK5開始,Java增加了Annotation(注解),Annotation是代碼里的特殊標(biāo)記,這些標(biāo)記可以在...
    lay_wn閱讀 995評論 0 1
  • java自定義注解 Java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運行時進行解析和使用,起到說明、配...
    尼爾君閱讀 565評論 0 0

友情鏈接更多精彩內(nèi)容