计划在Java asm上对Java类上的方法进行检测,
{
ClassReader cr = new ClassReader(classfileBuffer);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new LogMethodClassVisitor(cw, className);
cr.accept(cv, 0);
newClassByte = cw.toByteArray();
}
下面是ClassVisitor的派生类
public class LogMethodClassVisitor extends ClassVisitor {
private String className;
public LogMethodClassVisitor(ClassVisitor cv, String pClassName) {
super(Opcodes.ASM4, cv);
className = pClassName;
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature,
exceptions);
if (name.equalsIgnoreCase("getParameter") && (mv != null)) {
mv = new PrintMessageMethodVisitor(mv, name, className);
}
return mv;
}
}
public class PrintMessageMethodVisitor extends MethodVisitor {
String name;
String classname;
public PrintMessageMethodVisitor (MethodVisitor mv, String name, String classname)
{
super(ASM5, mv);
this.name = name;
this.classname = classname;
}
@Override
public void visitCode() {
{
System.out.println ("###### entering visitCode ###### ");
}
}
仅调用MethodVisitor中PrintMessageMethodVisitor中的构造函数。不会调用visitCode之类的其他方法。
我尝试过使用该论坛的其他方法,例如
1) use adviceadapter instead of methodVistor
2) cr.accept(cv,ClassReader.EXPAND_FRAMES) ;
他们都没有工作。
并且我已经启用了这些属性,
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
上面的代码是一个典型的Java asm工具示例,我肯定错过了一些琐碎的事情,但是我无法弄清楚。
有任何线索吗?请帮忙。谢谢
答案 0 :(得分:0)
非常愚蠢的错误。该方法是没有任何代码的抽象函数