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
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
).