首页 > 解决方案 > 使用 bytebuddy 更改对参数的方法访问

问题描述

我有一个类似下面例子的案例

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

我想用 byte-buddy 做这样的事情

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

我试图Advice拦截呼叫,但搞砸了对象引用。我一直在研究 byte-buddy 测试用例以及阅读 ASM,但到目前为止还没有取得很大进展。

标签: javareflectionbyte-buddy

解决方案


这可以通过覆盖参数的建议来完成。

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

推荐阅读