7

I have a line number of a Java source file and want to get the surrounding method for that line number programmatically.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
YesSir
  • 71
  • 1
  • 4

2 Answers2

7

Use something like JavaParser. From what I can see, the Node class has references to begin and end column and row indexes. MethodDeclaration is a subclass of Node, so parse the source file and search for the MethodDeclaration that 'contains' your line number.

Sample code

You would make sure the src file points to your own source. Here, I just use the source of the sample itself.

package grimbo.test;

import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.IOException;

public class TestMethodLineNumber {
    public static void method1() {
        int i = 1;
        System.out.println(i);
    }

    public static void method2() {
        String s = "hello";
        System.out.println(s);
    }

    public static void main(String[] args) throws ParseException, IOException {
        File f = new File(".").getAbsoluteFile();
        File srcRoot = new File(f, "src/main/java");
        String srcFilename = TestMethodLineNumber.class.getName().replaceAll("\\.", "/") + ".java";
        File src = new File(srcRoot, srcFilename);
        System.out.println(f);
        System.out.println(srcRoot);
        System.out.println(src);
        getMethodLineNumbers(src);
    }

    private static void getMethodLineNumbers(File src) throws ParseException, IOException {
        CompilationUnit cu = JavaParser.parse(src);
        new MethodVisitor().visit(cu, null);
    }

    /**
     * Simple visitor implementation for visiting MethodDeclaration nodes.
     */
    private static class MethodVisitor extends VoidVisitorAdapter {
        @Override
        public void visit(MethodDeclaration m, Object arg) {
            System.out.println("From [" + m.getBeginLine() + "," + m.getBeginColumn() + "] to [" + m.getEndLine() + ","
                    + m.getEndColumn() + "] is method:");
            System.out.println(m);
        }
    }
}

Sample output

From [13,5] to [16,5] is method:
public static void method1() {
    int i = 1;
    System.out.println(i);
}
From [18,5] to [21,5] is method:
public static void method2() {
    String s = "hello";
    System.out.println(s);
}
From [23,5] to [32,5] is method:
public static void main(String[] args) throws ParseException, IOException {
    File f = new File(".").getAbsoluteFile();
    File srcRoot = new File(f, "src/main/java");
    String srcFilename = TestMethodLineNumber.class.getName().replaceAll("\\.", "/") + ".java";
    File src = new File(srcRoot, srcFilename);
    System.out.println(f);
    System.out.println(srcRoot);
    System.out.println(src);
    getMethodLineNumbers(src);
}
From [34,5] to [37,5] is method:
private static void getMethodLineNumbers(File src) throws ParseException, IOException {
    CompilationUnit cu = JavaParser.parse(src);
    new MethodVisitor().visit(cu, null);
}
From [43,9] to [48,9] is method:
@Override
public void visit(MethodDeclaration m, Object arg) {
    System.out.println("From [" + m.getBeginLine() + "," + m.getBeginColumn() + "] to [" + m.getEndLine() + "," + m.getEndColumn() + "] is method:");
    System.out.println(m);
}
Paul Grime
  • 14,970
  • 4
  • 36
  • 58
  • 2
    I did not understand exactly how to do what you just told me – YesSir Sep 09 '11 at 12:49
  • @Paul Btw is a java new line a CRLF or a LF ? – Pacerier Jan 20 '12 at 23:17
  • @Pacerier - I believe it varies according to the O/S. http://stackoverflow.com/questions/207947/java-how-do-i-get-a-platform-independent-new-line-character – Paul Grime Jan 21 '12 at 19:21
  • @PaulGrime no I was talking about the Java source code.. I've found it here: http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html – Pacerier Jan 21 '12 at 19:27
  • Is it also possible to get Line number of instructions running inside each method with a small touch? @Paul Grime – alper Apr 23 '17 at 14:36
  • it works like a charm. And you need to replace m.getBeginLine() with m.getRange().get().begin.line in the new version of JavaParser – david euler Feb 02 '19 at 10:40
0

For the new version of JavaParser:

    @Override
    public void visit(MethodDeclaration m, Object arg) {
        System.out.println("From [" + m.getRange().get().begin.line + "," + m.getRange().get().begin.column
            + "] to [" + m.getRange().get().end.line + "," + m.getRange().get().end.column + "] is method:");
        System.out.println(m);
    }
david euler
  • 714
  • 8
  • 12