annotation的獲取方式(類、方法、域、參數、構造器)


先貼代碼:

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationTest {

/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
* @throws NoSuchFieldException
*/
public static void main(String[] args) throws SecurityException,
NoSuchMethodException, NoSuchFieldException {

Class<?> userAnnClass = UseMyAnnotation.class;
if (userAnnClass.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(userAnnClass.getAnnotation(MyAnnotation.class).value());
}

Constructor<?>[] constructorList = userAnnClass.getConstructors();
for (Constructor<?> c : constructorList) {
if (c.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(c.getAnnotation(MyAnnotation.class).value());
} else {
System.out.println("這個構造器沒有注釋");
}
}

Method method = userAnnClass.getDeclaredMethod("getName");
if (method.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(method.getAnnotation(MyAnnotation.class).value());
}

Method method1 = userAnnClass.getDeclaredMethod("setName", String.class);
Annotation[][] parAnn = method1.getParameterAnnotations();
for (Annotation[] par : parAnn) {
for (Annotation p : par) {
MyAnnotation my = (MyAnnotation) p;
System.out.println(my.value());
}
}
Field field = userAnnClass.getDeclaredField("name");
if (field.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(field.getAnnotation(MyAnnotation.class).value());
}

}

}

/*
* 為節約篇幅 將所有的方法寫到一起了 ElementType.CONSTRUCTOR 作用於構造器 ElementType.FIELD 作用於域/屬性
* ElementType.METHOD 作用於方法 ElementType.PARAMETER 用於方法的參數 ElementType.TYPE
* 用於描述類、接口(包括注解類型) 或enum聲明,最常用
*
* ElementType.PACKAGE 用於描述包 Package annotations must be in file
* package-info.java
* //ElementType.LOCAL_VARIABLE是方法中的本地變量。但是目前的javac不會在bytecode中的local
* variable中保存annotation信息,
* 所以就無法在runtime時獲取該annotaion。也就是說ElementType.LOCAL_VARIABLE只能用在RetentionPolicy
* .SOURCE情況下。 ElementType.LOCAL_VARIABLE 方法中的本地變量
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PACKAGE, ElementType.TYPE, ElementType.CONSTRUCTOR,
ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD })
@Inherited
@interface MyAnnotation {
String value() default "";
}

@MyAnnotation("我來自TYPE注解")
class UseMyAnnotation {
@MyAnnotation("我來自FIELD注解")
private String name = "蘋果";
private int price = 0;

@MyAnnotation("我來自CONSTRUCTOR(無參)注解")
public UseMyAnnotation() {
}

@MyAnnotation("我來自CONSTRUCTOR(有參)注解")
public UseMyAnnotation(String name) {
this.name = name;
}

public UseMyAnnotation(String name, int price) {
this.name = name;
this.price = price;
}

@MyAnnotation("我來自METHOD注解")
public String getName() {
return name;
}

@MyAnnotation("我來自METHOD注解")
public void setName(@MyAnnotation("我來自PARAMETER注解") String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}

執行結果:

我來自TYPE注解
我來自CONSTRUCTOR(無參)注解
我來自CONSTRUCTOR(有參)注解
這個構造器沒有注釋
我來自METHOD注解
我來自PARAMETER注解
我來自FIELD注解

說明:JAVA注解是元數據 相當於對JAVA的方法、類、域進行標注。 如果不進行處理 那么相當於沒有 ,如果進行處理,可以根據注解配置 可以用反射對原來的類、方法、域進行處理。

        RetentionPolicy.RUNTIME 注解會在class字節碼文件中存在,在運行時可以通過反射獲取到,用的比較多,特別在系統架構的時候

        RetentionPolicy.CLASS 默認的保留策略,注解會在class字節碼文件中存在,但運行時無法獲得 

        RetentionPolicy.SOURCE 注解僅存在於源碼中,在class字節碼文件中不包含 @override 等


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM