JDK注解(内置和自定义)
JDK注解(內置和自定義)
1.內置
3.@SuppressWarnings(value = “unchecked”)
//壓制警告(雖然可以使用SuppressWarning壓制警告,但不建議使用。)//忽略對泛型等的檢查操作。
其中的一些屬性和值:
-
value值:unchecked,deprecation(忽略一些過期的API),
-
unused(是否未被使用),
-
fallthrough(switch是否一致往下執行,而沒有break),
-
path(忽略對類路徑不存在的檢查),
-
serialversionUID(忽略一個類可以序列化,但卻沒有序列化的警告),
-
all,全部忽略
2.自定義
引入元注解:即修飾注解的注解
常用的幾個元注解:
(1)@Target:限制注解,使用位置(ElementType.枚舉)
限制注解能夠使用在屬性、方法、類中。如果一個注解沒有@Target描述,則可以修飾任何類型的元素;如果有@Target修飾,該注解只能用于被@Target修飾的地方
下圖見源碼:
上圖可知:使用時需要用ElementType來進行約束
ElementType:
ElementType就是用來約束使用的地方,和時間
@Rentention源碼:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention {/*** Returns the retention policy.* @return the retention policy*/RetentionPolicy value(); }@Document
javadoc:java幫助文檔。ABC.java->幫助文檔
默認情況下,Javadoc不包含注解的解釋,如果現在javadoc文檔中也包含對注解的說明,則需要使用@Document標注
Document源碼:
package java.lang.annotation;/*** Indicates that annotations with a type are to be documented by javadoc* and similar tools by default. This type should be used to annotate the* declarations of types whose annotations affect the use of annotated* elements by their clients. If a type declaration is annotated with* Documented, its annotations become part of the public API* of the annotated elements.** @author Joshua Bloch* @since 1.5*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented { }@Inherited:繼承
源碼:
package java.lang.annotation;/*** Indicates that an annotation type is automatically inherited. If* an Inherited meta-annotation is present on an annotation type* declaration, and the user queries the annotation type on a class* declaration, and the class declaration has no annotation for this type,* then the class's superclass will automatically be queried for the* annotation type. This process will be repeated until an annotation for this* type is found, or the top of the class hierarchy (Object)* is reached. If no superclass has an annotation for this type, then* the query will indicate that the class in question has no such annotation.** <p>Note that this meta-annotation type has no effect if the annotated* type is used to annotate anything other than a class. Note also* that this meta-annotation only causes annotations to be inherited* from superclasses; annotations on implemented interfaces have no* effect.** @author Joshua Bloch* @since 1.5* @jls 9.6.3.3 @Inherited*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Inherited { }使用實例:
package Annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(value = {ElementType.METHOD,ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {String value() default "張三";int age() default 24; } package Annotation;import java.lang.annotation.Annotation;public class Test {@MyAnnotation(value = "ls",age = 21)@Deprecated //不推薦使用注解public static void test() throws Exception {Annotation[] declaredAnnotations = Class.forName("Annotation.Test").getMethod("test").getDeclaredAnnotations();for(Annotation d:declaredAnnotations){if(d instanceof MyAnnotation){System.out.println(((MyAnnotation) d).age());System.out.println(((MyAnnotation) d).value());}else{System.out.println("Deprecated"); //不符合條件不推薦}}}public static void main(String[] args) throws Exception {test();} }結果:
修改代碼:修改生命周期,將RetentionPolicy.RUNTIME修改為下圖:
結果變為:
其他的幾個元注解調試也類似。
總結
以上是生活随笔為你收集整理的JDK注解(内置和自定义)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比较器Comparable(内部比较器)
- 下一篇: 字符串函数的应用