package test;import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; import java.util.HashMap; import java.util.Map;import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.converters.BigDecimalConverter; import org.apache.commons.beanutils.converters.BigIntegerConverter; import org.apache.commons.beanutils.converters.BooleanConverter; import org.apache.commons.beanutils.converters.ByteConverter; import org.apache.commons.beanutils.converters.CharacterConverter; import org.apache.commons.beanutils.converters.DoubleConverter; import org.apache.commons.beanutils.converters.FloatConverter; import org.apache.commons.beanutils.converters.IntegerConverter; import org.apache.commons.beanutils.converters.LongConverter; import org.apache.commons.beanutils.converters.ShortConverter; import org.apache.commons.beanutils.converters.SqlDateConverter; import org.apache.commons.beanutils.converters.SqlTimeConverter; import org.apache.commons.beanutils.converters.SqlTimestampConverter;//默认支持8个原生类型(int...),String, BigDecimal, BigInteger, java.sql.Date, java.sql.Time, java.sql.Timestamp等 public class Test { int x; Date date; B b; public Date getDate() { return date; } public int getX() { return x; } public B getB() { return b; } public void setDate(Date date) { this.date = date; } public void setX(int x) { this.x = x; } public void setB(B b) { this.b = b; } public static void main(String[] args) { ConvertUtils.deregister(); ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); //如果转换出错置null,默认报错 ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class); ConvertUtils.register(new BooleanConverter(null), Boolean.class); ConvertUtils.register(new ByteConverter(null), Byte.class); ConvertUtils.register(new CharacterConverter(null), Character.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new ShortConverter(null), Short.class); ConvertUtils.register(new SqlDateConverter(null), Date.class); ConvertUtils.register(new SqlTimeConverter(null), Time.class); ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class); ConvertUtils.register(new BConverter(), B.class); //注册 try { Test t = new Test(); BeanUtils.setProperty(t, "x", "12345"); //单个属性设置 Map properties = new HashMap(); properties.put("x", "123"); properties.put("date", "2005-10-11"); properties.put("b", "value in b"); BeanUtils.populate(t, properties); //整个JavaBean设置 System.out.println(BeanUtils.describe(t)); //与populate相反 } catch (Exception ex) { ex.printStackTrace(); } } }class B { String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } }class BConverter implements Converter { public Object convert(Class type, Object value) { try { B b = (B) type.newInstance(); if (value != null) { b.setValue(String.valueOf(value)); } return b; } catch (Exception ex) { throw new ConversionException("Unable to convert the FieldAttribute object!", ex); } } }
更能比较强大,支持setProperty(obj, "a.x.y", "") // obj has an a, a has an x, and x has a y 以及“a[i]”//a是一个数组
>得到 set 方法,怎么知道类型???Method.java /** * Returns a <code>Class</code> object that represents the formal return type * of the method represented by this <code>Method</code> object. * * @return the return type for the method this object represents */ public Class getReturnType() { return returnType; }
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;//默认支持8个原生类型(int...),String, BigDecimal, BigInteger, java.sql.Date, java.sql.Time, java.sql.Timestamp等
public class Test {
int x; Date date; B b; public Date getDate() {
return date;
} public int getX() {
return x;
} public B getB() {
return b;
} public void setDate(Date date) {
this.date = date;
} public void setX(int x) {
this.x = x;
} public void setB(B b) {
this.b = b;
} public static void main(String[] args) {
ConvertUtils.deregister();
ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); //如果转换出错置null,默认报错
ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
ConvertUtils.register(new BooleanConverter(null), Boolean.class);
ConvertUtils.register(new ByteConverter(null), Byte.class);
ConvertUtils.register(new CharacterConverter(null), Character.class);
ConvertUtils.register(new DoubleConverter(null), Double.class);
ConvertUtils.register(new FloatConverter(null), Float.class);
ConvertUtils.register(new IntegerConverter(null), Integer.class);
ConvertUtils.register(new LongConverter(null), Long.class);
ConvertUtils.register(new ShortConverter(null), Short.class);
ConvertUtils.register(new SqlDateConverter(null), Date.class);
ConvertUtils.register(new SqlTimeConverter(null), Time.class);
ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
ConvertUtils.register(new BConverter(), B.class); //注册
try {
Test t = new Test();
BeanUtils.setProperty(t, "x", "12345"); //单个属性设置
Map properties = new HashMap();
properties.put("x", "123");
properties.put("date", "2005-10-11");
properties.put("b", "value in b");
BeanUtils.populate(t, properties); //整个JavaBean设置
System.out.println(BeanUtils.describe(t)); //与populate相反
} catch (Exception ex) {
ex.printStackTrace();
}
}
}class B {
String value; public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
}
}class BConverter implements Converter {
public Object convert(Class type, Object value) {
try {
B b = (B) type.newInstance();
if (value != null) {
b.setValue(String.valueOf(value));
}
return b;
} catch (Exception ex) {
throw new ConversionException("Unable to convert the FieldAttribute object!", ex);
}
}
}
以及“a[i]”//a是一个数组
* Returns a <code>Class</code> object that represents the formal return type
* of the method represented by this <code>Method</code> object.
*
* @return the return type for the method this object represents
*/
public Class getReturnType() {
return returnType;
}