bean和beanform的对应的关系,同名的类型一致
public Integer x;
public String y;
public Date z;
public boolean k;
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public Date getZ() {
return z;
}
public void setZ(Date z) {
this.z = z;
}
public boolean isK() {
return k;
}
public void setK(boolean k) {
this.k = k;
}
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
TestPropertyUtilsBean testPropertyUtilsBean = new TestPropertyUtilsBean();
Bean bean = new Bean();
//判断是否public方法,getWriteMethod,getReadMethod方法调用MethodUtils
System.out.println("isReadable:");
System.out.println(testPropertyUtilsBean.isReadable(bean, "x"));
System.out.println(testPropertyUtilsBean.isReadable(bean, "y"));
System.out.println(testPropertyUtilsBean.isReadable(bean, "z"));
System.out.println(testPropertyUtilsBean.isReadable(bean, "k"));
System.out.println("-----------------------------------");
System.out.println("isWriteable:");
System.out.println(testPropertyUtilsBean.isWriteable(bean, "x"));
System.out.println(testPropertyUtilsBean.isWriteable(bean, "y"));
System.out.println(testPropertyUtilsBean.isWriteable(bean, "z"));
System.out.println(testPropertyUtilsBean.isWriteable(bean, "k"));
System.out.println("-----------------------------------");
//调用getPropertyDescriptors方法,得到name对应的PropertyDescriptor对象
System.out.println("setSimpleProperty and getSimpleProperty:");
testPropertyUtilsBean.setSimpleProperty(bean, "x", 1);
System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "x"));
testPropertyUtilsBean.setSimpleProperty(bean, "y", "y");
System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "y"));
testPropertyUtilsBean.setSimpleProperty(bean, "z", new Date());
System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "z"));
testPropertyUtilsBean.setSimpleProperty(bean, "k", true);
System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "k"));
System.out.println("-----------------------------------");
//把bean的值转化为map输出
System.out.println("describe:");
System.out.println(testPropertyUtilsBean.describe(bean));
System.out.println("-----------------------------------");
Beanform form = new Beanform();
testPropertyUtilsBean.copyProperties(form, bean);
System.out.println(testPropertyUtilsBean.describe(form));
}
测试类是继承PropertyUtilsBean,因为测试的有些方法是protected
控制台:
isReadable:
true
true
true
true
-----------------------------------
isWriteable:
true
true
true
true
-----------------------------------
setSimpleProperty and getSimpleProperty:
1
y
Sun May 20 15:31:49 CST 2012
true
-----------------------------------
describe:
{class=class Bean, z=Sun May 20 15:31:49 CST 2012, k=true, y=y, x=1}
-----------------------------------
{class=class Beanform, z=Sun May 20 15:31:49 CST 2012, k=true, y=y, x=1}
注意,PropertyUtilsBeancopy时,类型要一致,不然会报错
其他方法,没做测试,只是看了下代码
getProperty(Object bean, String name)
getNestedProperty(bean, name) 取bean相对应的name的值
getPropertyOfMapBean(Map bean, String propertyName),取bean的对应key的值
getMappedProperty(Object bean, String name)-->getMappedProperty(Object bean,String name, String key)先取bean的map,再通过map的key取值
getIndexedProperty(Object bean, String name)-->getIndexedProperty(Object bean,String name, int index)先取bean的list,再通过索引取值
setProperty(Object bean, String name, Object value)
setNestedProperty(Object bean,String name, Object value)给bean的name属性赋value值
setPropertyOfMapBean(Map bean, String propertyName, Object value)给bean的对应key的设值
setIndexedProperty(Object bean, String name,Object value)-->setIndexedProperty(Object bean, String name,int index, Object value)先取bean的map,再通过map的key设值
setMappedProperty(Object bean, String name,Object value)-->setMappedProperty(Object bean, String name,String key, Object value)先取bean的list,再通过索引设值