对象转换成另一个类对象
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