1, What is reflection?
Reflection means that in the running state, all properties and methods of any class can be known, and any method of any object can be called. This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of java language. Reflection is very powerful, with both advantages and disadvantages.
Advantages: high flexibility. Because reflection belongs to dynamic compilation, that is, object instances are dynamically created & obtained only at runtime.
Disadvantages: low execution efficiency
Class: represents a class.
Field class: represents the member variables of the class (member variables are also called class properties).
Method class: represents the method of the class.
Constructor class: represents the construction method of the class.
2, Reflection common API
Basic class |
package cn.yx.zg.reflex.demo3; public class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public void eat() { System.out.println("Call parameterless method"); } public void eat(int num) { System.out.println("Call parameterized method"); } }
1. Three ways to get Class |
public static void main(String[] args) throws Exception { Class<Person> clazz1 = (Class<Person>) Class.forName("cn.yx.zg.reflex.demo3.Person"); Class clazz2 = Person.class; Person p = new Person(); Class clazz3 = p.getClass(); System.out.println(clazz1 == clazz2);//true System.out.println(clazz2 == clazz3);//true //Common ways try { Class npcClazz3 = Class.forName("com.Reflection mechanism.demo1.Person"); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
2. Get object with newInstance method |
The newInstance() method of class class uses the class's parameterless constructor to create objects. If a class does not have a parameterless constructor, it cannot be created in this way. You can call the getConstructor (String.class,int.class) method of class class to obtain a specified constructor function, and then call
public static void main(String[] args) throws Exception { Class clazz = Class.forName("cn.yx.zg.reflex.demo3.Person"); //Creating objects through parameterless construction Person p = (Person) clazz.newInstance(); System.out.println(p); //Creating objects by constructing method parameters Constructor c = clazz.getConstructor(String.class,int.class); //Get parameterized structure Person p2 = (Person) c.newInstance("Zhang San",23); //Creating objects with parametric constructs System.out.println(p2); }
3. The Field method operates on fields. Dynamically set the value for the Field and obtain the value |
Class. The getfield (string) method can obtain the specified field (visible) in the class. If it is private, it can be obtained with the getDeclaedField("name") method. The value of the field on the specified object can be set through the set(obj, "Li Si") method. If it is private, it is necessary to call setAccessible(true) to set the access permission, Call get(obj) with the obtained specified field to obtain the value of the field in the specified object
public static void main(String[] args) throws Exception { Class clazz = Class.forName("cn.yx.zg.reflex.demo3.Person"); Constructor c = clazz.getConstructor(String.class, int.class); //Get parameterized structure Person p = (Person) c.newInstance("Zhang Gang", 2); //Creating objects with parametric constructs //Field f = clazz.getField("name"); // Get name field //f.set(p, "Li Si"); // Modify the value of the name Field f = clazz.getDeclaredField("name"); //If the name field is a private permission field f.setAccessible(true); //Remove private permissions f.set(p, "Li Si"); //Type of name field Class<?> type = f.getType(); System.out.println(type); System.out.println(p); }
4. The invoke method dynamically calls the specified method |
Class.getMethod(String, Class...) and class The getdeclaraedmethod (string, class...) method can obtain the specified method in the class. This method can be called by calling invoke(Object, Object...)
public static void main(String[] args) throws Exception { Class clazz = Class.forName("cn.yx.zg.reflex.demo3.Person"); Constructor c = clazz.getConstructor(String.class, int.class); //Get parameterized structure Person p = (Person) c.newInstance("Zhang San", 23); //Creating objects with parametric constructs Method m = clazz.getMethod("eat"); //Get eat method m.invoke(p); Method m2 = clazz.getMethod("eat", int.class); //Get the eat method with parameters m2.invoke(p, 10); }
3, Summary
Reflection mechanism is the cornerstone of many Java frameworks!!!