简体   繁体   中英

Modifying method arguments using byteman

I have classes as show below

 public class Caller {

  private Calle calle = new Calle();

  public void invoke(final String arg) {
    calle.invoke(arg);
  }

}


public class Calle {

  public void invoke(final String arg) {

  }

}

public class Main {
  public static void main(final String[] args) {

   Caller caller = new Caller();
   caller.invoke("suman");

 }

}

I wanted to write a byteman rule to capture caller.invoke("suman"); method call and modify the argument "suman" to "suman1". That means for calle.invoke(arg); in Caller class the argument should come as "suman1". I tried capturing the arguments using byteman rules, but i don't know how to modify the arguments.

can you please help?

The following 2 rules will do what you require:

RULE trace Caller.invoke entry
CLASS your.package.Caller
METHOD invoke(java.lang.String)
AT ENTRY
IF true
DO
    traceln("::::::::: Caller.invoke");
    $1 = $1 + "1";
ENDRULE
RULE trace Calle.invoke entry
CLASS your.package.Calle
METHOD invoke(java.lang.String)
AT ENTRY
IF true
DO
    traceln("::::::::: Calle.invoke");
    traceln("Argument: " + $1);
ENDRULE

The first rule " trace Caller.invoke entry " gets injected at the entry point of the invoke method of the Caller class and modifies the passed argument by appending "1" to its value. Arguments are accessible in byteman rules using the ${arg number} statement, in your case $1 for your only (and first) argument.

The second rule " trace Calle.invoke entry " simply prints the argument passed to the invoke method of the Calle class when entering the method, showing that the transformed argument string arrived.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM