Substituting of return object for java.lang.reflect.InvocationHandler is not working. How to fully substitute the return object in proxied call?How does the Java 'for each' loop work?How do I use reflection to call a generic method?How do I call one constructor from another in Java?Why can't I define a static method in a Java interface?Efficiency of Java “Double Brace Initialization”?Getting underlying type of a proxy objectJava.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignmentHow to make a simple dynamic proxy in C#Why is “final” not allowed in Java 8 interface methods?Java - Method executed prior to Default Constructor

How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?

How can the US president give an order to a civilian?

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

Explicit song lyrics checker

Is using Legacy mode is a bad thing to do?

Got a new frameset, don't know why I need this split ring collar?

Are there examples of rowers who also fought?

Can you place a web spell on a surface you cannot see?

...and then she held the gun

How can I prevent a user from copying files on another hard drive?

Are there any individual aliens that have gained superpowers in the Marvel universe?

Operator currying: how to convert f[a,b][c,d] to a+c,b+d?

Time at 1G acceleration to travel 100 000 light years

Does cooling a potato change the nature of its carbohydrates?

What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?

How to make all magic-casting innate, but still rare?

Boundaries and Buddhism

Simplify, equivalent for (p ∨ ¬q) ∧ (¬p ∨ ¬q)

Leaving job close to major deadlines

Does anyone recognize these rockets, and their location?

How to prevent cables getting intertwined

How to write a nice frame challenge?

Is there any possible way to get these hearts as Adult Link?

Does knowing the surface area of all faces uniquely determine a tetrahedron?



Substituting of return object for java.lang.reflect.InvocationHandler is not working. How to fully substitute the return object in proxied call?


How does the Java 'for each' loop work?How do I use reflection to call a generic method?How do I call one constructor from another in Java?Why can't I define a static method in a Java interface?Efficiency of Java “Double Brace Initialization”?Getting underlying type of a proxy objectJava.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignmentHow to make a simple dynamic proxy in C#Why is “final” not allowed in Java 8 interface methods?Java - Method executed prior to Default Constructor






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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?










share|improve this question






























    0















    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?










    share|improve this question


























      0












      0








      0








      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?










      share|improve this question
















      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?







      java reflection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 5:15







      Ilya Yevlampiev

















      asked Mar 25 at 5:09









      Ilya YevlampievIlya Yevlampiev

      1,09022037




      1,09022037






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The explanation is very straightforward: if was checking not the value returned by the intercepted proxy method, but instead, was checking the call that occured inside the proxied method, so it is clear that everything works here as expected.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331549%2fsubstituting-of-return-object-for-java-lang-reflect-invocationhandler-is-not-wor%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The explanation is very straightforward: if was checking not the value returned by the intercepted proxy method, but instead, was checking the call that occured inside the proxied method, so it is clear that everything works here as expected.






            share|improve this answer



























              0














              The explanation is very straightforward: if was checking not the value returned by the intercepted proxy method, but instead, was checking the call that occured inside the proxied method, so it is clear that everything works here as expected.






              share|improve this answer

























                0












                0








                0







                The explanation is very straightforward: if was checking not the value returned by the intercepted proxy method, but instead, was checking the call that occured inside the proxied method, so it is clear that everything works here as expected.






                share|improve this answer













                The explanation is very straightforward: if was checking not the value returned by the intercepted proxy method, but instead, was checking the call that occured inside the proxied method, so it is clear that everything works here as expected.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 14:11









                Ilya YevlampievIlya Yevlampiev

                1,09022037




                1,09022037





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331549%2fsubstituting-of-return-object-for-java-lang-reflect-invocationhandler-is-not-wor%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript