1. 程式人生 > >利用反射獲取類或者方法或者欄位上的註解的值

利用反射獲取類或者方法或者欄位上的註解的值

從JDK1.5之後,註解在各大框架上得到了廣泛的應用。下面這個例子中,你可以判斷一個類或者方法或者欄位上有沒有註解,以及怎麼獲取上面的註解值。話不多說,程式碼如下:

AnnotationTest01.java

package com.zkn.newlearn.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.PARAMETER})
public @interface AnnotationTest01 {

	String color();
}
AnnotationTest02.java
package com.zkn.newlearn.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AnnotationTest02 {
	
	String getUserName();
}

AnnotationTest04.java
package com.zkn.newlearn.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.FIELD})
public @interface AnnotationTest04 {
	
	String getAddress();
}
AnnotationTest03.java
package com.zkn.newlearn.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 測試Annotation
 * @author zkn
 *
 */
@AnnotationTest02(getUserName="zhangsan")
public class AnnotationTest03 {

	@AnnotationTest01(color="red")
	public static String testColor(String color){
		System.out.println(color);
		return color;
	}
	
	@AnnotationTest04(getAddress="北京市海淀區")
	String address;
	
	public static void main(String[] args) {
		//獲取方法上的註解值
		Method[] methods = AnnotationTest03.class.getDeclaredMethods();
		if(methods != null){
			for(Method method : methods){
				AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class);
				if(annotation == null)
					continue;
				Method[] me = annotation.annotationType().getDeclaredMethods();
				for(Method meth : me){
					try {
						String color = (String) meth.invoke(annotation,null);
						System.out.println(color);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
			}
		}

		//獲取類上的註解值
		AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class);
		if(anno != null){
			Method[] met = anno.annotationType().getDeclaredMethods();
			for(Method me : met ){
				if(!me.isAccessible()){
					me.setAccessible(true);
				}
				try {
					System.out.println(me.invoke(anno, null));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		
		//獲取欄位上的註解值
		AnnotationTest03 noon = new AnnotationTest03();
		Field[] field = AnnotationTest03.class.getDeclaredFields();
		if(field != null){
			for(Field fie : field){
				if(!fie.isAccessible()){
					fie.setAccessible(true);
				}
				AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class);
				Method[] meth = annon.annotationType().getDeclaredMethods();
				for(Method me : meth){
					if(!me.isAccessible()){
						me.setAccessible(true);
					}
					try {
						//給欄位重新賦值
						fie.set(noon, me.invoke(annon, null));
						System.out.println(fie.get(noon));
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
				
			}
		}
	}
}