Java record-86-Reflection API example 2
Obtain information under a specified class: all methods and attributes
Public class DumpClassInfo {public static void main (String [] args) throws Exception {// basic function of Reflection API Class <?> Classtype = Class. forName ("my. reflect. Customer"); Method [] methods = classtype. getDeclaredMethods (); // obtain all methods under the specified Class, including private methods. (All methods at runtime) for (Method method: methods) {System. out. println (method);} Field [] fields = classtype. getDeclaredFields (); // obtain all attributes of a specified class, including private attributes. (ALL runtime attributes) for (Field field: fields) {System. out. println (field) ;}}} class Customer {private long id; private String name; private int age; public Customer () {} public Customer (String name, int age) {this. name = name; this. age = age;} public long getId () {return id;} public void setId (long id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
Output results of the above program:
public void my.reflect.Customer.setId(long)public int my.reflect.Customer.getAge()public void my.reflect.Customer.setAge(int)public java.lang.String my.reflect.Customer.getName()public long my.reflect.Customer.getId()public void my.reflect.Customer.setName(java.lang.String)private long my.reflect.Customer.idprivate java.lang.String my.reflect.Customer.nameprivate int my.reflect.Customer.age