【问题标题】:Alter method access to an argument using bytebuddy使用 bytebuddy 更改对参数的方法访问
【发布时间】:2018-07-12 13:30:05
【问题描述】:

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

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,但到目前为止还没有取得很大进展。

【问题讨论】:

    标签: java reflection byte-buddy


    【解决方案1】:

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

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

    【讨论】:

    • 您好,感谢您的回答。该方法的问题在于它引入了对跟踪引用的副作用的引用。随着堆栈的继续,其他方法也将更改跟踪的引用,而 futureCall(例如,10 秒后)将引用不同的对象。
    • 但是引用只对方法的执行有效。生成的方法体是 100% 等效的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2011-04-18
    • 2017-05-31
    • 2015-10-14
    相关资源
    最近更新 更多