“代理”是什么意思? java.lang.reflect.InitationHandler 的 invoke 方法参数列表中的对象代表什么?
我对这种情况有点困惑:
我有一个类实现了标题中提到的 InvocableHandler 接口,该类看起来像:
class SimpleProxy implements InvocationHandler{
private Object proxied;
public SimpleProxy(Object proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(proxy);
return method.invoke(proxied, args);
}
}
并且在我的“main”方法中说:
public static void main(String[] args) {
consumer(new RealObject());
MyInterface proxy = (MyInterface)Proxy.newProxyInstance(MainClass.class.getClassLoader(), new Class[]{MyInterface.class}, new SimpleProxy(new MyInterfaceImpl()));
proxy.methodFromMyInterface();
}
现在的问题是“invoke”抛出像这样的错误:
...
在 rtti.SimpleProxy.invoke(MainClass.java:81)
在 rtti.$Proxy0.toString(来源未知)
在 java.lang.String.valueOf(String.java:2826)
在 java.io.PrintStream.println(PrintStream.java:771)
在 rtti.SimpleProxy.invoke(MainClass.java:81)
在 rtti.$Proxy0.toString(来源未知)
在 java.lang.String.valueOf(String.java:2826)
在 java.io.PrintStream.println(PrintStream.java:771)
...
因为这一行:
System.out.println(代理);
如果我评论这一行一切正常。
有人可以向我解释一下有什么问题吗?
注意:在 Java 文档中,它提到了 InvocableHandler 中的调用方法:
处理代理实例上的方法调用并返回结果。当在与其关联的代理实例上调用方法时,将在调用处理程序上调用此方法。
参数: proxy - 调用该方法的代理实例
...所以我不明白为什么会出错...
I'm a little bit confused by this scenario:
I have a class that implements InvocationHandler interface mentioned in title, class that looks like :
class SimpleProxy implements InvocationHandler{
private Object proxied;
public SimpleProxy(Object proxied) {
this.proxied = proxied;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(proxy);
return method.invoke(proxied, args);
}
}
and lets say in my "main" method I have:
public static void main(String[] args) {
consumer(new RealObject());
MyInterface proxy = (MyInterface)Proxy.newProxyInstance(MainClass.class.getClassLoader(), new Class[]{MyInterface.class}, new SimpleProxy(new MyInterfaceImpl()));
proxy.methodFromMyInterface();
}
Now the problem is that the "invoke" throws an error like:
...
at rtti.SimpleProxy.invoke(MainClass.java:81)
at rtti.$Proxy0.toString(Unknown Source)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
at rtti.SimpleProxy.invoke(MainClass.java:81)
at rtti.$Proxy0.toString(Unknown Source)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
...
because of this line :
System.out.println(proxy);
If I comment this line everithing works fine.
Can anybody explain me what's the problem?
N.B. In the Java docs it says about invoke method from InvocationHandler:
Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.
Parameters:
proxy - the proxy instance that the method was invoked on
... so I can't understand why it is going wrong ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
System.out.println(proxy);
将隐式调用代理上的toString()
,即调用代理方法。System.out.println(proxy);
will implicitly calltoString()
on the proxy, i.e. call a proxied method.