Reflection is a relatively low-level content.
In the future, you hardly write it yourself? Why?
Because you will use various frameworks, the underlying implementation of the framework is reflection.
Learning today's content will help you understand the implementation mechanism of the framework. To lay the foundation for the framework of future learning.
classloader
reflex
Dynamic proxy (first, agent design pattern, then dynamic proxy)
// How do classes (our own classes and the classes provided by the system) load into memory?
The process of loading a class (. class file):
1. JVM reads. class file and checks its correctness and error.
2. Load your static properties or methods
3. Initialization object (execution of initialization block and construction method).
When does a class load?
1. When an object is created (the class is loaded only when it is first created). Person p = new Person();
2. Person.count = 100 when using static attributes (assignments and values);
3. When calling static methods
4. When creating subclass objects (you are the parent class).
What classes are loaded by the system? Class loader.
The function of class loader: read. class file into memory. // This process is loading.
There are three classloaders in java
Bootstrap ClassLoader root loader (rt.jar, the core class library of the loading system) is not available
Extension ClassLoader Extension Class Loader Class Loader (Load all jar packages under the Extension jar package ext folder)
System Class Loader.
The location to which the classPath environment variable you configure points.)
Reflection:
Common classes describe objects. Person class, used to describe what attributes and methods each Person object should have.
You may have many classes, such as String, Person, ArrayList,InputStreamReader
// All of the above classes have commonalities, behaviors and attributes.
If you want to describe these classes, how should you describe them? Use Class classes
Class Class: Class used to describe the class, you can describe the class has several attributes, several methods, several constructions, several interfaces...
In Java becoming a language, any data type (including the basic data type) has a Class object to describe it.
To learn more about Class classes, the first thing to do is to get an object of Class type.
There are three ways to get Class objects
Mode 1: class name. class attribute
Mode 2: Object. getClass() method
Mode 3: Class.forName("Class name (including package name)")
What is reflection?
When a program runs, once you know a class (class name), you can get all the attributes and methods of that class.
At the same time, you can create objects of this class and call all attributes (including private) and all methods (including private).
This mechanism is called reflex.
The basis of reflection is the Class object. Class object can do what you want to achieve.
Retrospective summary
Reflection:
If you know the name of a class, you can
Get all the attributes and methods of this class, and be able to access the attributes and methods.
Reflections can actually be accessed:
Field: public, default, protected, private modified
Membership attributes and class attributes (static).
// Batch acquisition attributes:
// (1) Get all public attributes (including those inherited from the parent class).
getFields()
// (2) Get all attributes (remember: inheritance-free)
getDeclaredFields()
// (3) Get the specified public property (including the inherited parent class)
getField(String fieldName)
// (4) Get the specified attributes (remember: inheritance-free)
getDecalaredField(String fieldName)
Method: public, default, protected, private modifiers
// (1) Get all public methods (including those inherited from the parent class)
// (2) Obtain all methods (remember: inheritance-free)
// (3) Get the specified public method (with parent inheritance)
// (4) Get the specified method (remember: inheritance-free)
Constructor: public, default, protected, private
// (1) Obtain all public construction methods
// (2) Obtain all construction methods
// (3) Get the specified public constructor
// (4) Get the specified constructor
Class: The parent class is also a Class.
Class: Interfaces in Java are treated as Classes.
Get generic
Get annotations
Reflective uses:
(1) Access your private attributes and methods.
Step 1: Get private attributes or methods
Step 2: Set the setAccessable of the property or method to true. (Turn off JVM check for access modifiers)
Step 3: Setting properties or calling methods
(2) bypass generic type checking
Step 1: Get the Method to use
Step 2: Call methods
(3) Many frameworks make your code more flexible by reading files (e.g. XML files, properties files).
Application of Reflection: Dynamic Proxy
Agent? Design Patterns
Dynamic proxy?
//
package com.lanou3g.proxy; public interface BossInterface { public abstract void meet(); //Meet? public abstract void consume(); //consumption }
//
package com.lanou3g.proxy; public class CoalBoss implements BossInterface{ @Override public void meet() { System.out.println("Coal Boss Meet"); } @Override public void consume() { System.out.println("Coal owner is consuming"); } }
//
package com.lanou3g.proxy; public class CoalBossProxy implements BossInterface { private CoalBoss boss; public CoalBossProxy(CoalBoss boss) { super(); this.boss = boss; } @Override public void meet() { System.out.println("Preparations for meeting"); boss.meet(); System.out.println("Do things after meeting"); } @Override public void consume() { System.out.println("Book in advance..."); boss.consume(); System.out.println("Check"); } }
//
package com.lanou3g.proxy; import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) { CoalBoss cb = new CoalBoss(); // cb.consume(); // cb.meet(); CoalBossProxy cbp = new CoalBossProxy(cb); cbp.meet(); cbp.consume(); } }
Automatic agent
package com.lanou3g.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class DynamicProxy implements InvocationHandler { private Object target; //Target Objects - Objects that need to be proxied. //(Coal Boss, Steel Boss, etc.) public DynamicProxy(Object target) { super(); this.target = target; } //Get the proxy object. public Object newProxy() { return Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } //When a proxy object calls a method in the interface, the following code is executed. @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Pre-processing code"); Object o = method.invoke(target, args);//Execution of the original method System.out.println("Follow-up processing code"); return o; } }
//
package com.lanou3g.proxy; public interface Interface1 { public abstract void test(); }
//
package com.lanou3g.proxy; public class A implements Interface1 { @Override public void test() { System.out.println("Testing dynamic proxy is not easy to use"); } }
//
package com.lanou3g.proxy; public class TestProxy { public static void main(String[] args) { A a = new A(); DynamicProxy dp3 = new DynamicProxy(a); Interface1 proxy = (Interface1) dp3.newProxy(); proxy.test(); } }