Java gets all get methods and set methods in an object, reads the names of all variables in a class Java gets all get methods and set methods in an object, reads the names of all variables in a class

Java gets all get methods and set methods in an object, reads the names of all variables under a class

 
Copy code
All get methods and set methods 
public void getMethod (Object obj) { Class clazz = obj.getClass (); // Get entity class name Field [] fields = obj.getClass (). GetDeclaredFields (); // Get properties // Get all methods in Object object for (Field field: fields) ( PropertyDescriptor pd = new PropertyDescriptor (field.getName (), clazz); Method getMethod = pd.getReadMethod (); // Get get method //getMethod.invoke ( obj); // Here is the get method of executing the Object object Method setMethod = pd.getWriteMethod (); // Get the set method //setMethod.invoke(obj,"parameter");//Here is to execute the Object The set method of the object } }

Read the names of all variables under a class
public void getMethod (Class clz) {
// Get all the attribute fields in the corresponding class of the f object
Field [] fields = clz.getDeclaredFields ();
for (int i = 0, len = fields.length; i <len; i ++) {
// For each attribute, get the attribute name
String varName = fields [i] .getName ();
System.out.println ("variable The name is: "+ varName);
}
}
Copy code
Copy code
All get methods and set methods 
public void getMethod (Object obj) { Class clazz = obj.getClass (); // Get entity class name Field [] fields = obj.getClass (). GetDeclaredFields (); // Get properties // Get all methods in Object object for (Field field: fields) ( PropertyDescriptor pd = new PropertyDescriptor (field.getName (), clazz); Method getMethod = pd.getReadMethod (); // Get get method //getMethod.invoke ( obj); // Here is the get method of executing the Object object Method setMethod = pd.getWriteMethod (); // Get the set method //setMethod.invoke(obj,"parameter");//Here is to execute the Object The set method of the object } }

Read the names of all variables under a class
public void getMethod (Class clz) {
// Get all the attribute fields in the corresponding class of the f object
Field [] fields = clz.getDeclaredFields ();
for (int i = 0, len = fields.length; i <len; i ++) {
// For each attribute, get the attribute name
String varName = fields [i] .getName ();
System.out.println ("variable The name is: "+ varName);
}
}
Copy code

Guess you like

Origin www.cnblogs.com/aimei/p/12721522.html