import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 将object转为相应的实体类
* @param objectList
* @param list
*/
private List<Object> assembleper(List<Object[]> objectList,String className){
List<Object> list = new ArrayList<>();
className = "cn.wiztek.cissClient.tt.domain.PersonalHouseVBean";
try {
Class<?> classEntity = Class.forName(className);
//获取反射类的相关信息
Field[] fields = classEntity.getDeclaredFields();
//存放反射类的变量名称
String[] methodNames = new String[fields.length];
int i = 0;
//获取需要set的方法名
for(Field field : fields){
String name = field.getName();
String methodName = name.substring(0, 1).toUpperCase() + name.substring(1);
methodNames[i] = "set"+methodName;
i++;
}
for(Object[] object : objectList){
//将反射类变成object类,到时候可以用强制转换的方式变成自己上面的反射类,这个地方的object是可以强制转换的,上面参数中的object是不能强制转换的
Object oo = classEntity.newInstance();
int k = 0;
//获取反射类的相关方法
Method method = null;
for(Field field : fields){
method = classEntity.getMethod(methodNames[k], field.getType());
//上面获取的方法是反射类的set方法,在这里向set方法中赋值
method.invoke(oo, object[k]);
k++;
}
list.add(oo);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return list;
}