Java reflection usage, Java reflection usage
I. First, prepare a class to be reflected.
1 public class Person { 2 private String name; 3 4 private int age; 5 6 public String sex; 7 8 public int tel; 9 10 private Person(String name, int age) {11 this.name = name;12 this.age = age;13 }14 15 16 public Person() {17 18 }19 20 public Person(String name, int age, String sex, int tel) {21 this.name = name;22 this.age = age;23 this.sex = sex;24 this.tel = tel;25 }26 27 public String showInfo() {28 return "name:" + name + "-age:" + age + "-sex:" + sex + "-tel" + tel;29 }30 31 private String getName() {32 33 return name;34 }35 }
Ii. Use Reflection now
/*** Method for getting a Class Object of A Class ** @ param view * @ throws Exception */public void getClassMethod (View view) throws Exception {// There are three methods to obtain the class Object: Person person = new Person (); // The first Class class1 = person. getClass (); // The second Class class2 = Person. class; // The Third Class class3 = Class. forName ("com. adminhj. httpexercise. reflect. person "); Log. I (TAG, "class1 = class2 -->" + (class1 = class2); Log. I (TAG, "class2 = class3 -->" + (class2 = class3 ));}
The printed result is:
Com. adminhj. httpexercise. reflect. ReflectActivity: class1 = class2 --> true
Com. adminhj. httpexercise. reflect. ReflectActivity: class2 = class3 --> true
1/** 2 * Get the common constructor of the Person class through reflection 3*4 * @ param view 5 */6 public void getConstruct (View view) throws Exception {7 // load the class Object of the person Class 8 Class class1 = class. forName ("com. adminhj. httpexercise. reflect. person "); 9 // get the specified constructor public Person (String name, int age, String sex, int tel) 10 //, if there are no parameters in the Constructor constructor = class1.getConstructor (); 11 Constructor constructor = class1.getConstructor (String. class, int. class, String. class, int. class); 12 // new the Object 13 Object obj = constructor of the specified constructor. newInstance ("James", 22, "male", 110); 14 // obtain the public attribute 15 Field sex = class1.getField ("sex") in the reflection class "); 16 Field tel = class1.getField ("tel"); 17 Log. I (TAG, "sex:" + sex. get (obj) + "------- tel:" + tel. get (obj); 18 // get the public Method specified in the reflection class 19 Method showInfo = class1.getMethod ("showInfo"); 20 // The execution Method 21 String info = (String) showInfo. invoke (obj); 22 Log. I (TAG, "info" + "-------" + info); 23 24}
Print result:
1 com. adminhj. httpexercise. reflect. ReflectActivity: sex: Male ------- tel: 110
1 com. adminhj. httpexercise. reflect. ReflectActivity: info ------- name: James-age: 22-sex: Male-tel110
1/** 2 * obtain the private constructor of the Person class through reflection 3*4 * @ param view 5 * @ throws Exception 6 */7 public void getDeclaredConstruct (View view) throws Exception {8 // load the class Object of the person Class 9 Class class1 = class. forName ("com. adminhj. httpexercise. reflect. person "); 10 // obtain the specified Constructor private Person (String name, int age) 11 constructor Constructor = class1.getDeclaredConstructor (String. class, int. class); 12 // set this private constructor to be accessible. If this parameter is not set, 13 constructor is reported. setAccessible (true); 14 // create an instance Object 15 Object obj = constructor. newInstance ("ris", 2000); 16 17 18 // obtain all attributes including private, class1.getDeclaredField () this method is used to obtain all attributes excluding private 19 // 20 Field [] declaredFields = class1.getDeclaredFields (); 21 for (Field field: declaredFields) {22 Field. setAccessible (true); 23 24 // comment out 25 // field when using private familiarity. setAccessible (true); set the property to be visible 26 // field. set (obj, "Zhang Fei"); assign 27 28 Log values to the attribute. I (TAG, field. getName () + "-------" + field. get (obj); 29} 30 // get the private Method specified in the reflection class 31 Method getNameAndAge = class1.getDeclaredMethod ("getNameAndAge "); 32 // set the private method to run 33 getNameAndAge. setAccessible (true); 34 // execution Method 35 String result = (String) getNameAndAge. invoke (obj); 36 37 Log. I (TAG, result); 38}
Print result:
Com. adminhj. httpexercise. reflect. reflectActivity: age ------- 2000com. adminhj. httpexercise. reflect. reflectActivity: name ------- riscom. adminhj. httpexercise. reflect. reflectActivity: sex ------- nullcom. adminhj. httpexercise. reflect. reflectActivity: tel ------- 0
1 com. adminhj. httpexercise. reflect. ReflectActivity: name = ris---------- age = 2000
Learn how to cheer up your friends