1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaParserでメソッド定義を出力する

Posted at

この記事ではIntelliJ IDEA 2021.1.3 (Community Edition) Runtime version: 11.0.11+9-b1341.60 amd64を使っています。

ディレクトリ以下のjavaファイルをParseします。

javaparserを使用するには、build.gradleのdependenciesに追加します。

build.gradle
dependencies {
    implementation 'com.github.javaparser:javaparser-core:3.3.0'
}

コード

Main.java
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        Path path;
        if (args.length == 0) {
//            path = Paths.get("");
            path = Paths.get("src/main/java/");
        } else {
            path = Paths.get(args[0]);
        }

        try {
            parsePath(path);
        } catch (IOException e) {
            System.out.println("file not found!");
        }
    }

    static void parsePath(Path path) throws IOException {
        if (Files.isDirectory(path)) {
            Files.list(path).forEach(p -> {
                try {
                    parsePath(p);
                } catch (IOException e) {
                    // nop
                }
            });
        } else {
            parse(path);
        }
    }

    static void parse(Path path) throws IOException {
        CompilationUnit cu = JavaParser.parse(path);
        new MethodVisitor().visit(cu, null);
    }
}
MethodVisitor.java
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

class MethodVisitor extends VoidVisitorAdapter<Object> {
    /* Class name or interface name */
    @Override
    public void visit(final ClassOrInterfaceDeclaration n, final Object arg) {
        System.out.println(n.getName());
        super.visit(n, arg);
    }

    /* Method Declaration */
    @Override
    public void visit(final MethodDeclaration n, final Object arg) {
        System.out.println("  " + n.getDeclarationAsString());
        super.visit(n, arg);
    }

    /* Constructor declaration */
    @Override
    public void visit(final ConstructorDeclaration n, final Object arg) {
        System.out.println("  " + n.getDeclarationAsString());
        super.visit(n, arg);
    }
}
出力結果
Main
  public static void main(String[] args)
  static void parsePath(Path path) throws IOException
  static void parse(Path path) throws IOException
MethodVisitor
  public void visit(final ClassOrInterfaceDeclaration n, final Object arg)
  public void visit(final MethodDeclaration n, final Object arg)
  public void visit(final ConstructorDeclaration n, final Object arg)

##参考サイト
https://qiita.com/opengl-8080/items/50ddee7d635c7baee0ab

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?