通过反射生成类的utils
package com.gu.utils; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Map; import som.gu.core.DBManager; import som.gu.core.MysqlTypeConvertor; import som.gu.core.tableContext; import som.gu.core.typeConvetor; import com.gu.bean.JavaFieldGetSet; import com.gu.bean.columnInfo; import com.gu.bean.tableInfo; /** * 生成类源码常用的操作 * @author 谷 * */ public class javaFieldUtils { /** * 生成类某属性和get,set方法 * @param column * @param converter * @return */ public static JavaFieldGetSet createjavaFieldSetGetSRC(columnInfo column,typeConvetor converter){ JavaFieldGetSet jfgs=new JavaFieldGetSet(); String javaFieldType=converter.databassType2javatype(column.getDateType()); jfgs.setFieldInfo("\tprivate "+javaFieldType+" "+column.getName()+";\n"); //get方法 StringBuilder str=new StringBuilder(); str.append("\tpublic "+javaFieldType+" Get"+StringUtils.firstChar2uppercase(column.getName())+"(){\n"); str.append("\t\treturn "+column.getName()+";\n\t}\n"); jfgs.setGetInfo(str.toString()); //set方法 StringBuilder set=new StringBuilder(); set.append("\tpublic "+" void Set"+StringUtils.firstChar2uppercase(column.getName())+"("+javaFieldType+" "+column.getName()+"){\n"); set.append("\t\tthis."+column.getName()+"="+column.getName()+";\n\t}\n"); jfgs.setSetInfo(set.toString()); return jfgs; } /** * 生成某个类中所有信息 * @param table * @param conver * @return */ public static String JavaSrc(tableInfo table,typeConvetor conver){ StringBuilder src=new StringBuilder(); Map<String, columnInfo> columns=table.getColumns(); List<JavaFieldGetSet>list=new ArrayList<>();//所有属性列表 for(columnInfo column:columns.values()){ list.add(createjavaFieldSetGetSRC(column, conver)); } //生成包名; src.append("package "+DBManager.getPoPackage()+";\n"); //生成类名 src.append("public class "+StringUtils.firstChar2uppercase(table.gettName())+" {\n"); //生成属性 for(JavaFieldGetSet f:list){ src.append(f.getFieldInfo()); } src.append("\n\n"); //生成get,set方法 for(JavaFieldGetSet f:list){ src.append(f.getGetInfo()); } for(JavaFieldGetSet f:list){ src.append(f.getSetInfo()); } src.append("}"); return src.toString(); } /** * 生成表对应的java文件 * @param table * @param conver */ public static void creatJavaPoFile(tableInfo table,typeConvetor conver){ String po=JavaSrc(table,conver); String srcPath=DBManager.getSrcPath().replaceAll("\\\\", "/");; String PoPackagePath=DBManager.getPoPackage().replaceAll("\\.","/"); //String filePath=srcPath+"/"+PoPackagePath+"/"+table.gettName()+".java"; File f=new File(srcPath+"/"+PoPackagePath); if(!f.exists()){ f.mkdirs(); } // System.out.print(f.getAbsolutePath()); BufferedWriter writer=null; try { writer=new BufferedWriter(new FileWriter(f.getAbsolutePath()+"/"+StringUtils.firstChar2uppercase(table.gettName())+".java")); writer.write(po); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }