ITEEDU

注释的反射的一个简单例子

Bean

public class Bean {
	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}
	
	@Column("column1")
	private String field;
}

Column

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Column {
	String value() default "";
}

获取属性名和注释值

import java.lang.reflect.Field;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Class<Bean > c=Bean.class;
		Field[] fs=c.getDeclaredFields();
		for(Field f:fs){
			System.out.println(f.getName());
			System.out.println(f.isAccessible());
			Column ca=f.getAnnotation(Column.class);
			System.out.println(ca.value());
		}
	}
}

对属性赋值

import java.lang.reflect.Field;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Class<Bean> c=Bean.class;
		Field[] fs=c.getDeclaredFields();
		Bean b = null;
		try {
			b=c.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Field f=fs[0];//由于只有一个属性所以这样测试
		System.out.println(f.getName()+"="+b.getField());
		f.setAccessible(true);//使私有属性可以被设置
		try {
			if(f.getType()==String.class)
				f.set(b, "iteedu");//将b对象的f属性设置为"iteedu"
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch blockd
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(f.getName()+"="+b.getField());
		
	}
}

根据属性调用set方法

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

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Class<Bean> c = Bean.class;
		Field[] fs = c.getDeclaredFields();
		Bean b = null;
		try {
			b = c.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Field f = fs[0];// 由于只有一个属性所以这样测试
		System.out.println(f.getName() + "=" + b.getField());
		String fn = f.getName();
		//用属性名合成set方法名
		fn = fn.replaceFirst(fn.substring(0, 1), fn.substring(0, 1)
				.toUpperCase());
		System.out.println(fn);
		Method m = null;

		Class[] partypes = new Class[]{String.class};
		
		try {
			m = c.getMethod("set" + fn, partypes);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(m.getName());
		Object arglist[] = new Object[]{"iteedu"};

		try {
			m.invoke(b, arglist);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(f.getName() + "=" + b.getField());

	}
}