Why does the invoke () method in InvocationHandler have an Object proxy parameter?

When you verify that the invoke method (Object proxy, Method method, Object [] args) and the doc instruction, you will find that the input parameter is proxy

proxy - proxy instance that was called by the method

when I do a test on a dynamic Java proxy, I find that this proxy is created by vm.So. I want to know why the invoke method has this parameter, which is definitely nothing but an object ($ proxy0), but does not have an actual action for our use?

+3
source share
2 answers

, -. , - -. , Mokito test framework -.

+3

, http://rejoy.iteye.com/blog/1627405

$proxy0.class, , $proxy0

public final class $Proxy0 extends Proxy  
    implements UserService 

$proxy0 :

public final void add()  
    {  
        try  
        {  
            //the h(invocationhandler) is in Proxy class,so we need pass this $proxy0 instance to super ,so the super(Proxy) can invoke($proxy0,method,args)
            super.h.invoke(this, m3, null);  
            return;  
        }  
        catch(Error _ex) { }  
        catch(Throwable throwable)  
        {  
            throw new UndeclaredThrowableException(throwable);  
        }  
    }  
0

Source: https://habr.com/ru/post/1781223/


All Articles