I'm trying to dynamically replace in a method body a field reference by another field reference. is that possible with bytebuddy?
The class I like to transform is like this:
public class TestReplace {
@replace
int a = 1;
int b = 2;
public int printA() {
return a;
}
public int printB() {
return b;
}
}
The transformation consists in replacing the variables marked with @replace in the methods that visit them. The replacement will be done by variables that I will insert.
The class already transformed should look like this:
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;
}
}
I am new to the subject and would appreciate any help you could give me.
Thanks in advance
Yes, this is possible using MemberSubstitution
:
MemberSubstitution.relaxed()
.field(ElementMatchers.named("a"))
.onRead()
.doReplaceWith(otherField)
You can, for example, apply this transformation from a Java agent that you define using the AgentBuilder
API. You probably also need to define the alternative field from this agent. You can reference this field using a FieldDescription.Latent
.
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.