Automatically encapsulate objects into object objects using reflection, and encapsulate object Objects Using Reflection

Source: Internet
Author: User

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 ;}

 

Related Article

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.