Static agent + dynamic agent is easy to use

Keywords: Java Android

Static agent + dynamic agent is easy to use

1, Brief description

1. From the perspective of coding, the agent mode is divided into static agent and dynamic agent.
Static proxy: the compiled class file already exists before the code runs.
Dynamic Proxy: dynamically generate Proxy class objects through reflection when the code is running. It aims to determine who the Proxy is through the code during project running. Java's reflection package provides a Proxy class and InvokationHandler interface, which can be combined to create a dynamic Proxy class.
Porxy: the porxy class creates a dynamic proxy class based on the passed parameters.
InvokationHandler: InvokationHandler is used to fire the methods of the dynamic proxy class.

2, Simple implementation of static proxy:

Example: I wanted to go shopping, but I didn't have time, so I asked my friends to help me buy it.

1. Define interface

public interface IShop {
    void buy();
}

2. I want to buy it, so I want to have this implementation

public class MyBuy implements IShop{
    @Override
    public void buy() {
        System.out.println("proxy:"+" my buy");
    }
}

3. I asked my friends to buy it for some reasons, so my friends also need to realize it. Moreover, my friends buy it for me, not for others, so my friends should also know that they buy it for me

public class MyFriends implements IShop{

    private MyBuy myBuy;

    public MyFriends(MyBuy myBuy) {
        this.myBuy = myBuy;
    }

    @Override
    public void buy() {
        myBuy.buy();
        System.out.println("proxy:"+" my friends buy");
    }
}

4. Finally, use the client class

public class Client {

    public static void main(String[] args) {
        //Create agent
        MyBuy myBuy = new MyBuy();
        MyFriends myFriends = new MyFriends(myBuy);
        myFriends.buy();
    }
}

3, Use of dynamic agents

1,First introduce the key classes
	Porxy Class: Porxy Class provides a static method to create a dynamic proxy class
public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException

Parameters:
ClassLoader: ClassLoader defines dynamic proxy classes, which can be obtained through classes or interfaces. For example:

//Create the classloader of the agent
ClassLoader classLoader = myBuy.getClass().getClassLoader();

Class<?> [] interfaces: the interface that the dynamic proxy class needs to implement

InvocationHandler: an instance of a class that implements the InvocationHandler interface

4, Implement dynamic proxy

1. Continuing with the above example, first create a dynamic proxy instance class that implements InvocationHandler

public class DynamicPurchasing implements InvocationHandler {

    private Object obj;

    public DynamicPurchasing(Object obj) {
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Object result = method.invoke(obj,args);
        if (method.getName().equals("buy")){
            System.out.println("proxy"+"DynamicPurchasing buy ing ^^^");
        }
        return result;
    }
}

In the dynamic proxy implementation class, we declare a reference of object type, which points to the proxy class. In this case, it is the proxy me. Calling the proxy method is executed in the invoke method.
Parameters:

  1. Object: the proxy object that implements the method
  2. Method: the method fired by the proxy instance, and the interface method in the Porxy parameter
  3. Object []: a series of parameters passed to the method

2. The rest is to modify the code in the client

public class Client {

    public static void main(String[] args) {
        //Create agent
        MyBuy myBuy = new MyBuy();
//        MyFriends myFriends = new MyFriends(myBuy);
//        myFriends.buy();
        //Create dynamic proxy
        DynamicPurchasing dynamicPurchasing = new DynamicPurchasing(myBuy);
        //Create the classloader of the agent
        ClassLoader classLoader = myBuy.getClass().getClassLoader();
        //Dynamically create proxy classes
        IShop purchasing = (IShop) Proxy.newProxyInstance(classLoader,new Class[]{IShop.class},dynamicPurchasing);
        purchasing.buy();
    }
}

Proxy.newProxyInstance(classLoader,new Class[]{IShop.class},dynamicPurchasing); Generate a dynamic proxy class and call the invoke method of the proxy instance.
Operation results:

Posted by WebDesEyen on Tue, 12 Oct 2021 19:41:01 -0700