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);
}
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
- Map 和 java Bean 的相互转换
- Java Map和Bean相互转换
- Java bean 与 Map的相互转换
- Bean类和Map的相互转换
- Java中Bean与Map/Json/DBObject的相互转换
- java实现的bean类型和map类型相互转换的工具类
- Bean、Map相互转换
- java 对象和map的相互转换
- Java Map和Bean之前的转换
- java bean、List、数组、map和Json的相互转化
- JSON和JAVA的bean的相互转换
- java bean和xml相互转换
- java bean和xml相互转换
- 利用Gson使json字符串与java bean/list/map之间的相互转换
- java 利用内省实现bean和map的转换
- Xml和bean之间的相互转换
- Java:JSON和Map相互转换
- Bean和Map相互转化-Java工具类(一)
- visual assist x的工具栏不显示
- linux进程互斥锁
- (转)Android 编译选项user、u…
- (转)linux下svn常用指令
- (转)Linux下RPM软件包的安装及卸载
- Map 和 java Bean 的相互转换
- Android xml资源文件中@、@an…
- (转)Monkey test的使用
- (转) MTK 2G芯片使用联…
- 怎样使一个Android应用不被…
- Android 4.2日志系统驱动程序…
- linux下,文件名带括号如何处理
- 解决有道云笔记QQ总是授权失败问题
- android编译时出现cannot fin…