Automatically encapsulate objects into object objects using reflection, and encapsulate object Objects Using Reflection
When using this method, the name of the parameter to be passed must end with the row number. Removing the row number is the attribute name. For example, if name + rowNo is passed on the page, the attribute name of the object should be name. The Code is as follows:
// Obtain page data and automatically encapsulate it into the bean object public List getObjectList (Class clazz, String [] rowNos) throws Exception {List objList = new ArrayList (); for (int I = 0; rowNos! = Null & I <rowNos. length; I ++) {// create an Object instance object = clazz. newInstance (); // obtain the attribute Field [] fields = clazz. getDeclaredFields (); StringBuffer buffer = null; // traverses the attribute and runs the encapsulation for (int j = 0; j <fields. length; j ++) {// obtain the attribute name String fieldName = fields [j]. getName (); // obtain the parameter name String paraName = fields [j]. getName () + rowNos [I]; // if the obtained parameter value is null, the loop String value = getValueNull (paraName); if (value = null) {continue ;} // parameter value Object [] paramValue = new Object [1]; if (fields [j]. getType (). toString (). equals ("class java. lang. string ") {paramValue [0] = value;} if (fields [j]. getType (). toString (). equals ("class java. lang. integer ") {paramValue [0] = new Integer (value);} if (fields [j]. getType (). toString (). equals ("class java. lang. double ") {paramValue [0] = new Double (value);} if (fields [j]. getType (). toString (). equals ("class java. util. date ") {SimpleDateFormat sdf = new SimpleDateFormat (" yyyy-MM-dd "); paramValue [0] = sdf. parse (value);} // parameter type Class [] paramType = {fields [j]. getType ()}; // obtain the name of the set Method buffer = new StringBuffer ("set"); buffer. append (fieldName. substring (0, 1 ). toUpperCase (); buffer. append (fieldName. substring (1); // obtain Method = clazz. getDeclaredMethod (buffer. toString (), paramType); // execution method. invoke (object, paramValue);} // put the current object in the list objList. add (object) ;}return objList ;}