dimanche 24 mars 2019

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.

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); and returning it in the end of invoke method has absolutely no influence on what the proxy returns? Uneasy to believe, so where is my mistake?





Aucun commentaire:

Enregistrer un commentaire