我正在尝试检测构造函数,以便在调用它们时打印出来。我这样做是通过使用insertAfter。我还想打印出对象引用。我尝试使用$_
,但只是设置为0.是否可以打印新新对象的对象引用?
我结束了找到我想要的解决方案。它包括使用像这样的ExprEditor:
public CtClass instrumentMethods() throws CannotCompileException, IOException {
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(new LoaderClassPath(loader));
CtClass ctKlazz = cp.makeClass(instream);
CtMethod[] methods = ctKlazz.getMethods();
for (CtMethod method : methods) {
final CtBehavior method = methods[ind];
method.instrument(
new ExprEditor() {
// Instrument new expressions:
public void edit(NewExpr expr) throws CannotCompileException {
final int allocSiteId = getAllocSiteId(className, expr.indexOfBytecode());
expr.replace( "{ $_ = $proceed($$); someCodeHere($_); }");
}
}
);
method.insertBefore("{ someInstrumentationAtStart(); }");
}
return ctKlazz;
}