1.作用
2.自定義註解用法
(1)建一個註解@interface TestA
package com.annotationTest;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestA {
String name();
int id() default 0;
Class<Long> gid();
}
(2)寫一個類UserAnnotation 來使用@TestA
package com.annotationTest;
import java.util.ArrayList;
import java.util.List;
@TestA(name="type",gid=Long.class) //類成員註解
public class UserAnnotation {
@TestA(name="param",id=1,gid=Long.class) //類成員註解
private Integer age ;
@TestA (name="construct",id=2,gid=Long.class)//構造方法註解
public UserAnnotation() {
}
@TestA(name="public method",id=3,gid=Long.class) //類方法註解
public void functionA(){
List list = new ArrayList();
}
@TestA(name="protected method",id=4,gid=Long.class) //類方法註解
public void functionB(){
List list = new ArrayList();
}
@TestA(name="private method",id=5,gid=Long.class) //類方法註解
public void functionC(){
List list = new ArrayList();
}
}
(3)簡單打印出UserAnnotation 類中所使用到的類註解
jdk反射機制
public interface AnnotatedElement {
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
Annotation[] getAnnotations();
Annotation[] getDeclaredAnnotations();
}
isAnnotationPresent:判斷是否標註了指定註解
getAnnotation:獲取指定註解,沒有則返回null
getAnnotations:獲取所有註解,包括繼承自基類的,沒有則返回長度爲0的數組
getDeclaredAnnotations:獲取自身顯式標明的所有註解,沒有則返回長度爲0的數組
package com.annotationTest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class ParseAnnotation {
public static void parseTypeAnnotation() throws ClassNotFoundException{
Class clazz = Class.forName("com.annotationTest.UserAnnotation");
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
TestA testA = (TestA) annotation;
System.out.println("id = "
+testA.id()
+"\t name = "
+testA.name()
+"\t gid = "
+testA.gid()
);
}
}
private static void parseConstructAnnotation() {
Constructor[] constructors = UserAnnotation.class.getConstructors();
for (Constructor constructor : constructors) {
boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);
if (hasAnnotation) {
TestA annotation = (TestA) constructor.getAnnotation(TestA.class);
System.out.println("constructor : "
+constructor.getName()
+"\t id = "
+annotation.id()
+"\t name = "
+annotation.name()
+"\t gid = "
+annotation.gid()
);
}
}
}
private static void parseMethodAnnotation() {
Method[] methods = UserAnnotation.class.getDeclaredMethods();
for (Method method : methods) {
/*
* 判斷方法中是否有指定註解類型的註解
*/
boolean hasAnnotation = method.isAnnotationPresent(TestA.class);
if (hasAnnotation) {
TestA annotation = method.getAnnotation(TestA.class);
System.out.println("method : "
+method.getName()
+"\t id = "
+annotation.id()
+"\t name = "
+annotation.name()
+"\t gid = "
+annotation.gid()
);
}
}
}
public static void main(String[] args) throws ClassNotFoundException {
parseTypeAnnotation();
parseConstructAnnotation();
parseMethodAnnotation();
}
}