反射获取类,方法,属性上的注解

//类注解
package com.java.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 ClassAnnotation {
	String className();
}
//方法注解
package com.java.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.METHOD)
public @interface MethodAnnotation {
	String methodName();
	String methodAdd() default "";
}
//属性注解
package com.java.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.FIELD)
public @interface FieldAnnotation {
	String fieldName();
}
//测试类
package com.java.annotation;

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

@ClassAnnotation(className="test")
public class Test {
	@MethodAnnotation(methodName="getMethodName")
	public String getMethodName(){
		return "getMethodName1";
	}
	@FieldAnnotation(fieldName="nihao")
	public String field;
	public static void main(String[] args) {
		//获取类上面的注解
		ClassAnnotation anno = Test.class.getAnnotation(ClassAnnotation.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();
				}
			}
		}

		//获取方法上的注解
		Method[] methods = Test.class.getDeclaredMethods();
		if(methods != null){
			for(Method method : methods){
				//获取方法上含有MethodAnnotation注解的方法
				MethodAnnotation annotation = method.getAnnotation(MethodAnnotation.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();
					}
				}
			}
		}
		//获取属性上的注解
		Test noon = new Test();
		Field[] field = Test.class.getDeclaredFields();
		if(field != null){
			for(Field fie : field){
				if(!fie.isAccessible()){
					fie.setAccessible(true);
				}
				FieldAnnotation annon = fie.getAnnotation(FieldAnnotation.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();
					}
				}
			}
		}
		
		
	}
}

输出结果:

----test
--getMethodName
--
-----nihao

你可能感兴趣的:(工具类)