[英]Substituting of return object for java.lang.reflect.InvocationHandler is not working. How to fully substitute the return object in proxied call?
I have the real object and dynamic proxy handler classes, for dynamic proxy hanler I substitute all returned string values to some other values and return them in the implemented method, however, the values from the original return are returned and I can only modify the call arguments, not the return values. 我有真正的对象和动态代理处理程序类,对于动态代理处理程序我将所有返回的字符串值替换为其他一些值并在实现的方法中返回它们,但是,返回原始返回的值,我只能修改调用参数,而不是返回值。
package reflection;
public class RealObject implements Interface {
@Override
public void doSomething() {
System.out.println("Do Something");
}
@Override
public String returnSomethingElse(String arg) {
System.out.println("Do something else "+arg
);
return arg;
}
}
and here is the test and the handler: 这是测试和处理程序:
package reflection;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class SimpleProxyDemo
{
public static void process(Interface iface)
{
iface.doSomething();
iface.returnSomethingElse("argsHere");
}
public static void main(String[] args) {
process(new RealObject());
//process(new SimpleProxy(new RealObject()));
//Interface dynamicProxy=(Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),new Class[]{Interface.class},new SimpleProxyDemo().new DynamicProxyHandler(new RealObject()));
Interface dynamicProxy=(Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),new Class[]{Interface.class},new DynamicProxyHandler(new RealObject()));
process(dynamicProxy);
}
static class DynamicProxyHandler implements InvocationHandler{
private Object proxied;
public DynamicProxyHandler(Object proxied)
{
this.proxied=proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("THIS IS BEFORE CALL FROM DYNAMIC PROXY, CALLING METHOD "+method.getName());
if (args!=null&&args.length>0) {
args[0] = args[0] + "I DO INFLUENCE";
}
//Object toBeReturned= method.invoke(proxied,args+"I DO INFLUENCE");
Object toBeReturned= method.invoke(proxied,args);
System.out.println("THIS IS AFTER CALL FROM DYNAMIC PROXY, CALLING METHOD "+method.getName());
//if (toBeReturned instanceof String) {
if (toBeReturned !=null) {
return "OLOLO I CAN INFLUENCE";
}
else
return toBeReturned;
}
}
}
My expectation is that for the methods that return String the returned value would be substituted by my String "OLOLO I CAN INFLUENCE", but the proxy object don't return it in its methods. 我的期望是,对于返回String的方法,返回的值将被我的字符串“OLOLO I CAN INFLUENCE”替换,但代理对象不会在其方法中返回它。
and here is the output: 这是输出:
Do Something
Do something else argsHere
THIS IS BEFORE CALL FROM DYNAMIC PROXY, CALLING METHOD doSomething
Do Something
THIS IS AFTER CALL FROM DYNAMIC PROXY, CALLING METHOD doSomething
THIS IS BEFORE CALL FROM DYNAMIC PROXY, CALLING METHOD returnSomethingElse
Do something else argsHereI DO INFLUENCE
THIS IS AFTER CALL FROM DYNAMIC PROXY, CALLING METHOD returnSomethingElse
so it looks like Object toBeReturned= method.invoke(proxied,args); 所以它看起来像Object toBeReturned = method.invoke(proxied,args); and returning it in the end of invoke method has absolutely no influence on what the proxy returns? 并在invoke方法结束时返回它对代理返回的内容完全没有影响? Uneasy to believe, so where is my mistake? 不敢相信,所以我的错误在哪里?
解释非常简单:如果没有检查截获的代理方法返回的值,而是检查代理方法中发生的调用,所以很明显一切都按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.