From 7fccda0f13f6f8b7f7a1f6e87a648fe1c3396656 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Apr 2016 14:52:21 +0100 Subject: Use JSON to configure CLI --- .../couscous/frontends/java/JavaFrontend.java | 28 +++++++++++++--------- .../couscous/frontends/java/JavaParser.java | 6 ++--- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src/main/java/org/zwobble/couscous/frontends') diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java index 9925068..b633093 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java @@ -1,34 +1,40 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.Multimaps; -import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.Node; +import org.zwobble.couscous.ast.TypeNode; +import org.zwobble.couscous.ast.VariableDeclaration; +import org.zwobble.couscous.ast.VariableNode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.zwobble.couscous.ast.structure.NodeStructure.descendantNodesAndSelf; import static org.zwobble.couscous.util.Casts.tryCast; +import static org.zwobble.couscous.util.ExtraIterables.iterable; import static org.zwobble.couscous.util.ExtraLists.eagerFlatMap; -import static org.zwobble.couscous.util.ExtraLists.flatMap; -import static org.zwobble.couscous.util.ExtraLists.list; import static org.zwobble.couscous.util.ExtraStreams.toStream; public class JavaFrontend { - public List readSourceDirectory(List sourcePaths, Path directoryPath) throws IOException { - List classNodes = flatMap( - findJavaFiles(directoryPath), - javaFile -> JavaReader.readClassesFromFiles(sourcePaths, list(javaFile))); + public List readSourceDirectory(List sourcePaths, List sourceFiles) throws IOException { + List expandedSourceFiles = eagerFlatMap(sourceFiles, this::findJavaFiles); + List classNodes = JavaReader.readClassesFromFiles(sourcePaths, expandedSourceFiles); ensureDeclarationsAreUnique(classNodes); return classNodes; } - private Stream findJavaFiles(Path directoryPath) throws IOException { - return Files.walk(directoryPath) - .filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); + private Iterable findJavaFiles(Path directoryPath) { + return iterable(() -> { + try { + return Files.walk(directoryPath) + .filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + }); } private void ensureDeclarationsAreUnique(List classNodes) { diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java index e1addd3..e97d853 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaParser.java @@ -25,6 +25,9 @@ public class JavaParser { public JavaParser() { parser = ASTParser.newParser(AST.JLS8); + } + + public CompilationUnit parseCompilationUnit(List sourcePaths, Path sourcePath) throws IOException { parser.setBindingsRecovery(false); parser.setKind(ASTParser.K_COMPILATION_UNIT); @SuppressWarnings("unchecked") @@ -34,9 +37,6 @@ public class JavaParser { options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); parser.setCompilerOptions(options); parser.setResolveBindings(true); - } - - public CompilationUnit parseCompilationUnit(List sourcePaths, Path sourcePath) throws IOException { parser.setUnitName(sourcePath.toString()); String[] sourcePathArguments = Iterables.toArray( -- cgit v1.2.3