From 12c91d0c9fc8b9e400aae47058ae41d92602f6eb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Oct 2015 21:18:38 +0000 Subject: Throw IOException rather than wrapping in RuntimeException --- .../couscous/frontends/java/JavaFrontend.java | 19 +++++------- .../java/org/zwobble/couscous/util/ExtraLists.java | 36 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) (limited to 'src/main/java') 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 d7bd54f..f58e6af 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java @@ -4,26 +4,21 @@ 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 org.zwobble.couscous.ast.ClassNode; +import org.zwobble.couscous.util.ExtraLists; public class JavaFrontend { public static List readSourceDirectory(Path directoryPath) throws IOException { JavaReader reader = new JavaReader(); - return findJavaFiles(directoryPath) - .map(javaFile -> { - // TODO: unwrap exception in outer scope - try { - return reader.readClassFromFile(directoryPath, javaFile); - } catch (Exception e) { - throw new RuntimeException(e); - } - }) - .collect(Collectors.toList()); + return ExtraLists.map( + findJavaFiles(directoryPath), + javaFile -> reader.readClassFromFile(directoryPath, javaFile)); } private static Stream findJavaFiles(Path directoryPath) throws IOException { - return Files.walk(directoryPath).filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); + return Files.walk(directoryPath) + .filter(path -> path.toFile().isFile() && path.toString().endsWith(".java")); } } \ No newline at end of file diff --git a/src/main/java/org/zwobble/couscous/util/ExtraLists.java b/src/main/java/org/zwobble/couscous/util/ExtraLists.java index b3c017f..36c9b06 100644 --- a/src/main/java/org/zwobble/couscous/util/ExtraLists.java +++ b/src/main/java/org/zwobble/couscous/util/ExtraLists.java @@ -3,8 +3,44 @@ package org.zwobble.couscous.util; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class ExtraLists { + @SuppressWarnings("unchecked") + public static List map( + Stream stream, + CheckedFunction function) throws E { + try { + return stream + .map(value -> { + try { + return function.apply(value); + } catch (Exception exception) { + throw new StashedException(exception); + } + }) + .collect(Collectors.toList()); + } catch (StashedException exception) { + throw (E)exception.exception; + } + } + + @FunctionalInterface + public static interface CheckedFunction { + R apply(T value) throws E; + } + + private static class StashedException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private Exception exception; + + private StashedException(Exception exception) { + this.exception = exception; + } + } + public static R foldLeft( List list, Function initialValue, -- cgit v1.2.3