import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.lang.reflect.Field;import org.apache.commons.lang.StringUtils;public class GenerateFlexVO {public GenerateFlexVO() {}// 根據java類對象的類型返回vo類型@SuppressWarnings("unchecked")public static String getClassType(Class c) {String typeName = c.getSimpleName();if (typeName.equals("String") || typeName.equals("Date")) {return typeName;} else if (typeName.equals("BigDecimal") || typeName.equals("Decimal")|| typeName.equals("Double")) {return "Number";} else if (typeName.equals("Integer")) {return "int";} else if (typeName.equals("Boolean")) {return "boolean";} else {return "*";// 其它類型的設定為未知類型}}// 重複c字元count次,用于格式化產生的as檔案public static String repeat(String c, int count) {String temp = "";for (int i = 0; i < count; i++) {temp += c;}return temp;}/** * 產生FLex POJO * * @param pojoName * java POJO的名稱 * @param packageName * Flex包的名稱 * @param folder * Flex包所在的路徑 * @throws ClassNotFoundException * @throws IOException */@SuppressWarnings("unchecked")public static void generateAsFile(String pojoName, String packageName,String folder) throws ClassNotFoundException, IOException {Class c = Class.forName(pojoName);Field[] fields = c.getDeclaredFields();// as的vo對象名稱結尾加上VO標誌File f = new File(folder + c.getSimpleName() + ".as");BufferedWriter bw = new BufferedWriter(new FileWriter(f));// 包名,沒有設定包名就取和java pojo一樣的包名if (StringUtils.isEmpty(packageName)) {bw.write("package " + c.getPackage().getName() + "\n{\n");} else {bw.write("package " + packageName + "\n{\n");}// 寫類bw.write(repeat(" ", 4) + "public class " + c.getSimpleName() + "\n");bw.write(repeat(" ", 4) + "{\n");// 寫屬性for (int i = 0; i < fields.length; i++) {Class fieldType = fields[i].getType();String typeName = getClassType(fieldType);bw.write(repeat(" ", 8) + "private var _" + fields[i].getName()+ ":" + typeName + ";\n");}bw.write("\n\n\n");// 寫空的建構函式bw.write(repeat(" ", 8) + "public function " + c.getSimpleName()+ "(){}\n\n");// 寫 setter/getter 方法for (int i = 0; i < fields.length; i++) {Class fieldType = fields[i].getType();String typeName = getClassType(fieldType);// setterbw.write(repeat(" ", 8) + "public function set "+ fields[i].getName() + "(value:" + typeName + "):void{\n");bw.write(repeat(" ", 12) + "this._" + fields[i].getName()+ " = value;\n");bw.write(repeat(" ", 8) + "}\n\n");// getterbw.write(repeat(" ", 8) + "public function get "+ fields[i].getName() + "():" + typeName + "{\n");bw.write(repeat(" ", 12) + "return this._" + fields[i].getName()+ ";\n");bw.write(repeat(" ", 8) + "}\n\n\n");}bw.write(repeat(" ", 4) + "}\n");bw.write("}");bw.close();}// 產生的主程式public static void main(String[] args) throws ClassNotFoundException,IOException {String[] pojos = { "com.wgw.javavo.StudentVO" };for (int i = 0; i < pojos.length; i++) {// Class c = Class.forName(pojos[i]);// System.out.println("registerClassAlias('"+pojos[i]+"',login.flash.vo."+c.getSimpleName()+");");GenerateFlexVO.generateAsFile(pojos[i], "com.wgw.flexvo","E:\\");}}}
使用起來很簡單,匯入相應的包後,修改main函數裡面的javaVO路徑和產生ASVO的路徑,然後運行一下就行了,呵呵