1
我有以下類是使用以下了Javassist - 重定向到方法字段訪問(不工作)
public class Booking{
public String name;
public String comment;
public String session;
public void test(){
this.name = "hi";
}
}
我的儀器:
cc.instrument(new ExprEditor(){
public void edit(FieldAccess arg) throws CannotCompileException {
if (arg.isWriter()) {
StringBuffer code = new StringBuffer();
code.append("$0.");
code.append(arg.getFieldName());
code.append("=$1.toUpperCase();");
arg.replace(code.toString());
}
}
});
現在,當我把這叫做:
Booking b = new Booking();
b.name = "hello";
System.out.println(b.name); // Edited correction
b.test();
System.out.println(b.name);
給我
hello // Externally, doesn't.
HI // Internally, works as expected
我錯過了什麼?這似乎是我應該能夠輕鬆完成的其中一件事情。
請不要告訴我我必須在所有類上做一條「fieldAccess.replace」? O.O
第一個打印應該是println(b.name)而不是「」? – 2012-02-12 11:06:32
是的。謝謝@PeterLiljenberg – 2012-02-12 17:06:52