1

I've been using a CompilationUnit to parse Java files but now I'd like to parse all files within a directory.

I've tried the following however the compilation size is calculated as 0 when I expect it to be the number of Java files within the specified directory.

Path pathToSource = Paths.get("resources/src");
SourceRoot sourceRoot = new SourceRoot(pathToSource);
List<CompilationUnit> compilations = sourceRoot.getCompilationUnits();

Following the JavaDoc - https://www.javadoc.io/doc/com.github.javaparser/javaparser-core

algorhythm
  • 3,304
  • 6
  • 36
  • 56

2 Answers2

2

You either have to add all the compilation units manually, or just call tryToParse() on the sourceRoot.

With this small modification, it found (almost*) all .java files below the SourceRoot.

Path pathToSource = Paths.get("resources/src");
SourceRoot sourceRoot = new SourceRoot(pathToSource);
sourceRoot.tryToParse();
List<CompilationUnit> compilations = sourceRoot.getCompilationUnits();

(* it did not support java modules, so it failed to parse my module-info.java).

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
0

The pathToSource variable must refer to the root directory which corresponds to the root of the package structure of the source files it contains. For example, in a classic maven project, this variable should be initialized with the src/main/java directory.

jpl
  • 347
  • 3
  • 11