java 对象转换成另外一个类对象 泛型_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2153 | 回复: 0   主题: java 对象转换成另外一个类对象 泛型        下一篇 
    本主题由 koei123 于 2015-7-14 11:14:23 移动
youduoduo
注册用户
等级:新兵
经验:78
发帖:78
精华:0
注册:2011-11-26
状态:离线
发送短消息息给youduoduo 加好友    发送短消息息给youduoduo 发消息
发表于: IP:您无权察看 2015-7-2 15:56:48 | [全部帖] [楼主帖] 楼主

对象转换成另一个类对象

package com.mw.usims.fts.util;
import com.mw.usims.fts.model.BaseModel;
import org.apache.poi.ss.formula.functions.T;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created by LiAnAn on 2015/2/3.
*/
publicclass Test1 {
      publicstatic <T> T convertClass(Object obj,Class<T> cla) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
            Map<String,Object> maps = new HashMap<String,Object>();
            T dataBean = null;
            if(null==obj) {
                  returnnull;
            }
            Class<?> cls = obj.getClass();
            dataBean = cla.newInstance();
            Field[] fields = cls.getDeclaredFields();
            Field[] beanFields = cla.getDeclaredFields();
            for(Field field:fields){
                  String fieldName = field.getName();
                  String strGet = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
                  Method methodGet = cls.getDeclaredMethod(strGet);
                  Object object = methodGet.invoke(obj);
                  maps.put(fieldName,object==null?"":object);
            }
            for(Field field:beanFields){
                  field.setAccessible(true);
                  String fieldName = field.getName();
                  Class<?> fieldType = field.getType();
                  String fieldValue = maps.get(fieldName)==null?null:maps.get(fieldName).toString();
                  if(fieldValue!=null){
                        try {
                              if(String.class.equals(fieldType)){
                                    field.set(dataBean, fieldValue);
                              }elseif(byte.class.equals(fieldType)){
                                    field.setByte(dataBean, Byte.parseByte(fieldValue));
                              }elseif(Byte.class.equals(fieldType)){
                                    field.set(dataBean, Byte.valueOf(fieldValue));
                              }elseif(boolean.class.equals(fieldType)){
                                    field.setBoolean(dataBean, Boolean.parseBoolean(fieldValue));
                              }elseif(Boolean.class.equals(fieldType)){
                                    field.set(dataBean, Boolean.valueOf(fieldValue));
                              }elseif(short.class.equals(fieldType)){
                                    field.setShort(dataBean, Short.parseShort(fieldValue));
                              }elseif(Short.class.equals(fieldType)){
                                    field.set(dataBean, Short.valueOf(fieldValue));
                              }elseif(int.class.equals(fieldType)){
                              field.setInt(dataBean, Integer.parseInt(fieldValue));
                        }elseif(Integer.class.equals(fieldType)){
                              field.set(dataBean, Integer.valueOf(fieldValue));
                        }elseif(long.class.equals(fieldType)){
                              field.setLong(dataBean, Long.parseLong(fieldValue));
                        }elseif(Long.class.equals(fieldType)){
                              field.set(dataBean, Long.valueOf(fieldValue));
                        }elseif(float.class.equals(fieldType)){
                              field.setFloat(dataBean, Float.parseFloat(fieldValue));
                        }elseif(Float.class.equals(fieldType)){
                              field.set(dataBean, Float.valueOf(fieldValue));
                        }elseif(double.class.equals(fieldType)){
                              field.setDouble(dataBean, Double.parseDouble(fieldValue));
                        }elseif(Double.class.equals(fieldType)){
                              field.set(dataBean, Double.valueOf(fieldValue));
                        }elseif(Date.class.equals(fieldType)){
                              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                              field.set(dataBean, sdf.parse(fieldValue));
                        }
                  } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                  } catch (ParseException e) {
                        e.printStackTrace();
                  }
            }
      }
      return dataBean;
}
publicstaticvoid main(String arg0[]) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
      BaseModel baseModel = new BaseModel();
      baseModel.setId("1");
      baseModel.setDescription("2");
      baseModel.setSequence(3);
      baseModel.setType("4");
      baseModel.setValue("5");
      BaseModel base = convertClass(baseModel, BaseModel.class);
      System.out.println(base.getId());
}
}


--转自 北京联动北方科技有限公司



该贴由koei123转至本版2015-7-14 11:14:23



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论