java 之 内省(JavaBean操作)

来源:互联网 发布:软件测试单元测试 编辑:程序博客网 时间:2024/05/23 13:34
package com.ethan.introSpector;import java.util.Date;public class ReflectPoint {private int x;private int y;//初始化,setProperty() nullprivate 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;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}//此处注意 参数类型Object,而不是ReflectPoint,//否则就属于重载//Object参数类型,就属于重写Object的equals方法==@Overridepublic 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;}}}}


原创粉丝点击