java 之 内省(JavaBean操作)

package com.ethan.introSpector;
import java.util.Date;


public class ReflectPoint {
	private int x;
	private int y;
	
	//初始化,setProperty() null
	private Date birthday = new Date();
	
	public String str1 = "ball";
	public String str2 = "basketball";
	public String str3 = "ethan";
	
	public ReflectPoint(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	//此处注意 参数类型Object,而不是ReflectPoint,
	//否则就属于重载
	//Object参数类型,就属于重写Object的equals方法==
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ReflectPoint other = (ReflectPoint) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}
package com.ethan.introSpector;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;


public class IntroSpectorTest {

	/**
	 * IntroSpector 内省
	 * @param args
	 * @throws IntrospectionException 
	 */
	public static void main(String[] args) throws Exception {
		ReflectPoint pt1 = new ReflectPoint(3, 5);
		
		String propertyName = "x";
		
		Object retVal = getProperty(pt1, propertyName);
		System.out.println(retVal);
		
		//利用第三方jar,
		//把属性的值都自动转为字符串BeanUtils.getProperty(pt1, propertyName).getClass().getName()--->java.lang.String
		//自动进行类型转换
		Object bValue = BeanUtils.getProperty(pt1, propertyName);
		BeanUtils.setProperty(pt1, propertyName, 44);
		System.out.println(pt1.getX());
		
		
		Object value = 7;
		setProperties(pt1, propertyName, value);
		System.out.println(pt1.getX());
		
		//Date.setTime(); 支持级联操作,EL表达式,属性链
		BeanUtils.setProperty(pt1, "birthday.time", "111");
		System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
		
		//java7的新特性
		//Map map = {name:"ethan",age:22};
//		BeanUtils.setProperty(map, "name", "xxx");
		
		//以属性本身的类型,进行设置
		PropertyUtils.setProperty(pt1, "x",99);
	}
	
	private static Object getProperty(Object pt1,String propertyName) throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
		Method methodGetX = pd.getReadMethod();
		Object retVal = methodGetX.invoke(pt1);
		return retVal;
	}
	
	private static void setProperties(Object pt1,String propertyName,Object value) throws Exception {
//		PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
//		Method methodSetX = pd.getWriteMethod();
//		methodSetX.invoke(pt1, value);
		
		//这是比较麻烦的做法
		BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
		PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
		
		for(PropertyDescriptor pd:pds) {
			//循环遍历找到对应的属性名
			if(pd.getName().equals(propertyName)) {
				//找到pd,进行相应的操作
				Method methodSetX = pd.getWriteMethod();
				methodSetX.invoke(pt1, value);
				break;
			}
			
		}
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值