java VO轉換AS VO的工具類

來源:互聯網
上載者:User
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的路徑,然後運行一下就行了,呵呵
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.