Map 和 java Bean 的相互转换

来源:互联网 发布:.science域名 搜索引擎 编辑:程序博客网 时间:2024/05/23 19:18
package beanutils;


import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


import org.apache.commons.lang.StringUtils;


public class BeanUtils {


/**
* 把bean 转成 map
* @param obj
* @return
* @throws IntrospectionException 
* @throws InvocationTargetException 
* @throws IllegalArgumentException 
* @throws IllegalAccessException 
*/
@SuppressWarnings({ "rawtypes", "unused" })
public static Map beanTransMap(Object obj) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//得到类的文件路径
 Class type = obj.getClass();
 //获取对象的实例
 BeanInfo binfo = Introspector.getBeanInfo(type);
 // 获取该类的属性
 PropertyDescriptor[] propertyDescriptors = binfo.getPropertyDescriptors();
 Map<String,Object> retMap = new HashMap<String,Object>();
 for(int i=0;i<propertyDescriptors.length;i++){
 //得到属性相关信息
 PropertyDescriptor pd = propertyDescriptors[i];
 if(!pd.getName().equals("class")){
 // 获取该属性的信息
 Method method = pd.getReadMethod();
 // 得到该属性的值
Object objInvoke = method.invoke(obj); 
if(objInvoke!=null){
retMap.put(pd.getName(), objInvoke);
}else{
retMap.put(pd.getName(), ""); 
}
 
 }
 }
 return retMap;
}

/**
* 把map 转成bean
* @return
* @throws IntrospectionException 
* @throws IllegalAccessException 
* @throws InstantiationException 
* @throws InvocationTargetException 
* @throws IllegalArgumentException 
*/
public static Object mapConvertBean(Class type, Map map) throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
BeanInfo binfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = binfo.getPropertyDescriptors();
for(int i=0;i<propertyDescriptors.length;i++){
PropertyDescriptor pd = propertyDescriptors[i];
if(!pd.getName().equals("class")){
if(map.containsKey(pd.getName()) && map.get(pd.getName())!=null && StringUtils.isNotBlank(map.get(pd.getName()).toString())){
Object value = map.get(pd.getName());
Object[] args = new Object[1];
args[0] = value;
pd.getWriteMethod().invoke(obj, args);
}
}
}
return obj;

}
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
Person pson =  new Person();
pson.setAge(12);
pson.setName("转换");
Map<String,Object> retMap = beanTransMap(pson);
Person p = new Person();
p = (Person)mapConvertBean(p.getClass(), retMap);
}

}


package beanutils;


import java.util.Date;


public class Person {
 
public String name;
public long age;
public Date birthDay;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAge() {
return age;
}
public void setAge(long age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}


}







0 0
原创粉丝点击