package reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* 输出javabean集合信息
* @author 13061494
*
*/
public class Main {
private static final String LINE = "\r\n";
private static final String VERTICAL = "|";
private static final String PRE_METHOD = "get";
private static final String CLASS = "getClass";
public static void main(String[] args) {
test();
}
public static void test() {
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
PeopleBean bean = new PeopleBean();
bean.setAge(i);
bean.setName("gao");
list.add(bean);
}
try {
System.out.println(parseToTxt("1","1", list.size(),"1",list));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 将bean转成txt
* @param returnCode 成功标记(0成功,非0失败)
* @param message 信息提示(可以为空)
* @param contentLine 内容记录数(不包含文件1、2行)
* @param content 文件的私有字段
* @param list 内容集合
* @return 将bean转成txt
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static String parseToTxt(String returnCode, String message, int contentLine, String content, List list)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
List<Method> methods = getMethods(list.get(0));
StringBuffer sb = new StringBuffer();
sb.append(parseToTxtByFirstLine(returnCode, message, contentLine, content)).append(LINE);
sb.append(parseToTxtBySecondLine(methods)).append(LINE);
sb.append(parseToTxtByContentData(list, methods));
return sb.toString();
}
/**
* 第一行为汇总信息,至少有前3个字段:成功标记(0成功,非0失败)|信息提示(可以为空)|内容记录数(不包含文件1、2行)。
* 从第4个字段起是文件的私有字段,用|隔开,具体由各文件自由约定
* @param returnCode 成功标记(0成功,非0失败)
* @param message 信息提示(可以为空)
* @param contentLine 内容记录数(不包含文件1、2行)
* @param content 文件的私有字段
* @return
*/
private static String parseToTxtByFirstLine(String returnCode, String message, int contentLine, String content){
StringBuffer sb = new StringBuffer();
return sb.append(returnCode).append(VERTICAL).append(message).append(VERTICAL).append(contentLine).append(VERTICAL).append(content).toString();
}
/**
* 获取bean中属性的get方法集合
* @param o bean
* @return bean中属性的get方法集合
*/
private static List<Method> getMethods(Object o){
Method[] methods = o.getClass().getMethods();
List<Method> returnMethods = new ArrayList<Method>();
// 获取满足方法名以get开头的但是不是getClass的所有get方法集合
for (Method method : methods) {
if (method.getName().startsWith(PRE_METHOD)
&& !CLASS.equals(method.getName())){
returnMethods.add(method);
}
}
return returnMethods;
}
/**
* 文件第二行为字段名称,字段名之间用“|”隔开
* @param methods bean中属性的get方法集合
* @return
*/
private static String parseToTxtBySecondLine(List<Method> methods){
StringBuffer sb = new StringBuffer();
for (Method method : methods) {
// 属性首字母应该是小写
sb.append(toLowerCase(method.getName().substring(PRE_METHOD.length()))).append(VERTICAL);
}
// 去除最后一个|
return sb.toString().substring(0, sb.length() - 1);
}
/**
* 文件第三行开始为数据内容,字段之间用“|”隔开
* @param list 内容集合
* @param methods bean中属性的get方法集合
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static String parseToTxtByContentData(List list, List<Method> methods) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
StringBuffer sb = new StringBuffer();
for (Object o : list) {
for (Method method : methods) {
sb.append(method.invoke(o)).append(VERTICAL);
}
// 将每行最后一个|替换为\r\n
sb.replace(sb.length() - 1, sb.length(), LINE);
}
return sb.toString();
}
private static String toLowerCase(String s){
return s.replace(s.substring(0, 1), s.substring(0, 1).toLowerCase());
}
}