Search code examples
javassist

Read the return value of a method using javassist


I want to read the return value of a method which and I have to pass it to the code that I insert using method.insertAfter.

Example:

public String sayHello(){
    return "1";
}

I want to add the code say, someClass.someMethod() using javassist.

method.insertAfter("someClass.someMethod(<how to add that value here>);");

Can anyone let me know how to do this?

[Edit] I cannot edit the method body since there might be other code present. The above method is just an example.


Solution

  • This works for me:

        method.insertAfter( "System.err.println( $_ );");
    

    If the method returns void, then $_ will be null. So, in your case, I think this will work:

        method.insertAfter( "someClass.someMethod( $_ );");
    

    where someMethod takes an Object.