1 Packagecom.walkerjava.test; 2 3 ImportJava.lang.reflect.Field; 4 Importjava.lang.reflect.InvocationTargetException; 5 ImportJava.lang.reflect.Method; 6 Importjava.util.Date; 7 8 /*** 9 * Traverse the attributes and data types of an entity class and attribute valuesTen * One * @authorLibaozhen A * @date 2013-1-4 a.m. 10:25:02 - * @company - * @versionv1.3 the * @seeRelated Classes - * @sinceRelated/Version - */ - Public classReflecttest { + Public Static voidReflecttest (Object model)throwsNosuchmethodexception, - illegalaccessexception, IllegalArgumentException, + InvocationTargetException { A //gets all properties of the entity class, returns the field array atfield[] field =Model.getclass (). Getdeclaredfields (); - //Traverse All Properties - for(intj = 0; J < Field.length; J + +) { - //gets the name of the property -String name =Field[j].getname (); - //capitalize the first character of a property to make it easy to construct a Get,set method inName = name.substring (0, 1). toUpperCase () + name.substring (1); - //gets the type of the property toString type =Field[j].getgenerictype (). toString (); + //if type is a class type, it is preceded by "class", followed by the class name -System.out.println ("property is:" +name); the if(Type.equals ("Class java.lang.String"))) { *Method m = Model.getclass (). GetMethod ("get" +name); $ //Call getter method to get property valuePanax NotoginsengString value =(String) M.invoke (model); -SYSTEM.OUT.PRINTLN ("Data type: String"); the if(Value! =NULL) { +System.out.println ("Property value is:" +value); A}Else { theSystem.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"); the if(Value! =NULL) { -System.out.println ("Property value is:" +value); Wuyi}Else { theSystem.out.println ("Property value is: null"); - } Wu } - if(Type.equals ("Class Java.lang.Short"))) { AboutMethod 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); A}Else { +System.out.println ("Property value is: null"); the } - } $ if(Type.equals ("Class java.lang.Double"))) { theMethod m = Model.getclass (). GetMethod ("get" +name); theDouble value =(Double) M.invoke (model); theSYSTEM.OUT.PRINTLN ("Data type: Double"); the if(Value! =NULL) { -System.out.println ("Property value is:" +value); in}Else { theSystem.out.println ("Property value is: null"); the } About } the if(Type.equals ("Class Java.lang.Boolean"))) { theMethod m = Model.getclass (). GetMethod ("get" +name); theBoolean value =(Boolean) M.invoke (model); +SYSTEM.OUT.PRINTLN ("Data type: Boolean"); - if(Value! =NULL) { theSystem.out.println ("Property value is:" +value); Bayi}Else { theSystem.out.println ("Property value is: null"); the } - } - if(Type.equals ("Class Java.util.Date"))) { theMethod m = Model.getclass (). GetMethod ("get" +name); theDate value =(Date) M.invoke (model); theSYSTEM.OUT.PRINTLN ("Data type: Date"); the if(Value! =NULL) { -System.out.println ("Property value is:" +value); the}Else { theSystem.out.println ("Property value is: null"); the } 94 } the } the } the}
How to traverse the attributes and data types and property values of an entity class in Java