一、重復注解
1、基本介紹
-
自從 JDK 5 引入注解以來,注解的使用開始流行,在各個框架中被廣泛使用
-
不過注解有一個很大的限制,在同一個地方不能多次使用同一個注解
-
JDK 8 引入了重復注解的概念
2、具體實現
(1)自定義注解
- MyAnnotation 注解
package com.my.repeatannotation;import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;// 指定對應的容器
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String value();
}
- MyAnnotations 注解(重復注解的容器)
package com.my.repeatannotation;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotations {MyAnnotation[] value();
}
(2)實體類
- MyUser 類
package com.my.repeatannotation;@MyAnnotation("a1")
@MyAnnotation("a2")
@MyAnnotation("a3")
public class MyUser {@MyAnnotation("b1")@MyAnnotation("b2")@MyAnnotation("b3")private String name;@MyAnnotation("c1")@MyAnnotation("c2")@MyAnnotation("c3")public void say() {};
}
(3)測試
- RepeatAnnotationTest 類
package com.my.repeatannotation;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class RepeatAnnotationTest {public static void main(String[] args) {Class<MyUser> myUserClass = MyUser.class;// 獲取類的重復注解MyAnnotation[] myAnnotations1 = myUserClass.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations1) {System.out.println(myAnnotation.value());}// 獲取屬性的重復注解Field[] declaredFields = myUserClass.getDeclaredFields();for (Field declaredField : declaredFields) {declaredField.setAccessible(true);MyAnnotation[] myAnnotations2 = declaredField.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations2) {System.out.println(myAnnotation.value());}}// 獲取方法的重復注解Method[] declaredMethods = myUserClass.getDeclaredMethods();for (Field declaredField : declaredFields) {MyAnnotation[] myAnnotations3 = declaredField.getAnnotationsByType(MyAnnotation.class);for (MyAnnotation myAnnotation : myAnnotations3) {System.out.println(myAnnotation.value());}}}
}
二、類型注解
1、基本介紹
-
JDK 8 中為 @Target 添加了兩種類型
-
ElementType.TYPE_PARAMETER:表示該注解可以寫在參數列表的數據類型或泛型的聲明語句中
-
ElementType.TYPE_USE:表示該注解可以寫在任何數據類型的聲明語句中
-
2、具體實現
(1)自定義注解
- MyType 注解
package com.my.typeannotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyType {String value();
}
(2)實體類
- MyStudent 類
package com.my.typeannotation;import java.time.Instant;public class MyStudent {private @MyType("hello1") String name;private @MyType("Hello2") Instant age;public @MyType("Hello3") String say() {return "Hello World";};public String eat(@MyType("Hello4") String food) {return "Hello World";}
}
(3)測試
- TypeAnnotationTest 類
package com.my.typeannotation;import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;public class TypeAnnotationTest {public static void main(String[] args) {Class<MyStudent> myStudentClass = MyStudent.class;TypeVariable<Class<MyStudent>>[] typeParameters = myStudentClass.getTypeParameters();for (TypeVariable<Class<MyStudent>> typeParameter : typeParameters) {// 獲取類中泛型中的注解MyType myType = typeParameter.getAnnotation(MyType.class);System.out.println(myType.value());}Field[] declaredFields = myStudentClass.getDeclaredFields();for (Field declaredField : declaredFields) {// 獲取屬性的數據類型的注解AnnotatedType annotatedType = declaredField.getAnnotatedType();MyType myType = annotatedType.getAnnotation(MyType.class);System.out.println(myType.value());}Method[] declaredMethods = myStudentClass.getDeclaredMethods();for (Method declaredMethod : declaredMethods) {if (declaredMethod.getName().equals("say")) {// 獲取方法返回值的注解AnnotatedType annotatedReturnType = declaredMethod.getAnnotatedReturnType();MyType myType = annotatedReturnType.getAnnotation(MyType.class);System.out.println(myType.value());} else if (declaredMethod.getName().equals("eat")) {// 獲取方法參數列表的注解AnnotatedType[] annotatedParameterTypes = declaredMethod.getAnnotatedParameterTypes();MyType myType = annotatedParameterTypes[0].getAnnotation(MyType.class);System.out.println(myType.value());}}}
}