利用java反射机制对多属性javabean赋值
2013-05-30 18:24
162 查看
public class ReflectMatch {
//封装属性
private Map<String,Field> fieldMap = new HashMap<String,Field();
//封装属性的set方法
private Map<String,Method> methMap=new HashMap<String,Method>();
/*c:为赋值对象,
*values为给属性要赋的值,put('m1',1);put('m2',2),m1、m2等为属性
*/
public void setValue(Object c,Map values){
try{
Field[] fields = c.getClass().getDeclaredFields();
Method[] methods = c.getClass().getDeclaredMethods();
for(Field field : fields){
String attri = field.getName();
fieldMap.put(attri.toLowerCase(), field);
for(Method method : methods){
String meth = method.getName();
//匹配set方法
if(meth != null
&& "set".equals(meth.substring(0, 3))
&& Modifier.isPublic(method.getModifiers())
&& ("set"+Character.toUpperCase(attri.charAt(0))+attri.substring(1)).equals(meth)){
methodMap.put(attri.toLowerCase(), method);
break;
}
}
}
//2、属性赋值
for(Iterator it = values.keySet().iterator();it.hasNext();){
String name = (String)it.next();
String value = (String)values.get(name);
if (value == null)
continue;
value = value.trim();
name = name.trim();
Field field = fieldMap.get(name.toLowerCase());
if(field == null) continue;
Method method = methodMap.get(name.toLowerCase());
if (method == null) continue;
fill(c,field,method,value);
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 将字符串值转换为合适的值填充到对象的指定域
* @param bean 被填充的java bean
* @param field 需要填充的域
* @param value 字符串值
*/
public static void fill(Object bean, Field field, Method method, String value){
if (value == null||"null".equalsIgnoreCase(value))
return;
try{
Object[] oo = new Object[1];
String type = field.getType().getName();
if ("java.lang.String".equals(type)){
oo[0] = value;
}else if ("java.lang.Integer".equals(type)){
if (value.length() > 0) oo[0] = Integer.valueOf(value);
}else if ("java.lang.Float".equals(type)){
if (value.length() > 0) oo[0] = Float.valueOf(value);
}else if ("java.lang.Double".equals(type)){
if (value.length() > 0) oo[0] = Double.valueOf(value);
}else if ("java.math.BigDecimal".equals(type)){
if (value.length() > 0) oo[0] = new BigDecimal(value);
}else if("java.util.Date".equals(type)){
if (value.length() > 0) oo[0] = DateTimeUtil.create(value, "yyyy-mm-dd hh:mi:ss").toDate();
}else if("java.sql.Timestamp".equals(type)){
if (value.length() > 0) oo[0] = DateTimeUtil.create(value, "yyyy-mm-dd hh:mi:ss").toTimestamp();
}else if ("java.lang.Boolean".equals(type)){
if (value.length() > 0) oo[0] = Boolean.valueOf(value);
}else if("java.lang.Long".equals(type)){
if(value.length() > 0) oo[0] = Long.valueOf(value);
}
method.invoke(bean, oo);
}catch(Exception e){
e.printStackTrace();
}
}
}
相关文章推荐
- 利用反射对对象属性赋值取值操作
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- 利用JavaScript 给元素属性赋值
- 利用映射实现两个结构相同javabean的赋值
- java反射机制动态给属性赋值
- 利用反射实现JavaBean的自动赋值
- 利用JavaScript 给元素属性赋值
- 将父类的属性赋值给子类(利用反射)
- 遍历 Map 对象, 利用反射为 Class 对象的对应的属性赋值.
- 利用反射给对象中的某个属性赋值或取值
- 利用java反射机制,子类从父类拷贝属性--解决信息修改类设计的模式
- JAVA 输出javabean属性方法 详解 利用common包~~~important
- 利用JavaScript 给元素属性赋值
- hibernate利用mysql的自增张id属性实现自增长id和手动赋值id并存
- 用Set方法对JavaBean属性赋值
- 利用java反射机制给实体赋值
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- 利用JavaScript 给元素属性赋值
- 利用cglib给javabean动态添加属性
- 利用“反射”给类的属性赋值。