diff options
author | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-20 18:53:20 +0100 |
---|---|---|
committer | Danny van Bruggen <hexagonaal@gmail.com> | 2018-02-20 18:53:20 +0100 |
commit | 316814ceef6ee2158db22b0e2c63e1e8e4f81aeb (patch) | |
tree | 10b769eefc94b591e4cded7e4cbbb048f1fae6b0 /javaparser-core/src/main | |
parent | fbd8c73b0a66f3d71cd073c7b2cb61a1112f7ba6 (diff) | |
download | platform_external_javaparser-316814ceef6ee2158db22b0e2c63e1e8e4f81aeb.tar.gz platform_external_javaparser-316814ceef6ee2158db22b0e2c63e1e8e4f81aeb.tar.bz2 platform_external_javaparser-316814ceef6ee2158db22b0e2c63e1e8e4f81aeb.zip |
Let SourceRoot skip unlikely directories
Diffstat (limited to 'javaparser-core/src/main')
-rw-r--r-- | javaparser-core/src/main/java/com/github/javaparser/utils/SourceRoot.java | 57 |
1 files changed, 41 insertions, 16 deletions
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 a8a19a338..15db5a620 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 @@ -20,6 +20,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; import java.util.function.Function; +import java.util.regex.Pattern; import java.util.stream.Collectors; import static com.github.javaparser.ParseStart.COMPILATION_UNIT; @@ -28,14 +29,16 @@ import static com.github.javaparser.utils.CodeGenerationUtils.fileInPackageRelat 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 static java.nio.file.FileVisitResult.CONTINUE; +import static java.nio.file.FileVisitResult.SKIP_SUBTREE; /** * 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. <b>Note that</b> the internal cache used is thread-safe. * <ul> - * <li>methods called "tryToParse..." will return their result inside a "ParseResult", which supports parse successes and failures.</li> - * <li>methods called "parse..." will return "CompilationUnit"s. If a file fails to parse, an exception is thrown.</li> - * <li>methods ending in "...Parallelized" will speed up parsing by using multiple threads.</li> + * <li>methods called "tryToParse..." will return their result inside a "ParseResult", which supports parse successes and failures.</li> + * <li>methods called "parse..." will return "CompilationUnit"s. If a file fails to parse, an exception is thrown.</li> + * <li>methods ending in "...Parallelized" will speed up parsing by using multiple threads.</li> * </ul> */ public class SourceRoot { @@ -57,6 +60,7 @@ public class SourceRoot { private final Map<Path, ParseResult<CompilationUnit>> cache = new ConcurrentHashMap<>(); private ParserConfiguration parserConfiguration = new ParserConfiguration(); private Function<CompilationUnit, String> printer = new PrettyPrinter()::print; + private static final Pattern JAVA_IDENTIFIER = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*"); public SourceRoot(Path root) { assertNotNull(root); @@ -143,12 +147,27 @@ public class SourceRoot { Path relative = root.relativize(file.getParent()); tryToParse(relative.toString(), file.getFileName().toString()); } - return FileVisitResult.CONTINUE; + return CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + return isSensibleDirectoryToEnter(dir)?CONTINUE:SKIP_SUBTREE; } }); return getCache(); } + private static boolean isSensibleDirectoryToEnter(Path dir) throws IOException { + final String dirToEnter = dir.getFileName().toString(); + final boolean directoryIsAValidJavaIdentifier = JAVA_IDENTIFIER.matcher(dirToEnter).matches(); + if (Files.isHidden(dir) || !directoryIsAValidJavaIdentifier) { + Log.trace("Not processing directory \"%s\"", dirToEnter); + return false; + } + return true; + } + /** * 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 @@ -170,7 +189,7 @@ public class SourceRoot { * * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. */ - public List<ParseResult<CompilationUnit>> tryToParseParallelized(String startPackage) throws IOException { + public List<ParseResult<CompilationUnit>> tryToParseParallelized(String startPackage) { assertNotNull(startPackage); logPackage(startPackage); final Path path = packageAbsolutePath(root, startPackage); @@ -181,12 +200,12 @@ public class SourceRoot { tryToParse( relative.toString(), file.getFileName().toString(), - new JavaParser(parserConfiguration)); + parserConfiguration); } catch (IOException e) { Log.error(e); } } - return FileVisitResult.CONTINUE; + return CONTINUE; }); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(parse); @@ -269,7 +288,12 @@ public class SourceRoot { } } } - return FileVisitResult.CONTINUE; + return CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + return isSensibleDirectoryToEnter(dir)?CONTINUE:SKIP_SUBTREE; } }); return this; @@ -292,8 +316,7 @@ public class SourceRoot { * * @param startPackage files in this package and deeper are parsed. Pass "" to parse all files. */ - public SourceRoot parseParallelized(String startPackage, ParserConfiguration configuration, Callback callback) - throws IOException { + public SourceRoot parseParallelized(String startPackage, ParserConfiguration configuration, Callback callback) { assertNotNull(startPackage); assertNotNull(configuration); assertNotNull(callback); @@ -316,7 +339,7 @@ public class SourceRoot { Log.error(e); } } - return FileVisitResult.CONTINUE; + return CONTINUE; }); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(parse); @@ -519,20 +542,22 @@ public class SourceRoot { try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) - throws IOException { + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if(!SourceRoot.isSensibleDirectoryToEnter(dir)){ + return SKIP_SUBTREE; + } if (!dir.equals(ParallelParse.this.path)) { ParallelParse w = new ParallelParse(dir, callback); w.fork(); walks.add(w); - return FileVisitResult.SKIP_SUBTREE; + return SKIP_SUBTREE; } else { - return FileVisitResult.CONTINUE; + return CONTINUE; } } @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { return callback.process(file, attrs); } }); |