如何把Map 转化为指定对象呢?
先看测试:
@Test
public void test_convertMap2Obj() throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{
Map map=new HashMap();
map.put("personName", "黄威");
map.put("birthdate", new Timestamp(1000255));
map.put("identitify", "422203158895255555");
map.put("age", 21);
Object obj=ReflectHWUtils.convertMap2Obj(map, Person2.class);
Person2 person=(Person2)obj;
// System.out.println(obj);
System.out.println(person.getPersonName());
System.out.println(person.getAge());
System.out.println(person.getBirthdate());
}
运行结果:
黄威
21
1970-01-01 08:16:40.255
ReflectHWUtils.convertMap2Obj实现如下:
/***
* convert map to object ,see setObjectValue(obj, map)
* @param map : key是对象的成员变量,其value就是成员变量的值
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
*/
public static Object convertMap2Obj(Map<String, Object> map,Class clazz) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException, IllegalArgumentException{
if(ValueWidget.isNullOrEmpty(map)){
return null;
}
Object obj=clazz.newInstance();
setObjectValue(obj, map);
/*for(Iterator it=map.entrySet().iterator();it.hasNext();){
Map.Entry<String, Object> entry=(Map.Entry<String, Object>)it.next();
String key=entry.getKey();
Object val=entry.getValue();
}*/
return obj;
}
/***
* 利用反射设置对象的属性值. 注意:属性可以没有setter 方法.
*
* @param obj
* @param params
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setObjectValue(Object obj, Map<String, Object> params)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
if(ValueWidget.isNullOrEmpty(obj)){
return;
}
if (ValueWidget.isNullOrEmpty(params)) {
return;
}
Class<?> clazz = obj.getClass();
for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
.next();
String key = entry.getKey();
Object propertyValue = entry.getValue();
if (ValueWidget.isNullOrEmpty(propertyValue)) {
continue;
}
Field name = getSpecifiedField(clazz, key);
if (name != null) {
name.setAccessible(true);
name.set(obj, propertyValue);
}
}
}
依赖的方法:
/***
* 利用反射设置对象的属性值. 注意:属性可以没有setter 方法.
*
* @param obj
* @param params
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setObjectValue(Object obj, Map<String, Object> params)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
if(ValueWidget.isNullOrEmpty(obj)){
return;
}
if (ValueWidget.isNullOrEmpty(params)) {
return;
}
Class<?> clazz = obj.getClass();
for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
.next();
String key = entry.getKey();
Object propertyValue = entry.getValue();
if (ValueWidget.isNullOrEmpty(propertyValue)) {
continue;
}
Field name = getSpecifiedField(clazz, key);
if (name != null) {
name.setAccessible(true);
name.set(obj, propertyValue);
}
}
}
/***
* Get Specified Field
*
* @param clazz
* @param fieldName
* @return
*/
public static Field getSpecifiedField(Class<?> clazz, String fieldName) {
Field f = null;
if (ValueWidget.isNullOrEmpty(clazz)||ValueWidget.isNullOrEmpty(fieldName)) {
return null;
}
try {
f = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
Class superClass2=clazz.getSuperclass();
if (superClass2.getName().equals(Object.class.getName())){
return null;
}
return getSpecifiedField(superClass2/*
* may be null if it is
* Object .
*/, fieldName);
// e.printStackTrace();
}
return f;
}
源代码见:
源代码下载地址
跨浏览器实现placeholder