package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.utils.Classes;
import excel.impl.ExcelReader;
public class ClassUtil<T> {
private Class<T> classInstance;
private Method[] methods;
private Object object;
private Field[] fs;
/*属性信息*/
private Map<String, String> fieldMap=new HashMap<String, String>();
//属性个数
private Integer fieldNum;
private List<String> fieldNames = new ArrayList<String>();
private List<String> methodNames = new ArrayList<String>();
//由一个泛型集合保存对象,以对外提供,然后遍历,每个bean对象就可以进行测试了
public ClassUtil(Class<T> className) {
methods = className.getMethods();
fs = className.getDeclaredFields();
//field.put(, value)
for (Field field : fs) {
fieldNames.add(field.getName());
fieldMap.put(field.getName(), field.getType().toString().substring(16));
}
try {
object = className.newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static String stringToInt(String string){
String str = string.substring(0, string.indexOf("."));
return str;
}
public <V> void setField(Class<T> className, Map<String, V> map)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException {
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true); // 设置些属性是可以访问的
//System.out.println(f.getName());
if (map.containsKey(f.getName())) {
//System.out.println(f.getName());
if (fieldMap.get(f.getName()).equals("String")) {
//System.out.println(f.getName());
f.set(object, map.get(f.getName()) );
} else {
if (fieldMap.get(f.getName()).equals("Integer")) {
//System.out.println(f.getName());
//System.out.println(map.get(f.getName()));
f.set(object, Integer.parseInt( stringToInt( (String) map.get(f.getName()))) );
} else {
if (fieldMap.get(f.getName()).equals("Long")) {
f.set(object, Long.parseLong(stringToInt( (String) map.get(f.getName()))) );
} else {
}
}
}
}
}
}
public void setAllField(Class<T> className,Map<String,ArrayList<String>> map) {
Map<String, String> map1=new HashMap<String, String>();
for (String string : map.keySet()) {
for (int i = 0; i < map.get(string).size(); i++) {
map1.put(string, map.get(string).get(i));
}
try {
setField(className, map1);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void printField() throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().startsWith("get")) {
System.out.print("methodName:" + method.getName() + "\t");
System.out.println("value:" + method.invoke(object));// 得到get
// 方法的值
}
}
}
public static void printMap(Map<String, ArrayList<String>> map1) {
for (String string : map1.keySet()) {
System.out.print(string+"\t");
for (int i = 0; i < map1.get(string).size(); i++) {
System.out.print(map1.get(string).get(i)+"\t");
}
System.out.println();
}
}
/**
* 根据map生产大批量的对象
* @param map
*/
public static void createInstance(Map<String,ArrayList<String>> map) {
}
public static void main(String[] args) throws InvocationTargetException {
Classes classes = new Classes();
InputStream excelFileName;
try {
excelFileName = new FileInputStream(new File("D:\\test.xls"));
ExcelReader excelReader=new ExcelReader(excelFileName);
Map<String, ArrayList<String>> map1=excelReader.readFormatContent(0);
System.out.println(map1);
//printMap(map1);
//Map<String, ArrayList> map=excelReader.readContent(0);
//printMap(map1);
Class cls = (Class) classes.getClass();
ClassUtil<Classes> classUtil = new ClassUtil<Classes>(cls);
classUtil.setAllField(cls, map1);
try {
classUtil.printField();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}