Java VO converted to flex VO

Source: Internet
Author: User

Directly on the code

Package Com.cwap; Import Java.io.BufferedWriter; Import Java.io.File; Import Java.io.FileWriter; Import java.io.IOException;    Import Java.lang.reflect.Field; public class Convertjavavo2flexvo {private static final string[] Simplejavatypename = {"String", "Date", "Bigdecima       L "," Decimal "," Double "," Long "," Long "," Integer "," int "," Boolean "," Boolean "}; private static final string[] Simpleflextypename = {"String", "Date", "number", "number", "number", "Number", "          Number "," int "," int "," Boolean "," Boolean "};       private static final string[] Complexjavatypename = {"Java.util.List", "java.util.Collection"};       private static final string[] Complexflextypename = {"IList", "Icollectionview"};              private static final string[] Complexfleximportname = {"Mx.collections.IList", "Mx.collections.ICollectionView"};           private static String Getsimpletypename (Class c) {string simpletypename = C.getsimplename (); String ResUlt = null;                    for (int i = 0; i < simplejavatypename.length; i++) {if (Simplejavatypename[i].equals (Simpletypename)) {                   result = Simpleflextypename[i];               Break       }} return result;           } private static int getcomplexjavatypeindex (String typeName) {int result =-1;                   for (int i = 0; i < complexjavatypename.length; i++) {if (Complexjavatypename[i].equals (TypeName)) {                   result = i;               Break             }} return result;           } private static String Getcomplextypename (Class c) {string typeName = C.getname ();           int index = Getcomplexjavatypeindex (typeName);           if (Index >-1) return Complexflextypename[index];       return null; }//returns the VO type according to the type of the Java class object @SuppressWarnings ("UNChecked ") public static string Getclasstype (Class c) {string result = Getsimpletypename (c);           if (judgenotemptystr (result)) return result;           result = Getcomplextypename (c);           if (judgenotemptystr (result)) return result; Return "*";//other type set to unknown type}//repeat C character count times, used to format generated as file public static string repeat (string c, int co           UNT) {StringBuffer strbuf = new StringBuffer ();           for (int i = 0; i < count; i++) {strbuf.append (c);       } return strbuf.tostring (); /** * Generate Flex POJO * * @param pojoname * java POJO name * @param package Name * Flex Package Names * @param folder * The path where flex packages are located * @throws classnotfoundexceptio  n * @throws IOException */@SuppressWarnings ("unchecked") public static void Generateasfile (String Pojoname, String PACKagename, String folder) throws ClassNotFoundException, IOException {Class c = class.forname (Pojo           Name);              field[] fields = C.getdeclaredfields ();           As the Vo object name end plus VO flag file F = new file (getvofilename (folder, PackageName, C));           BufferedWriter bw = new BufferedWriter (new FileWriter (f));           StringBuffer contentbuf = new StringBuffer ();           Createpackagename (PackageName, C, contentbuf);           Createimport (fields, CONTENTBUF);              Createbindinfo (Pojoname, contentbuf);              Createclassheader (c, contentbuf);           Createproperites (fields, CONTENTBUF);           Contentbuf.append ("\ n");              Createemptyconstructor (c, contentbuf);           Creategetsetfunction (fields, CONTENTBUF);           Contentbuf.append (Gentabchars ());           Contentbuf.append ("}\n");           Contentbuf.append ("}");           Bw.write (Contentbuf.tostring ());       Bw.close (); } Private StatIC void Createimport (field[] fields, StringBuffer strbuf) {strbuf.append ("\ n");               Write attribute for (int i = 0; i < fields.length; i++) {Class FieldType = Fields[i].gettype ();               int index = Getcomplexjavatypeindex (Fieldtype.getname ());                                  if (Index >-1) {Strbuf.append (Gentabchars ());                   Strbuf.append ("import");                   Strbuf.append (Complexfleximportname[index]);               Strbuf.append ("; \ n");                  }} strbuf.append ("\ n"); } private static void Createproperites (field[] fields, StringBuffer strbuf) {//write property for (int i = 0; i < fields.length; i++)               {Class FieldType = Fields[i].gettype ();               String typeName = Getclasstype (FieldType);               Strbuf.append (Genfunctiontabchars ()); Strbuf.append ("Private var _");               Strbuf.append (Fields[i].getname ());               Strbuf.append (":");               Strbuf.append (TypeName);           Strbuf.append ("; \ n");           }} private static void Creategetsetfunction (field[] fields, StringBuffer contentbuf) { Write Setter/getter method for (int i = 0; i < fields.length; i++) {Class FieldType = fields[i               ].gettype ();               String typeName = Getclasstype (FieldType);               Createsetfunction (fields, Contentbuf, I, typeName);              Creategetfunction (fields, Contentbuf, I, typeName); }} private static void Createclassheader (Class C, StringBuffer contentbuf) {//write class con           Tentbuf.append (Gentabchars ());           Contentbuf.append ("public class");           Contentbuf.append (C.getsimplename ());           Contentbuf.append ("vo\n");           Contentbuf.append (Gentabchars ());     Contentbuf.append ("{\ n");  } private static void Createemptyconstructor (Class C, StringBuffer strbuf) {//write empty constructor str           Buf.append (Genfunctiontabchars ());           Strbuf.append ("public Function");           Strbuf.append (C.getsimplename ());       Strbuf.append ("VO () {}\n\n"); } private static void CreateFolder (String folderName) {try {file MyPath = new file (fold               Ername);               if (!mypath.exists ()) {mypath.mkdirs ();           }} catch (Exception e) {e.printstacktrace ();           }} private static string Getvofilename (String folder, String PackageName, Class c) {           String fd = folder;           if (Judgenotemptystr (FD)) {if (!folder.endswith (file.separator)) fd + = File.separator;           } String p1 = PackageName; if (!judgenotemptystr (packagename)) P1 = C.getpackage (). GetName ();           FD + = P1.replace ('. ', File.separatorchar) + file.separator;           CreateFolder (FD);       return FD + c.getsimplename () + "vo.as"; } private static Boolean judgenotemptystr (String value) {return (null! = value) && (Value.leng       Th () > 0); } private static void Creategetfunction (field[] fields, StringBuffer strbuf, int i, String typeName           ) {//Getter Strbuf.append (Genfunctiontabchars ());           Strbuf.append ("Public function get");           Strbuf.append (Fields[i].getname ());           Strbuf.append ("():");           Strbuf.append (TypeName);           Strbuf.append ("{\ n");           Strbuf.append (Gencontenttabchars ());           Strbuf.append ("return this._");           Strbuf.append (Fields[i].getname ());           Strbuf.append ("; \ n");       Genfunctionend (STRBUF); } private static void Genfunctionend (StringBuffer strbuf) {strbuf.append (GenfunctiontAbchars () + "}\n\n"); } private static void Createsetfunction (field[] fields, StringBuffer strbuf, int i, String typeName           ) {//Setter Strbuf.append (Genfunctiontabchars ());           Strbuf.append ("Public function set");           Strbuf.append (Fields[i].getname ());           Strbuf.append ("(Value:");           Strbuf.append (TypeName);           Strbuf.append ("): void{\n");           Strbuf.append (Gencontenttabchars ());           Strbuf.append ("This._");           Strbuf.append (Fields[i].getname ());           Strbuf.append ("= value;\n");       Genfunctionend (STRBUF);           } private static String getcounttabchars (int count) {StringBuffer Strbuff = new StringBuffer ();           for (int i = 0; i < count; i++) {Strbuff.append (Gentabchars ());                  } return strbuff.tostring (); } private static String Gencontenttabchars () {return GETCOUNTTABCHARS (3);       } private static String Genfunctiontabchars () {return getcounttabchars (2); } private static void Createbindinfo (String pojoname, StringBuffer strbuf) {//write bind header Strbuf           . Append (Gentabchars ());           Strbuf.append ("[bindable]\n");           Strbuf.append (Gentabchars ());           Strbuf.append ("[Remoteclass (alias=\");           Strbuf.append (Pojoname);       Strbuf.append ("\")]\n ");       } private static String Gentabchars () {return repeat ("", 4);           } private static void Createpackagename (String packagename, Class C, StringBuffer strbuf) {               Package name, do not set the package name to take the same package name as Java Pojo if ((null! = PackageName) && (Packagename.trim (). Length () > 1)) {           Strbuf.append ("package" + c.getpackage (). GetName () + "\n{\n");           } else {strbuf.append ("package" + PackageName + "\n{\n");  }}//Generated main program     public static void Main (string[] args) throws ClassNotFoundException, IOException {string[]              POJOs = {"Com.cwap.Person"};                       for (int i = 0; i < pojos.length; i++) {Convertjavavo2flexvo.generateasfile (pojos[i],           "Com.cwap", "d:\\ work Area \\myworkspace\\test\\src\\");    }          }      }


Java VO converted to flex VO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.