diff --git a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/core/CoreGenerator.java b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/core/CoreGenerator.java index 2d133ea4a1..bfe7fc2fb4 100644 --- a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/core/CoreGenerator.java +++ b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/core/CoreGenerator.java @@ -1,10 +1,8 @@ package com.github.javaparser.generator.core; -import com.github.javaparser.JavaParser; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.generator.core.node.*; import com.github.javaparser.generator.core.visitor.*; -import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import com.github.javaparser.utils.SourceRoot; import java.nio.file.Path; @@ -21,12 +19,11 @@ public static void main(String[] args) throws Exception { final Path root = Paths.get(args[0], "..", "javaparser-core", "src", "main", "java"); final SourceRoot sourceRoot = new SourceRoot(root) // .setPrinter(LexicalPreservingPrinter::print) - .setJavaParser(new JavaParser( - new ParserConfiguration() + .setParserConfiguration(new ParserConfiguration() // .setStoreTokens(false) // .setAttributeComments(false) // .setLexicalPreservationEnabled(true) - )); + ); new CoreGenerator().run(sourceRoot); diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/MethodCallExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/MethodCallExpr.java index edc33568e8..f61dde233c 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/MethodCallExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/MethodCallExpr.java @@ -40,7 +40,6 @@ import com.github.javaparser.TokenRange; import com.github.javaparser.resolution.SymbolResolver; import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; - import java.util.function.Consumer; /** diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ObjectCreationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ObjectCreationExpr.java index 30f7f7b2f0..dd0f2829f4 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ObjectCreationExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ObjectCreationExpr.java @@ -44,7 +44,6 @@ import com.github.javaparser.TokenRange; import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; - import java.util.function.Consumer; /** diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitor.java b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitor.java index 693789a4dd..3fa06f3437 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitor.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/visitor/GenericVisitor.java @@ -38,7 +38,7 @@ */ public interface GenericVisitor { - //- Compilation Unit ---------------------------------- + // - Compilation Unit ---------------------------------- @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(CompilationUnit n, A arg); @@ -54,7 +54,7 @@ public interface GenericVisitor { @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(BlockComment n, A arg); - //- Body ---------------------------------------------- + // - Body ---------------------------------------------- @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(ClassOrInterfaceDeclaration n, A arg); @@ -91,7 +91,7 @@ public interface GenericVisitor { @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(JavadocComment n, A arg); - //- Type ---------------------------------------------- + // - Type ---------------------------------------------- @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(ClassOrInterfaceType n, A arg); @@ -119,7 +119,7 @@ public interface GenericVisitor { @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(UnknownType n, A arg); - //- Expression ---------------------------------------- + // - Expression ---------------------------------------- @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(ArrayAccessExpr n, A arg); @@ -207,7 +207,7 @@ public interface GenericVisitor { @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(MemberValuePair n, A arg); - //- Statements ---------------------------------------- + // - Statements ---------------------------------------- @Generated("com.github.javaparser.generator.core.visitor.GenericVisitorGenerator") R visit(ExplicitConstructorInvocationStmt n, A arg); diff --git a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java index bbeb8c8094..a813ab9bd4 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java +++ b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java @@ -1,11 +1,11 @@ package com.github.javaparser.utils; -import static com.github.javaparser.ParseStart.COMPILATION_UNIT; -import static com.github.javaparser.Providers.provider; -import static com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath; -import static com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath; -import static com.github.javaparser.utils.SourceRoot.Callback.Result.SAVE; -import static com.github.javaparser.utils.Utils.assertNotNull; +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseProblemException; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.printer.PrettyPrinter; import java.io.IOException; import java.nio.file.FileVisitResult; @@ -22,16 +22,21 @@ import java.util.function.Function; import java.util.stream.Collectors; -import com.github.javaparser.JavaParser; -import com.github.javaparser.ParseProblemException; -import com.github.javaparser.ParseResult; -import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.printer.PrettyPrinter; +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelativePath; +import static com.github.javaparser.utils.CodeGenerationUtils.packageAbsolutePath; +import static com.github.javaparser.utils.SourceRoot.Callback.Result.SAVE; +import static com.github.javaparser.utils.Utils.assertNotNull; /** - * A collection of Java source files located in one directory and its subdirectories on the file system. - * Files can be parsed and written back one by one or all together. Note that the internal cache - * used is thread-safe. + * A collection of Java source files located in one directory and its subdirectories on the file system. Files can be + * parsed and written back one by one or all together. Note that the internal cache used is thread-safe. + *
    + *
  • methods called "tryToParse..." will return their result inside a "ParseResult", which supports parse successes and failures.
  • + *
  • methods called "parse..." will return "CompilationUnit"s. If a file fails to parse, an exception is thrown.
  • + *
  • methods ending in "...Parallelized" will speed up parsing by using multiple threads.
  • + *
*/ public class SourceRoot { @FunctionalInterface @@ -50,7 +55,7 @@ enum Result { private final Path root; private final Map> cache = new ConcurrentHashMap<>(); - private JavaParser javaParser = new JavaParser(); + private ParserConfiguration parserConfiguration = new ParserConfiguration(); private Function printer = new PrettyPrinter()::print; public SourceRoot(Path root) { @@ -63,25 +68,39 @@ public SourceRoot(Path root) { } /** - * Tries to parse a .java files under the source root and returns the ParseResult. - * It keeps track of the parsed file so you can write it out with the saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * Tries to parse a .java files under the source root and returns the ParseResult. It keeps track of the parsed file + * so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you + * don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you + * might want to use the parse method with a callback. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. + * @deprecated pass ParserConfiguration instead of JavaParser */ - public ParseResult tryToParse(String pkg, String filename, JavaParser javaParser) + @Deprecated + public ParseResult tryToParse(String startPackage, String filename, JavaParser javaParser) throws IOException { - assertNotNull(pkg); + return tryToParse(startPackage, filename, javaParser.getParserConfiguration()); + } + + /** + * Tries to parse a .java files under the source root and returns the ParseResult. It keeps track of the parsed file + * so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you + * don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you + * might want to use the parse method with a callback. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. + */ + public ParseResult tryToParse(String startPackage, String filename, ParserConfiguration configuration) throws IOException { + assertNotNull(startPackage); assertNotNull(filename); - final Path relativePath = fileInPackageRelativePath(pkg, filename); + final Path relativePath = fileInPackageRelativePath(startPackage, filename); if (cache.containsKey(relativePath)) { Log.trace("Retrieving cached %s", relativePath); return cache.get(relativePath); } final Path path = root.resolve(relativePath); Log.trace("Parsing %s", path); - final ParseResult result = javaParser + final ParseResult result = new JavaParser(configuration) .parse(COMPILATION_UNIT, provider(path)); result.getResult().ifPresent(cu -> cu.setStorage(path)); cache.put(relativePath, result); @@ -89,25 +108,24 @@ public ParseResult tryToParse(String pkg, String filename, Java } /** - * Tries to parse a .java files under the source root and returns the ParseResult. - * It keeps track of the parsed file so you can write it out with the saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * Tries to parse a .java files under the source root and returns the ParseResult. It keeps track of the parsed file + * so you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you + * don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you + * might want to use the parse method with a callback. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. */ - public ParseResult tryToParse(String pkg, String filename) throws IOException { - return tryToParse(pkg, filename, javaParser); + public ParseResult tryToParse(String startPackage, String filename) throws IOException { + return tryToParse(startPackage, filename, parserConfiguration); } /** - * Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source - * root. - * It keeps track of all parsed files so you can write them out with a single saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * Tries to parse all .java files in a package recursively, and returns all files ever parsed with this source root. + * It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that the cache + * grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple + * times (where the cache is useful) you might want to use the parse method with a callback. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. */ public List> tryToParse(String startPackage) throws IOException { assertNotNull(startPackage); @@ -128,48 +146,42 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO /** * Tries to parse all .java files under the source root recursively, and returns all files ever parsed with this - * source root. - * It keeps track of all parsed files so you can write them out with a single saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * source root. It keeps track of all parsed files so you can write them out with a single saveAll() call. Note that + * the cache grows with every file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse + * files multiple times (where the cache is useful) you might want to use the parse method with a callback. */ public List> tryToParse() throws IOException { return tryToParse(""); } /** - * Tries to parse all .java files in a package recursively using multiple threads, and returns all files ever - * parsed with this source root. - * A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in - * that directory. - * Note that to ensure thread safety, a new parser instance is created for every file with the internal - * parser's (i.e. {@link #setJavaParser}) configuration. - * It keeps track of all parsed files so you can write them out with a single saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * Tries to parse all .java files in a package recursively using multiple threads, and returns all files ever parsed + * with this source root. A new thread is forked each time a new directory is visited and is responsible for parsing + * all .java files in that directory. Note that to ensure thread safety, a new parser instance is created for + * every file with the internal parser's (i.e. {@link #setJavaParser}) configuration. It keeps track of all parsed + * files so you can write them out with a single saveAll() call. Note that the cache grows with every file parsed, + * so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is + * useful) you might want to use the parse method with a callback. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. */ public List> tryToParseParallelized(String startPackage) throws IOException { assertNotNull(startPackage); logPackage(startPackage); final Path path = packageAbsolutePath(root, startPackage); - ParallelParse parse = new ParallelParse(path, new ParallelParse.VisitFileCallback() { - @Override - public FileVisitResult process(Path file, BasicFileAttributes attrs) { - if (!attrs.isDirectory() && file.toString().endsWith(".java")) { - Path relative = root.relativize(file.getParent()); - try { - tryToParse(relative.toString(), file.getFileName().toString(), new JavaParser( - SourceRoot.this.javaParser.getParserConfiguration())); - } catch (IOException e) { - Log.error(e); - } + ParallelParse parse = new ParallelParse(path, (file, attrs) -> { + if (!attrs.isDirectory() && file.toString().endsWith(".java")) { + Path relative = root.relativize(file.getParent()); + try { + tryToParse( + relative.toString(), + file.getFileName().toString(), + new JavaParser(parserConfiguration)); + } catch (IOException e) { + Log.error(e); } - return FileVisitResult.CONTINUE; } + return FileVisitResult.CONTINUE; }); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(parse); @@ -178,37 +190,31 @@ public FileVisitResult process(Path file, BasicFileAttributes attrs) { /** * Tries to parse all .java files under the source root recursively using multiple threads, and returns all files - * ever parsed with this - * source root. - * A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in - * that directory. - * Note that to ensure thread safety, a new parser instance is created for every file with the internal - * parser's (i.e. {@link #setJavaParser}) configuration. - * It keeps track of all parsed files so you can write them out with a single saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * ever parsed with this source root. A new thread is forked each time a new directory is visited and is responsible + * for parsing all .java files in that directory. Note that to ensure thread safety, a new parser instance is + * created for every file with the internal parser's (i.e. {@link #setJavaParser}) configuration. It keeps track of + * all parsed files so you can write them out with a single saveAll() call. Note that the cache grows with every + * file parsed, so if you don't need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the + * cache is useful) you might want to use the parse method with a callback. */ public List> tryToParseParallelized() throws IOException { return tryToParseParallelized(""); } /** - * Parses a .java files under the source root and returns its CompilationUnit. - * It keeps track of the parsed file so you can write it out with the saveAll() call. - * Note that the cache grows with every file parsed, - * so if you don't need saveAll(), - * or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might want to use - * the parse method with a callback. + * Parses a .java files under the source root and returns its CompilationUnit. It keeps track of the parsed file so + * you can write it out with the saveAll() call. Note that the cache grows with every file parsed, so if you don't + * need saveAll(), or you don't ask SourceRoot to parse files multiple times (where the cache is useful) you might + * want to use the parse method with a callback. * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. * @throws ParseProblemException when something went wrong. */ - public CompilationUnit parse(String pkg, String filename) { - assertNotNull(pkg); + public CompilationUnit parse(String startPackage, String filename) { + assertNotNull(startPackage); assertNotNull(filename); try { - final ParseResult result = tryToParse(pkg, filename); + final ParseResult result = tryToParse(startPackage, filename); if (result.isSuccessful()) { return result.getResult().get(); } @@ -219,15 +225,29 @@ public CompilationUnit parse(String pkg, String filename) { } /** - * 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. + * 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. + * @deprecated pass ParserConfiguration instead of JavaParser */ + @Deprecated public SourceRoot parse(String startPackage, JavaParser javaParser, Callback callback) throws IOException { + return parse(startPackage, javaParser.getParserConfiguration(), callback); + } + + /** + * 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(javaParser); + assertNotNull(configuration); assertNotNull(callback); logPackage(startPackage); + final JavaParser javaParser = new JavaParser(configuration); final Path path = packageAbsolutePath(root, startPackage); Files.walkFileTree(path, new SimpleFileVisitor() { @Override @@ -258,45 +278,40 @@ private void logPackage(String startPackage) { } /** - * Tries to parse all .java files in a package recursively using multiple threads, and passes them one by one to - * the callback. - * A new thread is forked each time a new directory is visited and is responsible for parsing all .java files in - * that directory. - * Note that the provided {@link Callback} code must be made thread-safe. - * Note that to ensure thread safety, a new parser instance is created for every file with the provided - * {@link JavaParser}'s configuration. - * In comparison to the other parse methods, this is much more memory efficient, - * but saveAll() won't work. + * Tries to parse all .java files in a package recursively using multiple threads, and passes them one by one to the + * callback. A new thread is forked each time a new directory is visited and is responsible for parsing all .java + * files in that directory. Note that the provided {@link Callback} code must be made thread-safe. Note + * that to ensure thread safety, a new parser instance is created for every file with the provided {@link + * ParserConfiguration}. 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 parseParallelized(String startPackage, JavaParser javaParser, Callback callback) + public SourceRoot parseParallelized(String startPackage, ParserConfiguration configuration, Callback callback) throws IOException { assertNotNull(startPackage); - assertNotNull(javaParser); + assertNotNull(configuration); assertNotNull(callback); logPackage(startPackage); final Path path = packageAbsolutePath(root, startPackage); - ParallelParse parse = new ParallelParse(path, new ParallelParse.VisitFileCallback() { - @Override - public FileVisitResult process(Path file, BasicFileAttributes attrs) { - if (!attrs.isDirectory() && file.toString().endsWith(".java")) { - Path localPath = root.relativize(file); - Log.trace("Parsing %s", localPath); - try { - ParseResult result = new JavaParser( - SourceRoot.this.javaParser.getParserConfiguration()).parse(COMPILATION_UNIT, - provider(file)); - result.getResult().ifPresent(cu -> cu.setStorage(file)); - if (callback.process(localPath, file, result) == SAVE) { - if (result.getResult().isPresent()) { - save(result.getResult().get(), path); - } + ParallelParse parse = new ParallelParse(path, (file, attrs) -> { + if (!attrs.isDirectory() && file.toString().endsWith(".java")) { + Path localPath = root.relativize(file); + Log.trace("Parsing %s", localPath); + try { + ParseResult result = new JavaParser(configuration) + .parse(COMPILATION_UNIT, provider(file)); + result.getResult().ifPresent(cu -> cu.setStorage(file)); + if (callback.process(localPath, file, result) == SAVE) { + if (result.getResult().isPresent()) { + save(result.getResult().get(), path); } - } catch (IOException e) { - Log.error(e); } + } catch (IOException e) { + Log.error(e); } - return FileVisitResult.CONTINUE; } + return FileVisitResult.CONTINUE; }); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(parse); @@ -304,32 +319,62 @@ public FileVisitResult process(Path file, BasicFileAttributes attrs) { } /** - * Add a newly created Java file to the cache of this source root. - * It will be saved when saveAll is called. + * Tries to parse all .java files in a package recursively using multiple threads, and passes them one by one to the + * callback. A new thread is forked each time a new directory is visited and is responsible for parsing all .java + * files in that directory. Note that the provided {@link Callback} code must be made thread-safe. Note + * that to ensure thread safety, a new parser instance is created for every file. 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 add(String pkg, String filename, CompilationUnit compilationUnit) { - assertNotNull(pkg); + public SourceRoot parseParallelized(String startPackage, Callback callback) throws IOException { + return parseParallelized(startPackage, new ParserConfiguration(), callback); + } + + /** + * Tries to parse all .java files recursively using multiple threads, and passes them one by one to the callback. A + * new thread is forked each time a new directory is visited and is responsible for parsing all .java files in that + * directory. Note that the provided {@link Callback} code must be made thread-safe. Note that to + * ensure thread safety, a new parser instance is created for every file. In comparison to the other parse methods, + * this is much more memory efficient, but saveAll() won't work. + */ + public SourceRoot parseParallelized(Callback callback) throws IOException { + return parseParallelized("", new ParserConfiguration(), callback); + } + + /** + * Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called. + * + * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. + */ + public SourceRoot add(String startPackage, String filename, CompilationUnit compilationUnit) { + assertNotNull(startPackage); assertNotNull(filename); assertNotNull(compilationUnit); - Log.trace("Adding new file %s.%s", pkg, filename); - final Path path = fileInPackageRelativePath(pkg, filename); - final ParseResult parseResult = new ParseResult<>(compilationUnit, new ArrayList<>(), null, + Log.trace("Adding new file %s.%s", startPackage, filename); + final Path path = fileInPackageRelativePath(startPackage, filename); + final ParseResult parseResult = new ParseResult<>( + compilationUnit, + new ArrayList<>(), + null, null); cache.put(path, parseResult); return this; } /** - * Add a newly created Java file to the cache of this source root. - * It will be saved when saveAll is called. - * It needs to have its path set. + * Add a newly created Java file to the cache of this source root. It will be saved when saveAll is called. It needs + * to have its path set. */ public SourceRoot add(CompilationUnit compilationUnit) { assertNotNull(compilationUnit); if (compilationUnit.getStorage().isPresent()) { final Path path = compilationUnit.getStorage().get().getPath(); Log.trace("Adding new file %s", path); - final ParseResult parseResult = new ParseResult<>(compilationUnit, new ArrayList<>(), null, + final ParseResult parseResult = new ParseResult<>( + compilationUnit, + new ArrayList<>(), + null, null); cache.put(path, parseResult); } else { @@ -373,16 +418,15 @@ public SourceRoot saveAll() { } /** - * The Java files that have been parsed by this source root object, - * or have been added manually. + * The Java files that have been parsed by this source root object, or have been added manually. */ public List> getCache() { return new ArrayList<>(cache.values()); } /** - * The CompilationUnits of the Java files that have been parsed succesfully by this source root object, - * or have been added manually. + * The CompilationUnits of the Java files that have been parsed succesfully by this source root object, or have been + * added manually. */ public List getCompilationUnits() { return cache.values().stream() @@ -398,16 +442,36 @@ public Path getRoot() { return root; } + /** + * @deprecated store ParserConfiguration now + */ + @Deprecated public JavaParser getJavaParser() { - return javaParser; + return new JavaParser(parserConfiguration); } /** * Set the parser that is used for parsing by default. + * + * @deprecated store ParserConfiguration now */ + @Deprecated public SourceRoot setJavaParser(JavaParser javaParser) { assertNotNull(javaParser); - this.javaParser = javaParser; + this.parserConfiguration = javaParser.getParserConfiguration(); + return this; + } + + public ParserConfiguration getParserConfiguration() { + return parserConfiguration; + } + + /** + * Set the parser configuration that is used for parsing when no configuration is passed to a method. + */ + public SourceRoot setParserConfiguration(ParserConfiguration parserConfiguration) { + assertNotNull(parserConfiguration); + this.parserConfiguration = parserConfiguration; return this; } @@ -429,9 +493,9 @@ public Function getPrinter() { /** * Executes a recursive file tree walk using threads. A new thread is invoked for each new directory discovered - * during the walk. For each file visited, the user-provided {@link VisitFileCallback} is called - * with the current path and file attributes. Any shared resources accessed in a {@link VisitFileCallback} should - * be made thread-safe. + * during the walk. For each file visited, the user-provided {@link VisitFileCallback} is called with the current + * path and file attributes. Any shared resources accessed in a {@link VisitFileCallback} should be made + * thread-safe. */ private static class ParallelParse extends RecursiveAction { @@ -476,7 +540,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } } - static interface VisitFileCallback { + interface VisitFileCallback { FileVisitResult process(Path file, BasicFileAttributes attrs); } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceZip.java b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceZip.java index 490f455d96..c8eeef8482 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/utils/SourceZip.java +++ b/javaparser-core/src/main/java/com/github/javaparser/utils/SourceZip.java @@ -47,7 +47,7 @@ public class SourceZip { private final Path zipPath; - private JavaParser javaParser; + private ParserConfiguration parserConfiguration; /** * Create a new ZIP parser. An instance of {@link JavaParser} with the default {@link ParserConfiguration} will be @@ -70,7 +70,7 @@ public SourceZip(Path zipPath, ParserConfiguration configuration) { assertNotNull(zipPath); assertNotNull(configuration); this.zipPath = zipPath.normalize(); - this.javaParser = new JavaParser(configuration); + this.parserConfiguration = configuration; Log.info("New source zip at \"%s\"", this.zipPath); } @@ -99,6 +99,7 @@ public List>> parse() throws IOException */ public SourceZip parse(Callback callback) throws IOException { Log.info("Parsing zip at \"%s\"", zipPath); + JavaParser javaParser = new JavaParser(parserConfiguration); try (ZipFile zipFile = new ZipFile(zipPath.toFile())) { for (ZipEntry entry : Collections.list(zipFile.entries())) { if (!entry.isDirectory() && entry.getName().endsWith(".java")) { @@ -136,23 +137,13 @@ public Path getZipPath() { return zipPath; } - /** - * Get the parser used for parsing. - * - * @return The currently set parser. - */ - public JavaParser getJavaParser() { - return javaParser; + public ParserConfiguration getParserConfiguration() { + return parserConfiguration; } - /** - * Set the parser that is used for parsing. - * - * @param javaParser The parser to use. - */ - public SourceZip setJavaParser(JavaParser javaParser) { - assertNotNull(javaParser); - this.javaParser = javaParser; + public SourceZip setParserConfiguration(ParserConfiguration parserConfiguration) { + assertNotNull(parserConfiguration); + this.parserConfiguration = parserConfiguration; return this; } } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/manual/BulkParseTest.java b/javaparser-testing/src/test/java/com/github/javaparser/manual/BulkParseTest.java index e4bca41238..c843594777 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/manual/BulkParseTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/manual/BulkParseTest.java @@ -1,9 +1,7 @@ package com.github.javaparser.manual; -import com.github.javaparser.JavaParser; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.Problem; -import com.github.javaparser.ast.validator.Java8Validator; import com.github.javaparser.ast.validator.Java9Validator; import com.github.javaparser.utils.CodeGenerationUtils; import com.github.javaparser.utils.Log; @@ -23,7 +21,8 @@ import static com.github.javaparser.utils.CodeGenerationUtils.f; import static com.github.javaparser.utils.SourceRoot.Callback.Result.DONT_SAVE; -import static com.github.javaparser.utils.TestUtils.*; +import static com.github.javaparser.utils.TestUtils.download; +import static com.github.javaparser.utils.TestUtils.temporaryDirectory; import static java.util.Comparator.comparing; public class BulkParseTest { @@ -59,13 +58,16 @@ private void parseJdkSrcZip() throws IOException { @Test public void parseOwnSourceCode() throws IOException { - bulkTest(new SourceRoot(CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve("..")), "javaparser_test_results.txt", new ParserConfiguration().setValidator(new Java9Validator())); + bulkTest( + new SourceRoot(CodeGenerationUtils.mavenModuleRoot(BulkParseTest.class).resolve("..")), + "javaparser_test_results.txt", + new ParserConfiguration().setValidator(new Java9Validator())); } public void bulkTest(SourceRoot sourceRoot, String testResultsFileName, ParserConfiguration configuration) throws IOException { - sourceRoot.setJavaParser(new JavaParser(configuration)); + sourceRoot.setParserConfiguration(configuration); TreeMap> results = new TreeMap<>(comparing(o -> o.toString().toLowerCase())); - sourceRoot.parse("", new JavaParser(), (localPath, absolutePath, result) -> { + sourceRoot.parseParallelized((localPath, absolutePath, result) -> { if (!localPath.toString().contains("target")) { if (!result.isSuccessful()) { results.put(localPath, result.getProblems()); @@ -77,7 +79,7 @@ public void bulkTest(SourceRoot sourceRoot, String testResultsFileName, ParserCo } public void bulkTest(SourceZip sourceRoot, String testResultsFileName, ParserConfiguration configuration) throws IOException { - sourceRoot.setJavaParser(new JavaParser(configuration)); + sourceRoot.setParserConfiguration(configuration); TreeMap> results = new TreeMap<>(comparing(o -> o.toString().toLowerCase())); sourceRoot.parse((path, result) -> { if (!path.toString().contains("target")) {