Search in sources :

Example 6 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration 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)

Example 7 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class CommentParsingSteps method whenTheClassIsParsedByTheCommentParser.

@When("the class is parsed by the comment parser")
public void whenTheClassIsParsedByTheCommentParser() throws IOException {
    ParseResult<CompilationUnit> parseResult = new JavaParser(new ParserConfiguration()).parse(COMPILATION_UNIT, provider(sourceUnderTest));
    commentsCollection = parseResult.getCommentsCollection().orElse(new CommentsCollection());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser) ParserConfiguration(com.github.javaparser.ParserConfiguration) When(org.jbehave.core.annotations.When)

Example 8 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class TryStmtTest method parse9.

private <T> T parse9(String code) {
    JavaParser parser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_9));
    ParseResult<Statement> result = parser.parse(ParseStart.STATEMENT, provider(code));
    assertTrue(result.toString(), result.isSuccessful());
    return (T) result.getResult().get();
}
Also used : JavaParser(com.github.javaparser.JavaParser) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 9 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class BulkParseTest method parseJdkSrcZip.

private void parseJdkSrcZip() throws IOException {
    // This is where Ubuntu stores the contents of package openjdk-8-src
    Path path = Paths.get("/usr/lib/jvm/openjdk-9/src.zip");
    bulkTest(new SourceZip(path), "openjdk_src_zip_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_9));
}
Also used : Path(java.nio.file.Path) SourceZip(com.github.javaparser.utils.SourceZip) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 10 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project javaparser by javaparser.

the class BulkParseTest method parseOpenJdkLangToolsRepository.

private void parseOpenJdkLangToolsRepository() throws IOException {
    Path workdir = CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve(Paths.get(temporaryDirectory(), "javaparser_bulkparsetest"));
    workdir.toFile().mkdirs();
    Path openJdkZipPath = workdir.resolve("langtools.zip");
    if (Files.notExists(openJdkZipPath)) {
        Log.info("Downloading JDK langtools");
        /* Found by choosing a tag here: http://hg.openjdk.java.net/jdk9/jdk9/langtools/tags
             then copying the "zip" link to the line below: */
        download(new URL("http://hg.openjdk.java.net/jdk10/jdk10/langtools/archive/19293ea3999f.zip"), openJdkZipPath);
    }
    bulkTest(new SourceZip(openJdkZipPath), "openjdk_src_repo_test_results.txt", new ParserConfiguration().setLanguageLevel(JAVA_10));
}
Also used : Path(java.nio.file.Path) SourceZip(com.github.javaparser.utils.SourceZip) URL(java.net.URL) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Aggregations

ParserConfiguration (com.github.javaparser.ParserConfiguration)11 JavaParser (com.github.javaparser.JavaParser)9 CompilationUnit (com.github.javaparser.ast.CompilationUnit)5 StreamProvider (com.github.javaparser.StreamProvider)3 FileInputStream (java.io.FileInputStream)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 SourceZip (com.github.javaparser.utils.SourceZip)2 File (java.io.File)2 When (org.jbehave.core.annotations.When)2 JavaParser.parseVariableDeclarationExpr (com.github.javaparser.JavaParser.parseVariableDeclarationExpr)1 ParseProblemException (com.github.javaparser.ParseProblemException)1 ParseResult (com.github.javaparser.ParseResult)1 COMPILATION_UNIT (com.github.javaparser.ParseStart.COMPILATION_UNIT)1 Providers.provider (com.github.javaparser.Providers.provider)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 Java5Validator (com.github.javaparser.ast.validator.Java5Validator)1 PrettyPrinter (com.github.javaparser.printer.PrettyPrinter)1