简体   繁体   English

将return对象替换为java.lang.reflect.InvocationHandler不起作用。 如何在代理呼叫中完全替换返回对象?

[英]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.

相关问题 调用的java.lang.reflect.InvocationHandler的方法参数列表中的“代理”对象代表什么? - What does “proxy” object from invoke's method parameters list of java.lang.reflect.InvocationHandler represent? java.lang.reflect.Proxy和java.lang.reflect.InvocationHandler - java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler 了解java.lang.reflect.InvocationHandler的invoke方法的“代理”参数 - Understanding “proxy” arguments of the invoke method of java.lang.reflect.InvocationHandler 替换java.lang.reflect.Proxy中的InvocationHandler - Replace InvocationHandler in java.lang.reflect.Proxy 将方法调用转换为该方法的 java.lang.reflect.Method object - Converting method call to the java.lang.reflect.Method object of that method 使用或不使用Regex从java.lang.reflect.Method-object中提取完全准类名 - Extract fully quallified classname out of a java.lang.reflect.Method-object with or without Regex 如何检查java.lang.reflect.Method返回类型是Collection? - How to check that java.lang.reflect.Method return type is Collection? 如何从MonoDroid中的重写方法返回Java.Lang.Object - How to return Java.Lang.Object from a overridden method in MonoDroid 替换 java.lang.Object 绑定到匿名类型 - Substituting java.lang.Object is bound to an anonymous type 如何使用vertx调用外部API并将数据返回到Java对象 - How to call an external API with vertx and return data to java object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM