I am trying (with no success) to print only the contents of a given method. The following code almost does the trick:
class MyTraceMethodVisitor extends MethodVisitor {
public MyTraceMethodVisitor(MethodVisitor mv) {
super(Opcodes.ASM4, mv);
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
}
}
class MyClassVisitor extends ClassVisitor {
public MyClassVisitor(ClassVisitor cv) {
super(Opcodes.ASM4, cv);
}
@Override
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
return null;
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
if (name.equals("get777"))
return new MyTraceMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions));
return null;
}
}
running it with
ClassReader classReader = new ClassReader("something.Point");
PrintWriter printWriter = new PrintWriter(System.out);
TraceClassVisitor traceClassVisitor = new TraceClassVisitor(printWriter);
MyClassVisitor myClassVisitor = new MyClassVisitor(traceClassVisitor);
classReader.accept(myClassVisitor, ClassReader.SKIP_DEBUG);
resulting in
// class version 50.0 (50)
// access flags 0x21
public class something/Point {
// access flags 0x1
public get777()I
SIPUSH 777
IRETURN
}
What I'd like to get was just
SIPUSH 777
IRETURN
without signature, comments and whatsoever.
How can I accomplish that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…