public static void Testreflect (Object model) throws Nosuchmethodexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception{
field[] field = Model.getclass (). Getdeclaredfields (); Gets all properties of the entity class, returns the field array
for (int j=0; j<field.length; j + +) {//Traverse all properties
String name = Field[j].getname (); Gets the name of the property
System.out.println ("attribute name:" +name);
Name = name.substring (0,1). toUpperCase () +name.substring (1); Capitalize the first character of a property to make it easy to construct a Get,set method
String type = Field[j].getgenerictype (). toString (); Gets the type of the property
if (Type.equals ("Class java.lang.String")) {//if type is a class type, it is preceded by "class", followed by the class name
Method m = Model.getclass (). GetMethod ("get" +name);
String value = (string) m.invoke (model); Call getter method to get property value
if (value = null) {
System.out.println ("attribute value:" +value);
}
}
if (Type.equals ("Class Java.lang.Integer")) {
Method m = Model.getclass (). GetMethod ("get" +name);
Integer value = (integer) m.invoke (model);
if (value = null) {
System.out.println ("attribute value:" +value);
}
}
if (Type.equals ("Class Java.lang.Short")) {
Method m = Model.getclass (). GetMethod ("get" +name);
Short value = (short) m.invoke (model);
if (value = null) {
System.out.println ("attribute value:" +value); }
}
if (Type.equals ("Class java.lang.Double")) {
Method m = Model.getclass (). GetMethod ("get" +name);
Double value = (double) m.invoke (model);
if (value = null) {
System.out.println ("attribute value:" +value);
}
}
if (Type.equals ("Class Java.lang.Boolean")) {
Method m = Model.getclass (). GetMethod ("get" +name);
Boolean value = (Boolean) m.invoke (model);
if (value = null) {
System.out.println ("attribute value:" +value);
}
}
if (Type.equals ("Class Java.util.Date")) {
Method m = Model.getclass (). GetMethod ("get" +name);
Date value = (date) m.invoke (model);
if (value = null) {
System.out.println ("attribute value:" +value.tolocalestring ());
}
}
}
}
Traversing properties in a class in Java