Notes for combined development of flex and Java

Source: Internet
Author: User
In pojo of Java, such member variables cannot exist.
Srate,
It means that the first letter cannot only be lowercase, and the first two letters are at least lowercase. Otherwise, it is bound with Flex vo.

The following is a tool class that generates flex VO from Java pojo.

Package CN. bestwiz. Design. util;

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;

/**
* @ Author Administrator
*
*/
Public class voutils {
Public voutils (){
}

// Return the VO type based on the Java Class Object Type
Public static string getclasstype (Class C ){
String typename = C. getsimplename ();

If (typename. Equals ("string") | typename. Equals ("date ")
| Typename. Equals ("Boolean") | typename. Equals ("int ")){
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 ("timestamp ")){
Return "date ";
} Else if (typename. Equals ("Boolean ")){
Return "Boolean ";
}

Else {
Return "*"; // other types are set to unknown.
}

}

// Repeat the C Character Count for formatting 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 an as file
*
* @ Param pojoname
* Java object name
* @ Param packagename
* Package name of the vo object in flex
* @ Throws classnotfoundexception
* @ Throws ioexception
*/
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 to the end of the VO Object Name of
File F = new file (Folder + C. getsimplename () + "VO. ");
Bufferedwriter BW = new bufferedwriter (New filewriter (f ));
// Package name. If no package name is set, the package name is the same as that of Java pojo.
If (packagename = NULL ){
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 () + "VO/N ");
Bw. Write (Repeat ("", 4) + "{/N ");

// Write attributes
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 ()
+ "VO () {}/ n/n ");

// Write the setter/getter Method
For (INT I = 0; I <fields. length; I ++ ){
Class fieldtype = Fields [I]. GetType ();
String typename = getclasstype (fieldtype );
// Setter
Bw. 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 ");
// Getter
Bw. 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 ();
}

// Test. A test is written.
Public static void main (string [] ARGs) throws classnotfoundexception,
Ioexception {
// Todo auto-generated method stub
String [] pojos = {"cn. bestwiz. Design. tc. tick "};
For (INT I = 0; I <pojos. length; I ++ ){
// Class C = Class. forname (pojos [I]);
// System. out. println ("registerclassalias ('" + pojos [I] + "', Com. nstar. orderexpress. VO. "+ C. getsimplename () + "VO );");
Voutils. generateasfile (pojos [I], "", "E :/");
}

}

}

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.