Traversing the tree and data type level attribute values of an entity class
/*** Traverse the attributes and data types of the entity class and attribute values *@parammodel *@throwsnosuchmethodexception *@throwsillegalaccessexception *@throwsIllegalArgumentException *@throwsinvocationtargetexception*/ Public Static voidReflecttest (Object model)throwsnosuchmethodexception, Illegalaccessexception, IllegalArgumentException, Invoca tiontargetexception {//gets all properties of the entity class, returns the field arrayfield[] field =Model.getclass (). Getdeclaredfields (); //Traverse All Properties for(intj = 0; J < Field.length; J + +) { //gets the name of the propertyString name =Field[j].getname (); //capitalize the first character of a property to make it easy to construct a Get,set methodName = name.substring (0, 1). toUpperCase () + name.substring (1); //gets the type of the propertyString type =Field[j].getgenerictype (). toString (); //if type is a class type, it is preceded by "class", followed by the class nameSystem.out.println ("property is:" +name); if(Type.equals ("Class java.lang.String")) ) {Method m= Model.getclass (). GetMethod ("get" +name); //Call getter method to get property valueString value =(String) M.invoke (model); System.out.println ("Data type: String"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } if(Type.equals ("Class Java.lang.Integer")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Integer value=(Integer) M.invoke (model); System.out.println ("Data type: Integer"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } if(Type.equals ("Class Java.lang.Short")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Short value=(short) m.invoke (model); System.out.println ("Data type: Short"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } if(Type.equals ("Class java.lang.Double")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Double value=(Double) M.invoke (model); System.out.println ("Data type: Double"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } if(Type.equals ("Class Java.lang.Boolean")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Boolean value=(Boolean) M.invoke (model); System.out.println ("Data type: Boolean"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } if(Type.equals ("Class Java.util.Date")) ) {Method m= Model.getclass (). GetMethod ("get" +name); Date value=(Date) M.invoke (model); System.out.println ("Data type: Date"); if(Value! =NULL) {System.out.println ("Property value is:" +value); } Else{System.out.println ("Property value is: null"); } } } }
Since I have a double type in my entity bean, I don't want to use the method of encapsulating the class to get the value, so I added a piece of code on his basis,
if (Type.equals ("Double" = Model.getclass (). GetMethod ("Get" + name); double value = (double ) M.invoke (model); System.out.println ( "Data type: Double" if (value >0 property value: "+ value); else {SYSTEM.OUT.PRI Ntln ( property value: null " property type is: "+type);
This way, you can know what the attribute type is, and you can add a good judgment statement. Of course, there are a lot of things to optimize the original code, I do not optimize here, ready to wait for me to integrate it with the POI after the optimization.
Java traverses the attributes and data types of entity classes and property values