I'm trying to use the ASM library to read byte code and translate it into a different format. Starting off with a simple test class containing this method:
public static double square(double a) {
return a * a;
}
Which compiles to this byte code:
public static double square(double);
Code:
0: dload_0
1: dload_0
2: dmul
3: dreturn
using this MethodVisitor to read the code:
return new MethodVisitor(ASM7) {
@Override
public void visitInsn(int opcode) {
System.out.println(String.format("%02x", opcode));
}
@Override
public void visitLdcInsn(Object value) {
System.out.println(value);
}
};
I get this output:
6b
af
Those are the opcodes for dmul
and dreturn
respectively, so it is traversing the code, but it's not reporting the dload_0
instructions, either by visitInsn
or visitLdcInsn
.
What am I doing wrong?
ASM expands dload_0
to dload 0
. At the same time, when writing, ASM optimizes the byte code instruction back to the short-cut constant when writing it to a ClassWriter
. Override visitVarInsn
and you will see the output with the byte code as first and the variable index as second argument.
(Edited after Holger's comment.)