我正在尝试在方法主体中动态地将字段引用替换为另一个字段引用。bytebuddy有可能吗?我要转换的类是这样的:public class TestReplace { @replace int a = 1; int b = 2; public int printA() { return a; } public int printB() { return b; }}转换包括在访问它们的方法中替换标有@replace的变量。替换将由我将插入的变量完成。已经转换的类应如下所示:public class TestReplace { @replace int a = 1; int ___a = a * 10; // var inserted int b = 2; public int printA() { return ___a; } public int printB() { return b; }}我是这个主题的新手,希望您能给我任何帮助。提前致谢
1 回答
![?](http://img1.sycdn.imooc.com/545862e700016daa02200220-100-100.jpg)
交互式爱情
TA贡献1712条经验 获得超3个赞
是的,可以使用MemberSubstitution
:
MemberSubstitution.relaxed() .field(ElementMatchers.named("a")) .onRead() .doReplaceWith(otherField)
例如,您可以从使用AgentBuilder
API定义的Java代理中应用此转换。您可能还需要从该代理定义替代字段。您可以使用来引用此字段FieldDescription.Latent
。
添加回答
举报
0/150
提交
取消