package reflectTest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
public class CommonUtis {
/**
* map转化为bean
* @param map
* @param object
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static Object mapToBean(Map<String,Object> map, Class<?> object) throws InstantiationException, IllegalAccessException {
Object instance = object.newInstance();
for(Map.Entry<String, Object> eachMap:map.entrySet()) {
String property = eachMap.getKey();
Object value = eachMap.getValue();
String setMethod = "set"+property.substring(0,1).toUpperCase()+property.substring(1);
Field field = getField(property,object);
Class<?> fType = field.getType();
value = convert(value,fType);
System.out.println(value);
try {
object.getMethod(setMethod, fType).invoke(instance, value);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return instance;
}
private static Object convert(Object value, Class<?> fType) {
Object retVal = null;
if (Long.class.getName().equals(fType.getName()) || long.class.getName().equals(fType.getName())) {
retVal = Long.parseLong(value.toString());
}
else if (Float.class.getName().equals(fType.getName()) || float.class.getName().equals(fType.getName())) {
retVal = Float.parseFloat(value.toString());
}
else if (Double.class.getName().equals(fType.getName()) || double.class.getName().equals(fType.getName())) {
retVal = Double.parseDouble(value.toString());
}
else if (Integer.class.getName().equals(fType.getName()) || int.class.getName().equals(fType.getName())) {
retVal = Integer.parseInt(value.toString());
}
else {
retVal = value;
}
return retVal;
}
public static Field getField(String property,Class<?> obj)
{
if (Object.class.getName().equals(obj.getName()))
{
return null;
}
Field[] field = obj.getDeclaredFields();
for (Field fe:field) {
if (fe.getName().equals(property))
{
return fe;
}
}
Class<?> parent = obj.getSuperclass();
if (parent != null)
{
return getField(property,parent);
}
return null;
}
//Bean到Map的转化
public static Map beanToMap(Object object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Field[] field = object.getClass().getDeclaredFields();
Map map = new HashMap();
for (Field fi :field )
{
String property = fi.getName();
String getMe = "get"+property.substring(0, 1).toUpperCase()+property.substring(1);
Object obj = object.getClass().getMethod(getMe).invoke(object);
map.put(property,obj);
}
return map;
}
}
实体类
package reflectTest;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
测试类:
package reflectTest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
public class Hello {
public static void main(String[] args) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
{
Map map = new HashMap();
map.put("name", "burning");
map.put("age", 24);
try {
Person person = (Person)CommonUtis.mapToBean(map,Person.class);
System.out.println(person);
map = CommonUtis.beanToMap(person);
System.out.println(map.get("age"));
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}