当前位置:网站首页>And play the little chestnut of dynamic agent

And play the little chestnut of dynamic agent

2022-07-05 07:16:00 I am the king of X

Go straight to the code

Premise

This needs to be understood in combination with another blog post :

Code

Interface :

package com.xmonster.demo2;

public interface IService {
    

    public void sayHello();

}

Real implementation class :

package com.xmonster.demo2;

public class RealService implements IService{
    
    @Override
    public void sayHello() {
    
        System.out.println("hello i am xmonster!");
    }
}

Tools + test :

package com.xmonster.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class SimpleInvocationHandler implements InvocationHandler {
    

    private Object realObj;
    public SimpleInvocationHandler(Object realObj) {
    
        this.realObj = realObj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
        System.out.println("entering"+method.getName());
        Object invokeResult = method.invoke(realObj, args);
        System.out.println("leaving"+method.getName());
        return invokeResult;

    }

    public static void main(String[] args) {
    
        IService realService = new RealService();
        IService proxy =(IService) Proxy.newProxyInstance(IService.class.getClassLoader(), new Class<?>[]{
    IService.class},
                new SimpleInvocationHandler(realService));
        proxy.sayHello();

    }
}

test result :
 Insert picture description here

newProxyInstance

Let's see newProxyInstance Three parameters of

private static Object newProxyInstance(ClassLoader loader, // null if no SecurityManager
                                       Constructor<?>[] interfaces,
                                       InvocationHandler h)

Parameters :

  1. ClassLoader loader Represents a class loader , Use the same class loader as the interface here
  2. interfaces Represents the list of interfaces to be implemented by the proxy class , Is an array , The type of element can only be interface , It cannot be a general class
  3. InvocationHandler It's an interface , Only one... Is defined invoke Method , Calls to all methods of the proxy interface will be transferred to this method for processing

newProxyInstance The return value of Object, Can be forced to interfaces An interface type in the array , Here, it is directly converted to IService type , Be careful : It cannot be forcibly converted to a class , such as RealService !

InvocationHandler

package java.lang.reflect;

public interface InvocationHandler {
    
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable;
}

Parameters :

  1. proxy Represents the agent itself , Be careful , He is not the object of being represented
  2. method Represents the method being called
  3. args Parameters representing methods
Object invokeResult = method.invoke(realObj, args);

So this is calling theta method Of invoke Method , What is passed is the actual object realObj, Can't be proxy Pass as parameter to method.invoke, There will be a dead cycle , Because it has this method itself
Here are method Of invoke Method source code

 @HotSpotIntrinsicCandidate
    public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException,
           InvocationTargetException
    {
    
        if (!override) {
    
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz,
                        Modifier.isStatic(modifiers) ? null : obj.getClass(),
                        modifiers);
        }
        MethodAccessor ma = methodAccessor;             // read volatile
        if (ma == null) {
    
            ma = acquireMethodAccessor();
        }
        return ma.invoke(obj, args);
    }
原网站

版权声明
本文为[I am the king of X]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140558358128.html