简体   繁体   中英

Alter method access to an argument using bytebuddy

I have a case like the example below

public String test(Trail trail) {
  AnotherClass.access(trail);
  this.executeAnotherMethod(trail);
  futureCall(trail::end);
  return "emptyString";
}

And I want to use byte-buddy to do something like this

public String test(Trail trail) {
  Trail clonedTrail = trail.clone("test");
  AnotherClass.access(clonedTrail);
  this.executeAnotherMethod(clonedTrail);
  futureCall(clonedTrail::end);
  return "emptyString";
}

I have tried Advice to intercept the call but that messed up object reference. I have been diving through byte-buddy testcases as well as reading ASM, but haven't made great progress so far.

This can be done with advice by overwriting the argument.

@Advice.OnMethodEnter
static void enter(@Advice.Argument(readOnly = false) Trail trail) {
  trail = trail.clone("test");
}

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