Starting from an instance -- getting information about an object at runtime
1. First, we define a customer class to indicate the customer's information.
public class Customer {private Long id;private String name;private int age;private String phone;public Customer() {}public Customer(Long id,String name,int age,String phone) {this.id=id;this.name=name;this.age=age;this.phone=phone;}public final Long getId() {return id;}public void setId(Long id) {this.id=id;}public final String getName() {return name;}public final void setName(String name) {this.name=name;}public final int getAge() {return age;}public void setAge(int age) {this.age=age;}public final String getPhone() {return phone;}public void setAge(String phone) {this.phone=phone;}}
2. Now we write a class to obtain all the constructor methods, common methods, and attribute information of the customer instance.
Import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. method; public class reflectionapplication {/*** @ Param ARGs */public static void showobjectinfo (Object OBJ) throws exception {Class C = obj. getclass (); int I; system. out. println (obj. tostring () + "All constructor construction methods are:"); constructor [] cs = C. getconstructors (); for (I = 0; I <CS. length; I ++) {system. out. println (CS [I]. tostring ();} system. out. println (obj. tostring () + "all declared methods are:"); method [] Methods = C. getdeclaredmethods (); for (I = 0; I <methods. length; I ++) {system. out. println (methods [I]. tostring ();} system. out. println (obj. tostring () + "all declared attributes are:"); field [] fields = C. getdeclaredfields (); for (I = 0; I <fields. length; I ++) {system. out. println (fields [I]. tostring () ;}} public static void main (string [] ARGs) throws exception {// todo auto-generated method stubcustomer customer = new customer (001l, "wangzhicheng ", 28, "13866916216"); showobjectinfo (customer );}}
3. The running result is:
Customer @ 1fb8ee3: Public customer () Public customer (Java. lang. long, Java. lang. string, Int, Java. lang. string) Customer @ 1fb8ee3: Public void customer. setid (Java. lang. long) Public final int customer. getage () Public void customer. setage (INT) Public void customer. setage (Java. lang. string) Public final Java. lang. string customer. getphone () Public final Java. lang. string customer. getname () Public final Java. lang. long customer. GETID () Public final void customer. setname (Java. lang. string) all declared attributes of customer @ 1fb8ee3 are: Private Java. lang. long customer. idprivate Java. lang. string customer. nameprivate int customer. ageprivate Java. lang. string customer. phoneprivate Java. lang. string customer. phone
2. What is reflection?
From the above example, we can see that in the Java Runtime Environment, we can know the attributes and methods of any class or object. Furthermore, for any object, we can call any of its methods at runtime. Java reflection technology is used to dynamically obtain class information and call objects.
3. Why does reflection exist? -- Starting from the java. Lang. Class class
When a Java program is running, the Java runtime system always identifies all objects with the so-called runtime type. This job records the class to which each object belongs, the class is used to save information of these types. Therefore, we can use the class to obtain information about the class, as shown in the previous example.
Simple Application of four reflection technologies-copying objects during runtime
Import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. member; import Java. lang. reflect. method; public class copyobject {/*** @ Param ARGs */public static object copy (Object source) throws exception {class <?> C = source. getclass (); // obtain the object of the source type object OBJ = C. newinstance (); // obtain the source instance object field [] fields = C. getdeclaredfields (); // get the declared attribute for (INT I = 0; I <fields. length; I ++) {field = Fields [I]; string fieldname = field. getname (); // get each attribute string firstletter = fieldname. substring (0, 1 ). touppercase (); // convert the first letter of the attribute name to an uppercase string getmethodname = "get" + firstletter + fieldname. substring (1); // obtain the get method name string setmethodname = "set" + firstletter + fieldname. substring (1); // obtain the set method name method getmethod = C. getmethod (getmethodname, new class [] {}); // obtain the get method setmethod = C. getmethod (setmethodname, new class [] {field. getType ()}); // obtain the set method. The type of this method is field. gettypeobject value = getmethod. invoke (source, new object [] {}); // call the get method system. out. println (fieldname + ":" + value); setmethod. invoke (OBJ, new object [] {value}); // call the Set Method} return OBJ;} public static void main (string [] ARGs) throws exception {// todo auto-generated method stubcustomer customer = new customer (001l, "wangzhicheng", 28, "13866916216"); customer customercopy = (customer) Copy (customer ); system. out. println ("The Copied object is:"); system. out. println (customercopy. GETID () + "" + customercopy. getname () + "" + customercopy. getage () + "" + customercopy. getphone ());}}
Running result:
ID: 1 Name: wangzhichengage: 28 phone: 13866916216 the Copied object is: 1 wangzhicheng 28 13866916216
Reflection also has side effects, affecting Program Performance
6. More references: Java reflection in action