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 () {}// the VO type is returned Based on the Java class object type @ 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 "*"; // other types are set to unknown type }}// repeated C characters count, used to format the generated as file public static string repeat (string C, int count) {string temp = ""; for (INT I = 0; I <count; I ++) {temp + = C;} return temp ;} /*** generate flex pojo ** @ Param pojoname * Java pojo name * @ Param packagename * Name of the Flex package * @ Param folder * path of the Flex package * @ 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 (); // Add the VO sign file F = new file (Folder + C. getsimplename () + ". as "); bufferedwriter BW = new bufferedwriter (New filewriter (f); // package name. If no package name is set, use the same package name as Java pojo if (stringutils. isempty (packagename) {BW. write ("package" + C. getpackage (). getname () + "\ n {\ n");} else {BW. write ("package" + packagename + "\ n {\ n");} // write class BW. write (Repeat ("", 4) + "public class" + C. getsimplename () + "\ n"); BW. write (Repeat ("", 4) + "{\ n"); // write attribute 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"); // write an empty constructor BW. write (Repeat ("", 8) + "public function" + C. getsimplename () + "() {}\ n \ n"); // write the setter/getter Method 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"); // 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");} BW. write (Repeat ("", 4) + "} \ n"); BW. write ("}"); BW. close () ;}// the generated main program 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 :\\");}}}
It's easy to use. After importing the corresponding package, modify the javavo path in the main function and generate the asvo path, and then run it.