1. 程式人生 > >Map 和 java Bean 的相互轉換

Map 和 java Bean 的相互轉換

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;
}


}