2

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; } } 
9
  • 1
    thanks Chris.... dint knw we are suppsd to do that.... doing nw Commented Apr 16, 2011 at 20:47
  • Do you know what the individual JVM instructions mean? Just trying to figure out where you're starting from here. Commented Apr 16, 2011 at 20:47
  • Yes I do.... Ive gone through the ASM manual.... Commented Apr 16, 2011 at 20:51
  • Then what's the question? Given the instruction at offset 10, you should know how to determine whether the bytecode there is invoking a specific function. Similarly, given a specific instruction it should be trivial to determine whether it's a conditional or not. Commented Apr 16, 2011 at 20:57
  • 1
    That's right, invokespecial is one of the bytecodes that can invoke a method (there are a few more). 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. Commented Apr 16, 2011 at 21:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Simple - once you've determined that you've invoked foobar, start incrementing a counter on each instruction you visit. On the tenth invocation, perform your check for the invocation of barfoo.

1 Comment

He doesn't need to run the code for the visitors to visit the class/method/instructions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.