Automatically encapsulates a solid object with reflection

Source: Internet
Author: User

The name of the parameter to be passed when using this method must end with the line number, minus the line number is the property name, such as page Pass Name+rowno, then the property name of the entity object should be name. The code is as follows

Get page data, automatically encapsulated as 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 objects = Clazz.newinstance ();// Gets the properties of the class declaration field[] fields = Clazz.getdeclaredfields (); StringBuffer buffer = null;//Traversal property, performing encapsulation for (int j=0;j<fields.length;j++) {//Gets the name of the property string fieldName = Fields[j]. GetName ();//Gets the name of the parameter string paraname = Fields[j].getname () +rownos[i];//If the Get parameter value is empty then continue looping 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 ");p Aramvalue[0]=sdf.parse (value);} Parameter type class[] paramtype= {fields[j].gettype ()};        Get the name of the Set method buffer = new StringBuffer ("set");          Buffer.append (fieldname.substring (0, 1). toUpperCase ());         Buffer.append (fieldname.substring (1));         Get put back method = Clazz.getdeclaredmethod (Buffer.tostring (), paramtype); Execution Method Method.invoke (Object,paramvalue);} Places the current object in the list Objlist.add (object);} Return objlist;}

Automatically encapsulates a solid object with reflection

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.