Reflection and proxies

1. Reflection

1 Overview

Reflection is a mechanism that allows Object objects to be dynamically generated in the running state, and properties, methods, and annotations can be obtained through Class information, and can be called dynamically, and object methods can be called dynamically.

2. Get the Class object

Get the Class object in the following ways:

  1. Through the object's getClass() method.
  2. Through the class member variables of the class.
  3. Through the forName() method of the Class class.
Student student = new Student();
//1. Through the getClass() method of the object.
Class<? extends Student> clazz = student.getClass();
//2. Through the class member variables of the class.
Class<Student> clazz1 = Student.class;
//3. Through the forName() method of the Class class.
Class<?> clazz2 = Class.forName("reflect.reflect.Student");

3. Create objects

Objects are created by:

//Call method: clazz.newInstance()
Student student1 = clazz.newInstance();

//Call the method after getting the constructor: constructor.newInstance()
Constructor<? extends Student> constructor = clazz.getConstructor();
Student student2 = constructor.newInstance();

Get properties, methods and annotations by:

//get attribute
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {

//access method
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
    Class<?>[] types = method.getParameterTypes();
    if (types != null && types.length > 0) {

//get annotation
Annotation[] annotations = clazz.getDeclaredAnnotations();
for (Annotation annotation : annotations) {

4. Summary

Advantages of reflection:

  1. It can dynamically obtain the attributes, methods and annotation information of the class in the running state, and can dynamically generate objects.

Disadvantages of reflection:

  1. The encapsulation of the class is destroyed, and private properties and methods can be read through reflection.

Two, agency

Proxy is a design pattern, which is used to enhance the function of the target object by extending the proxy class without expanding the target object. Its model is that the visitor accesses the target object through the proxy object. Generally, it includes three roles: proxy object, target object, and target object interface.

1. Static proxy

Static proxy means that the function to be extended by the target object has been determined in the proxy class at compile time, and the proxy object cannot be dynamically generated in the running state, which is not flexible enough.

Code implementation

2. Dynamic agent

Dynamic proxies can dynamically generate proxy objects in the running state, which is suitable for flexible generation of target objects in the running state.

(1) JDK native dynamic proxy

  • Implementation method: The key point of the native JDK dynamic proxy is to implement the InvocationHandler interface, which requires you to rewrite the invoke() method. In this method, it is enough to enhance the part that needs to be enhanced.
  • Implementation principle: Create the invoke() method that implements the InvocationHandler interface through the Proxy.newProxyInstance() method, and the bottom layer is done through reflection.

(2) CgLib dynamic proxy

  • Implementation method: This is a third-party lib implementation. It needs to introduce the cglib library. First, it still needs to implement the MethodInterceptor interface and rewrite the intercepter() method. This looks similar to the JDK native dynamic proxy.

(3) Summary

The difference between JDK dynamic proxy and CgLib dynamic proxy:

  1. JDK Proxy can be implemented in the native JDK by implementing the InvocationHandler interface. Its principle is to generate an anonymous class of the target class through reflection; while CgLib Proxy needs to introduce a third-party package. It uses asm to operate bytecode and generate A subclass of the target class, the bytecode of the subclass is loaded at runtime.
  2. As a proxy class, JDK proxy requires the target class to have an interface to be implemented as a proxy, that is, the proxy class can only be generated for the interface; CgLib Proxy can be implemented as a proxy without the target class having an interface.


  1. Java three proxy modes: static proxy, dynamic proxy and cglib proxy:

Tags: Java

Posted by brunosdiniz on Sun, 27 Nov 2022 23:27:40 +0530