I need to determine line number of specific method in class using ObjectWeb ASM library.
Line number of method declaration or first line in method's body are equally accepted as right answers (6 or 7 in example).
Example:
1. public class Foo {
...
6. public void bar() {
7. try {
8. try {
9. System.out.println(); //first executable line
I try to use MethodVisitor's visitLineNumber method, but it visit only first executable line (line 9 in example).
I found solution for this problem on JavaAssist library (link).
But is there a way to solve this with ASM?
EDIT:
Following snippet gave same result, line 9 instead of 6 or 7.
public static int getLineNumber(String path) throws IOException {
final File f = new File(path);
try (FileInputStream fis = new FileInputStream(f)) {
ClassReader reader = new ClassReader(fis);
ClassNode clNode = new ClassNode(Opcodes.ASM5);
reader.accept(clNode, Opcodes.ASM5);
for (MethodNode mNode : (List<MethodNode>) clNode.methods) {
if (mNode.name.equals("bar")) {
ListIterator<AbstractInsnNode> it = mNode.instructions.iterator();
while (it.hasNext()) {
AbstractInsnNode inNode = it.next();
if (inNode instanceof LineNumberNode) {
return ((LineNumberNode) inNode).line;
}
}
}
}
}
return -1;
}