我試圖改變一個已經加載類的方法的返回值。ByteBuddy - 修改加載類的默認值
從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;
}
}