Compute Bean's and (Java)

Source: Internet
Author: User

When there are too many properties in a bean, it is a very troublesome thing to calculate the sum of a bean's list, which can be solved with the reflection mechanism of Java, the code is as follows:

 Packagecom.jzzhcs.utils;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;Importjava.util.ArrayList;Importjava.util.List;/*** System Generic Tool class * *@authorJeffy **/ Public classUtil {/*** Traverse Bean's Properties * *@authorJeffy *@paramE * Bean Object *@throwsException*/     Public Static voidReflect (Object e)throwsException {Class<?extendsObject> CLS =E.getclass (); Field[] Fields=Cls.getdeclaredfields ();  for(inti = 0; i < fields.length; i++) {Field F=Fields[i]; F.setaccessible (true); System.out.println ("Property Name:" + f.getname () + "property value:" +F.get (e)); }    }    /*** COMPUTE Bean's and * Use the Java reflection principle to get the property value of the incoming object and the Getset method *@authorJeffy *@paramE * Bean Object *@throwsException*/     Public StaticObject Getsumofbean (list<?> List)throwsException {Class<?extendsObject> cls = list.get (0). GetClass (); Object Resultbean=cls.newinstance ();  for(Object o:list) {field[] fields=Cls.getdeclaredfields ();  for(inti = 0; i < fields.length; i++) {Field F=Fields[i]; //because it's written in private, it must be setF.setaccessible (true); //defining property names and property valuesString FieldName =F.getname (); Object Fieldvalue=F.get (o); //defines the name of the variable used to construct the get, set methodString name = fieldname.substring (0, 1). toUpperCase () + fieldname.substring (1); //defining get, Set methodsMethod GetMethod = Resultbean.getclass (). GetMethod ("get" +name); //call the Get methodObject value =Getmethod.invoke (Resultbean); //gets the type of the propertyString type =F.getgenerictype (). toString (); //if type is a class type, it is preceded by "class", followed by the class name                if(Type.equals ("Class java.lang.String"))) {                }                if(Type.equals ("Class java.lang.Double")) {                    if(Fieldvalue! =NULL) {//System.out.println ("Property name:" + name);//System.out.println ("attribute value is:" + value);Method Setmethod = Resultbean.getclass (). GetMethod ("set" + name,double.class); if(Double) Value! =NULL) {Setmethod.invoke (Resultbean, (Double) value+(Double) fieldvalue); } Else{Setmethod.invoke (Resultbean, (Double) fieldvalue); }//System.out.println (Getmethod.invoke (Resultbean));}Else{System.out.println ("Property value is: null"); }                }            }        }        returnResultbean; }     Public Static voidMain (string[] args)throwsException {Entity e=NewEntity (); List<Entity> list =NewArraylist<entity>(); E.SETD1 (1.0); E.SETD2 (3.0); E.SETD3 (7.0);        List.add (e); E=NewEntity (); E.SETD1 (13.0); E.SETD2 (17.0); E.SETD3 (23.0);        List.add (e); Object o=Getsumofbean (list); return ; }}

The Getsumofbean method is the primary logic that iterates through all the beans in the list, sums the attributes of the bean's double type, and, by reflection, invokes the set method of the result bean, storing the property values in the resulting bean.

Here is the definition of the bean:

 Packagecom.jzzhcs.utils; Public classEntity {Privatestring S1 = "String 1"; PrivateDouble D1; PrivateDouble D2; PrivateDouble D3;  PublicString getS1 () {returnS1; }     Public voidsetS1 (String s1) { This. S1 =S1; }     PublicDouble getD1 () {returnD1; }     Public voidsetD1 (Double d1) { This. D1 =D1; }     PublicDouble getD2 () {returnD2; }     Public voidsetD2 (Double D2) { This. d4 {D2; }     PublicDouble getD3 () {returnD3; }     Public voidsetD3 (Double D3) { This. d3 =D3; }  }

Compute Bean's and (Java)

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.