java反射---我之见解

阅读更多

最近在做一个项目,开始就没打算使用反射,但是做着做着,发现有很多代码相同!感觉很郁闷,还有很多地方需要运行时才定义的类型,这让我不禁想起了反射,开始只是用一点,然后发现使用反射代码少了,循环多了!很多地方不用写死一个类型,可以使用反射去获取源对象,越做就感觉越好用,甚至连对象封装我都放弃了apache的BeanUtils,自己去写一个封装的方法,可能性能和优势都不够别人强,但是爆出异常至少我可以控制到!这个是使用框架无法做到的!
所以我觉得,作为一个优秀的程序员,需要懂得反射,会用反射,如果作为一个架构师,需要精通反射!有了反射可以让代码更少,让代码的复用性更高更容易维护!而且会减少模块和模块之间的耦合,进一步做到了低耦合高内聚!

 

public class DemoBean {
	private String name;
	private String engName;
	private int age;
	private String dept;
	private String address;
	private String phone;
	private String info;
	private String desc;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEngName() {
		return engName;
	}

	public void setEngName(String engName) {
		this.engName = engName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

}
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Demo {
	public static void main(String[] args) throws SecurityException,
			NoSuchFieldException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException {
		DemoBean2 db = new DemoBean2();
		merge(db, getMap());
		System.out.println(db.getAge());
		System.out.println(db.getEngName());
		System.out.println(db.getDept());
		System.out.println(db.getInfo());
		System.out.println(db.getAddress());
		System.out.println(db.getPhone());
	}

	public static void merge(Object bean, Map map)
			throws SecurityException, NoSuchFieldException,
			NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		Set set = map.keySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String fieldName = it.next();
			Field f = bean.getClass().getDeclaredField(fieldName);
			String methodName = fieldName.substring(0, 1).toUpperCase()
					+ fieldName.substring(1, fieldName.length());
			Method m = bean.getClass().getDeclaredMethod("set" + methodName,
					f.getType());
			m.invoke(bean, map.get(fieldName));
		}
	}

	public static Map getMap() {
		Map map = new HashMap();
		map.put("engName", "vincent");
		map.put("age", 12);
		map.put("dept", "Boss");
		map.put("address", "中国北京");
		map.put("phone", "12450358357");
		map.put("info", "this is infomation");
		return map;
	}
}

 

    利用反射去封装一个实体,这样无论你在map里面放入多少个元素都和可以完整的封装到实体里面,用别的方法就比较难实现了!

你可能感兴趣的:(Java,Bean,框架,Apache,F#)