[英]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 当你检查出方法调用(Object proxy,Method method,Object [] args)声明和doc语句时,你会发现输入参数代理
proxy - the proxy instance that the method was invoked on proxy - 调用该方法的代理实例
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? 当我在java动态代理上进行测试时,我发现这个代理是由vm生成的。所以我想知道为什么方法调用有这个参数,除了那只是一个对象($ proxy0)之外什么都没有对我们的使用有实际行动吗?
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. 例如 - Mokito测试框架存储代理调用历史记录。
if you are chinese ,you can read this article http://rejoy.iteye.com/blog/1627405 如果你是中国人,你可以阅读这篇文章http://rejoy.iteye.com/blog/1627405
he makes a decompliation of $proxy0.class, you should know that $proxy0 extends Proxy 他对$ proxy0.class进行了解压缩,你应该知道$ proxy0扩展了Proxy
public final class $Proxy0 extends Proxy
implements UserService
and there is a function in $proxy0: $ 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.