0
我是Java和Javassist的新手。我想在某些方法的每個讀取訪問某個我的原始變量之前添加一個記錄器。經過一番研究,我想出了這個代碼:Javassist insturmenter
try {
method.instrument(
new ExprEditor() {
public void edit(FieldAccess fa) throws CannotCompileException {
CtField field = null;
CtClass fieldType = null;
try {
field = fa.getField();
fieldType = field.getType();
} catch (NotFoundException e) {
e.printStackTrace();
}
if (fa.isReader() && field != null && fieldType.isPrimitive()){
System.out.println("Primitive found: " + field.getName());
//ToDo: call method before variable access
fa.replace("{ $_ = $proceed($$); }");
}
}
}
);
} catch (CannotCompileException e) {
e.printStackTrace();
}
有沒有辦法來調用變量被訪問之前(記錄)方法,還是我用不同的方式來解決這個問題?使用Javassist實現這一點的最佳方法是什麼?
希望有人能幫助我:)