すでにロードされているクラスのメソッドの戻り値を変更しようとしています。
ByteBuddy のドキュメント ( http://bytebuddy.net/#/tutorial ) から、フィールド/メソッドを追加しない限り、これは Java エージェントを使用して可能のようです。
私のコードは次のとおりです。
ByteBuddyAgent.install();
new ByteBuddy()
.redefine(StuffImpl.class)
.method(returns(Result.class))
.intercept(FixedValue.value(new Result("intercepted")))
.make()
.load(StuffImpl.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
しかし、次の例外が発生します。
java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)
問題は、メソッドを追加していないということです。Byte Buddy は、上記のコードのどこにフィールドまたはメソッドを追加しますか?
編集:
public class StuffImpl {
public Result validate() {
return new Result("original");
}
}
public class Result {
private String result;
public Result(String result) {
this.result = result;
}
}