Reflection
java is a static language. Because of the existence of reflection mechanism, it can become a "quasi dynamic language";
Reflection allows the program to obtain the internal information of any class at run time, and can directly operate the internal properties and methods of any object;
Orthophoto and reflection
Positive: import package name - > instantiate through new - > get instantiated object;
Inverse: instantiate object - > getClass () method - > get complete class information;
Functions provided by reflection mechanism
Determine the class of any object at run time;
Construct the object of any class;
Judge the member variables and methods of any class;
Get generic information;
Call member variables and methods of any object
Processing annotation (annotation matching reflection and perfect matching);
Generate dynamic agent;
. . .
Advantages and disadvantages of reflection
Advantages: dynamic and flexible;
Disadvantages: the thief is slow, dozens of times worse than the normal new method;
Reflection is mainly a common API
java.lang.Class: represents a class;
java.lang.reflect.Method: represents the method of the class;
java.lang.reflect.Field: represents the member variable of the class;
java.lang.reflect.Constructor: constructor representing a class;
Get class instance
package com.yuxing.demo; public class GetClass { public static void main(String[] args) throws ClassNotFoundException { // Get through getClass Yuge y1 = new Yuge(); Class<?> c1 = y1.getClass(); // Get the package name through forName and load it Class<?> c2 = Class.forName("com.yuxing.demo.Yuge"); // Get directly from class Class<Yuge> c3 = Yuge.class; System.out.println(c1.hashCode()); System.out.println(c2.hashCode()); System.out.println(c3.hashCode()); // Get the class of the parent class through class Class<?> superclass = c1.getSuperclass(); System.out.println(superclass.getName()); } } class Yuge{ } // A class has only one class 142257191 142257191 142257191 java.lang.Object
When does class initialization occur
Active reference of class
1. When the virtual machine is started, the class where the main method is located will be initialized first;
2.new is the object of a class;
3. Call static members (except final constants) and static methods of the class;
4. Use Java The method of lang.reflect package makes reflection calls to the class;
5. When initializing a class, if its parent class is not initialized, its parent class will be initialized first;
Class initialization does not occur for passive references to classes
1. When accessing a static domain, only the class that actually declares the domain will be initialized. For example, if a static variable of the parent class is referenced through a subclass, the subclass will not be initialized;
public class TestStatic { public static void main(String[] args) { System.out.println(Son.a); } } @Data class Father{ static int a = 0; static{ System.out.println("The parent class is loaded"); } } @Data class Son extends Father{ static{ System.out.println("Subclass loaded"); } } The parent class is loaded 0
2. When declaring an array, it will not be initialized
public static void main(String[] args) { Son[] s = new Son[5]; } } @Data class Son extends Father{ static{ System.out.println("Subclass loaded"); } } No output
3. Reference constants will not be initialized
Get class information through class
Class<?> aClass = Class.forName("com.yuge.redistest.Father"); aClass.getMethods(); aClass.getDeclaredMethods(); aClass.getMethod("aaaa"); // Parameter is method name aClass.getDeclaredMethod("getAaaa"); aClass.getFields(); // Obtain methods, properties, construction methods, etc
Get the object using newInstance()
The newInstance() calls a parameterless construction. When the class has no parameterless construction method, an error will be reported
public class TestStatic { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException { // System.out.println(Son.a); Class<?> a = Class.forName("com.yuge.redistest.Father"); Object o = a.newInstance(); System.out.println(o); } } @Data class Father{ private int b; private String a; // public Father(){ // // } public Father( String a,int b){ this.a = a; this.b = b; } } Exception in thread "main" java.lang.InstantiationException: com.yuge.redistest.Father
When using newInstance(), get the constructor of class first and get the object by passing parameters
public class TestStatic { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { // System.out.println(Son.a); Class<?> a = Class.forName("com.yuge.redistest.Father"); // The parameter is the type class of the constructor parameter Constructor<?> constructor = a.getConstructor( String.class, int.class); Father o1 = (Father)constructor.newInstance("1", 2); System.out.println(o1); } } @Data class Father{ private int b; private String a; public Father( String a,int b){ this.a = a; this.b = b; } } Father(b=2, a=1)
Execution method, operation attribute
public class TestStatic { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException { Class<?> a = Class.forName("com.yuge.redistest.Father"); Father father = (Father) a.newInstance(); Method a1 = a.getMethod("getName", String.class); // incoke method. The parameters are the instance object and the parameters required by the method a1.invoke(father,"aas"); Field a2 = a.getDeclaredField("a"); // The a attribute in the Father class is private and cannot be assigned directly. Therefore, permission detection needs to be turned off before assignment a2.setAccessible(true); // Set method. The parameters are the instance object, and the value to be set a2.set(father,"asfs"); System.out.println(father); } } @Data class Father{ private int b; private String a; public void getName(String cc){ System.out.println(cc); } public Father(){ } public Father( String a,int b){ this.a = a; this.b = b; } }
Reflection operation is generic, direct Baidu, and there is little content
Reflection operation annotation
To get the annotation on the class, use class getAnnotations();
To get the annotation on the method, use method Getannotation (annotation class. class);
To get the annotation on the attribute, use field Getannotation (annotation class. class);