Reputation: 1
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?
Upvotes: 0
Views: 1051
Reputation: 355
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.
Upvotes: 1