I am new to bytebuddy, given class Foo and Bar:
class Foo {
public String hello() {
return "greeting"
}
}
class Bar {
public Greet hello() {
return new Greet("greeting");
}
}
class Greet {
private String message;
Greet(String message) {
this.message = message;
}
}
Then if I try to redefine Foo's hello, it works, but I cannot make it work for Bar:
//worked
ByteBuddyAgent.install();
new ByteBuddy()
.redefine(Foo.class)
.method(named("hello"))
.intercept(FixedValue.value("modified greeting"))
.make()
.load(getClass().getClassLoader(), ClassReloadingStrategy.fromInstalledAgent())
.getLoaded()
.newInstance()
.hello();
//gives error
ByteBuddyAgent.install();
new ByteBuddy()
.redefine(Bar.class)
.method(named("hello"))
.intercept(FixedValue.value(new Greeting("modified greeting")))
.make()
.load(getClass().getClassLoader(), ClassReloadingStrategy.fromInstalledAgent())
.getLoaded()
.newInstance()
.hello();
it gives error:
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)
How can I make it work for Bar?