简体   繁体   中英

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

When you r checking out that the method invoke(Object proxy, Method method, Object[] args) declaration & the doc statement,you will find that the input parameter proxy

proxy - the proxy instance that the method was invoked on

when I am doing a test on java dynamic proxy,I find this proxy is produced by vm.So I do want to know why the method invoke has this param,which is surely nothing except that is just an object ($proxy0 )but don't have the actual action for our usage?

This very useful if you have single invocation handle for multiple proxy objects. So you can use hash map to store proxy states information. For example - Mokito test framework store proxy invocation history.

if you are chinese ,you can read this article http://rejoy.iteye.com/blog/1627405

he makes a decompliation of $proxy0.class, you should know that $proxy0 extends Proxy

public final class $Proxy0 extends Proxy  
    implements UserService 

and there is a function in $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);  
        }  
    }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM