Reflection allows your program code to access internal information of classes loaded into the JVM, allowing you to write and execute, rather than code collaboration in the source code. This makes reflections become the main tool for building flexible applications. First observe the following XML files:
Xml version = '1.0' encoding = 'gb2312'?>
The XML file describes a segment current process information, and then translates it into VO:
public class BidProcess implements Serializable {private String itemNo; private String itemName; private String subItemNo; private String subItemName; private String functionName; / ** * @return return functionName. * / Public string getFunctionName () {return functionname;} / ** * @Param FunctionName To set the functionName. * / Public void setFunctionName (String functionName) {this.functionName = functionname;} / ** * @return Returns ItemName. * / Public string getItemName () {return itemname;} / ** * @Param ItemName To set ItemName. * / Public void setItemName (String ItemName) {this.itemname = itemname;} / ** * @return Returns ItemNo. * / Public string getitemno () {return itemno;} / ** * @Param item Item Item Item * / Public void setitemno (string itemno) {this.itemno = itemno;} / ** * @return Returns SubiteMname. * / Public string getSubItemName () {return subitemname;} / ** * @Param SubItemName To set the substemname. * / Public void setSubiteMname (String SubiteMname) {this.subitemname = SUBITEMNAME;} / ** * @return Returns SubiteMno. * / Public string getSubitemno () {return subitemno;} / ** * @Param SubiteMno SubiteMno to set. * / Public void setsubitemno (String Subitemno) {this.subitemno = SUBITEMNO;}} From the XML constructed VO, put it in the list, the code is as follows:
Public class vofactory {// Context Object Collection private arraylist list = new arraylist (); / ** Using reflection acquisition result set * / public list parse (String XML) {Try {// Construct XML input stream BYTEARRAYINPUTSTREAM ba = new ByteArrayInputStream (xml.getBytes ()); // Dom4J initialization SAXReader reader = new SAXReader (); Document document = reader.read (ba); Element root = document.getRootElement (); // get the class object class c = Source.getClass (); // Get constructor constructor [] cons = c.getDeclaredConstructors (); constructor con = cons [0]; // Get field field in the class field [] fields = c.getDeclaredFields (); / / Set the permissions access to the private field accessible (fields, true); // Get an XML element list list lis = root.eference ("arsubitem") under a specific name; // "Arsubitem" Elements under Elements for (int " i = 0; i (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)