Skip to content

Commit

Permalink
added way to exit parsing with a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
daans committed May 31, 2018
1 parent 1225422 commit 11b5229
Showing 1 changed file with 57 additions and 39 deletions.
Expand Up @@ -26,10 +26,8 @@
import static com.github.javaparser.ParseStart.COMPILATION_UNIT; import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
import static com.github.javaparser.Providers.provider; import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.CodeGenerationUtils.*; import static com.github.javaparser.utils.CodeGenerationUtils.*;
import static com.github.javaparser.utils.SourceRoot.Callback.Result.SAVE;
import static com.github.javaparser.utils.Utils.assertNotNull; import static com.github.javaparser.utils.Utils.assertNotNull;
import static java.nio.file.FileVisitResult.CONTINUE; import static java.nio.file.FileVisitResult.*;
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. The root directory * A collection of Java source files located in one directory and its subdirectories on the file system. The root directory
Expand All @@ -45,7 +43,7 @@ public class SourceRoot {
@FunctionalInterface @FunctionalInterface
public interface Callback { public interface Callback {
enum Result { enum Result {
SAVE, DONT_SAVE SAVE, DONT_SAVE, TERMINATE
} }


/** /**
Expand Down Expand Up @@ -258,21 +256,24 @@ public CompilationUnit parse(String startPackage, String filename) {
} }
} }


/** private FileVisitResult callback(Path absolutePath, ParserConfiguration configuration, Callback callback) throws IOException {
* Parses the provided .java file and passes it to the callback. In comparison to the other parse methods, this is much more memory efficient, but saveAll() won't work. if (!Files.exists(absolutePath)) {
*/ return TERMINATE;
public SourceRoot parse(Path absolutePath, ParserConfiguration configuration, Callback callback) throws IOException { }
Path localPath = root.relativize(absolutePath); Path localPath = root.relativize(absolutePath);
Log.trace("Parsing %s", localPath); Log.trace("Parsing %s", localPath);
ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(absolutePath)); ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(absolutePath));
result.getResult().ifPresent(cu -> cu.setStorage(absolutePath)); result.getResult().ifPresent(cu -> cu.setStorage(absolutePath));
if (callback.process(localPath, absolutePath, result) == SAVE) { switch (callback.process(localPath, absolutePath, result)) {
if (result.getResult().isPresent()) { case SAVE:
CompilationUnit compilationUnit = result.getResult().get(); result.getResult().ifPresent(cu -> save(cu, absolutePath));
save(compilationUnit, absolutePath); case DONT_SAVE:
} return CONTINUE;
case TERMINATE:
return TERMINATE;
default:
throw new AssertionError("Return an enum defined in SourceRoot.Callback.Result");
} }
return this;
} }


/** /**
Expand All @@ -288,7 +289,16 @@ public SourceRoot parse(String startPackage, String filename, ParserConfiguratio
assertNotNull(filename); assertNotNull(filename);
assertNotNull(configuration); assertNotNull(configuration);
assertNotNull(callback); assertNotNull(callback);
parse(fileInPackageAbsolutePath(root, startPackage, filename), configuration, callback); callback(fileInPackageAbsolutePath(root, startPackage, filename), configuration, callback);
return this;
}

/**
* Parses the provided .java file and passes it to the callback. In comparison to the other parse methods, this
* makes is much more memory efficient., but saveAll() won't work.
*/
public SourceRoot parse(String startPackage, String filename, Callback callback) throws IOException {
parse(startPackage, filename, parserConfiguration, callback);
return this; return this;
} }


Expand All @@ -315,22 +325,28 @@ public SourceRoot parse(String startPackage, ParserConfiguration configuration,
assertNotNull(configuration); assertNotNull(configuration);
assertNotNull(callback); assertNotNull(callback);
logPackage(startPackage); logPackage(startPackage);
final JavaParser javaParser = new JavaParser(configuration);
final Path path = packageAbsolutePath(root, startPackage); final Path path = packageAbsolutePath(root, startPackage);
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { if (Files.exists(path)) {
@Override Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path absolutePath, BasicFileAttributes attrs) throws IOException { @Override
if (!attrs.isDirectory() && absolutePath.toString().endsWith(".java")) { public FileVisitResult visitFile(Path absolutePath, BasicFileAttributes attrs) throws IOException {
parse(absolutePath, configuration, callback); if (!attrs.isDirectory() && absolutePath.toString().endsWith(".java")) {
return callback(absolutePath, configuration, callback);
}
return CONTINUE;
} }
return CONTINUE;
}


@Override @Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return isSensibleDirectoryToEnter(dir) ? CONTINUE : SKIP_SUBTREE; return isSensibleDirectoryToEnter(dir) ? CONTINUE : SKIP_SUBTREE;
} }
}); });
}
return this;
}

public SourceRoot parse(String startPackage, Callback callback) throws IOException {
parse(startPackage, parserConfiguration, callback);
return this; return this;
} }


Expand All @@ -357,18 +373,20 @@ public SourceRoot parseParallelized(String startPackage, ParserConfiguration con
assertNotNull(callback); assertNotNull(callback);
logPackage(startPackage); logPackage(startPackage);
final Path path = packageAbsolutePath(root, startPackage); final Path path = packageAbsolutePath(root, startPackage);
ParallelParse parse = new ParallelParse(path, (absolutePath, attrs) -> { if (Files.exists(path)) {
if (!attrs.isDirectory() && absolutePath.toString().endsWith(".java")) { ParallelParse parse = new ParallelParse(path, (absolutePath, attrs) -> {
try { if (!attrs.isDirectory() && absolutePath.toString().endsWith(".java")) {
parse(absolutePath, configuration, callback); try {
} catch (IOException e) { return callback(absolutePath, configuration, callback);
Log.error(e); } catch (IOException e) {
Log.error(e);
}
} }
} return CONTINUE;
return CONTINUE; });
}); ForkJoinPool pool = new ForkJoinPool();
ForkJoinPool pool = new ForkJoinPool(); pool.invoke(parse);
pool.invoke(parse); }
return this; return this;
} }


Expand Down

0 comments on commit 11b5229

Please sign in to comment.