Search in sources :

Example 1 with SKIP_SUBTREE

use of java.nio.file.FileVisitResult.SKIP_SUBTREE in project javaparser by javaparser.

the class SourceRoot method parse.

/**
 * Tries to parse all .java files in a package recursively and passes them one by one to the callback. In comparison
 * to the other parse methods, this is much more memory efficient, but saveAll() won't work.
 *
 * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files.
 */
public SourceRoot parse(String startPackage, ParserConfiguration configuration, Callback callback) throws IOException {
    assertNotNull(startPackage);
    assertNotNull(configuration);
    assertNotNull(callback);
    logPackage(startPackage);
    final JavaParser javaParser = new JavaParser(configuration);
    final Path path = packageAbsolutePath(root, startPackage);
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path absolutePath, BasicFileAttributes attrs) throws IOException {
            if (!attrs.isDirectory() && absolutePath.toString().endsWith(".java")) {
                Path localPath = root.relativize(absolutePath);
                Log.trace("Parsing %s", localPath);
                final ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(absolutePath));
                result.getResult().ifPresent(cu -> cu.setStorage(absolutePath));
                if (callback.process(localPath, absolutePath, result) == SAVE) {
                    if (result.getResult().isPresent()) {
                        save(result.getResult().get(), path);
                    }
                }
            }
            return CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return isSensibleDirectoryToEnter(dir) ? CONTINUE : SKIP_SUBTREE;
        }
    });
    return this;
}
Also used : CodeGenerationUtils.fileInPackageRelativePath(com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath) Path(java.nio.file.Path) CodeGenerationUtils.packageAbsolutePath(com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath) Providers.provider(com.github.javaparser.Providers.provider) SAVE(com.github.javaparser.utils.SourceRoot.Callback.Result.SAVE) ParseResult(com.github.javaparser.ParseResult) COMPILATION_UNIT(com.github.javaparser.ParseStart.COMPILATION_UNIT) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Utils.assertNotNull(com.github.javaparser.utils.Utils.assertNotNull) CodeGenerationUtils.fileInPackageRelativePath(com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath) Map(java.util.Map) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) PrettyPrinter(com.github.javaparser.printer.PrettyPrinter) Files(java.nio.file.Files) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ParserConfiguration(com.github.javaparser.ParserConfiguration) IOException(java.io.IOException) RecursiveAction(java.util.concurrent.RecursiveAction) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) CONTINUE(java.nio.file.FileVisitResult.CONTINUE) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) ParseProblemException(com.github.javaparser.ParseProblemException) SKIP_SUBTREE(java.nio.file.FileVisitResult.SKIP_SUBTREE) ForkJoinPool(java.util.concurrent.ForkJoinPool) Pattern(java.util.regex.Pattern) JavaParser(com.github.javaparser.JavaParser) CodeGenerationUtils.packageAbsolutePath(com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath) JavaParser(com.github.javaparser.JavaParser) ParseResult(com.github.javaparser.ParseResult) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

JavaParser (com.github.javaparser.JavaParser)1 ParseProblemException (com.github.javaparser.ParseProblemException)1 ParseResult (com.github.javaparser.ParseResult)1 COMPILATION_UNIT (com.github.javaparser.ParseStart.COMPILATION_UNIT)1 ParserConfiguration (com.github.javaparser.ParserConfiguration)1 Providers.provider (com.github.javaparser.Providers.provider)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 PrettyPrinter (com.github.javaparser.printer.PrettyPrinter)1 CodeGenerationUtils.fileInPackageRelativePath (com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath)1 CodeGenerationUtils.packageAbsolutePath (com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath)1 SAVE (com.github.javaparser.utils.SourceRoot.Callback.Result.SAVE)1 Utils.assertNotNull (com.github.javaparser.utils.Utils.assertNotNull)1 IOException (java.io.IOException)1 FileVisitResult (java.nio.file.FileVisitResult)1 CONTINUE (java.nio.file.FileVisitResult.CONTINUE)1 SKIP_SUBTREE (java.nio.file.FileVisitResult.SKIP_SUBTREE)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1