Search code examples
javabytecodeinstrumentationbytecode-manipulationjava-bytecode-asm

Asm bytecode queries


Hey all, I am trying to use the ASM bytecode Tree Api to do static analysis for a class. I guess I have a pretty basic question. In a method say foobar(), I have a list of instructions within foobar (InsnList which has a List). Now I want to check if at instruction number 10, whether a function "barfoo(String args)" is being invoked.

Also seperatly, I need to verify whether a particular instruction is a conditional.

Thanks and Regards, SJ

Note: I can already read a class and reach the particular method I am interested in and iterate through each intruction of that method.

Solved (see: Greg's comments):

    AbstractInsnNode s = ...
    if(s.getType()==AbstractInsnNode.METHOD_INSN){
        MethodInsnNode methodInvocationNode = (MethodInsnNode) s;
        if(methodInvocationNode.name.equals("barfoo"))
        {
            return true;
        }
    }

Solution

  • From looking at the ASM javadoc, it looks like you'll have to call getType() on your instruction node, and if the type is METHOD_INSN then you can cast to a MethodInsnNode. From there, look at .name to see the method name.