如何在Java源文件中获取给定行号的周围方法

时间:2011-09-09 10:29:40

标签: java parsing

我有一个Java源文件的行号,并希望以编程方式获取该行号的周围方法。

2 个答案:

答案 0 :(得分:7)

使用JavaParser之类的内容。从我所看到的,Node类引用了开始和结束列和行索引。 MethodDeclaration是Node的子类,因此解析源文件并搜索“包含”您的行号的MethodDeclaration。

示例代码

您需要确保src文件指向您自己的来源。在这里,我只使用样本本身的来源。

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);
        }
    }
}

样本输出

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);
}

答案 1 :(得分:0)

对于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);
    }