diff --git a/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/Concatenated.java b/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/Concatenated.java index 3196f672..1b5b092e 100644 --- a/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/Concatenated.java +++ b/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/Concatenated.java @@ -124,11 +124,8 @@ public PathContentFragment(Path value) { @Override public void writeTo(OutputStream out, TargetEvaluationContext ctx) throws IOException { - InputStream in = new FileInputStream(ctx.cached(value)); - try { + try (InputStream in = new FileInputStream(ctx.cached(value))) { ctx.iwant().pipe(in, out); - } finally { - in.close(); } } @@ -223,13 +220,10 @@ protected IngredientsAndParametersDefined ingredientsAndParameters( @Override public void path(TargetEvaluationContext ctx) throws Exception { - OutputStream out = new FileOutputStream(ctx.cached(this)); - try { + try (OutputStream out = new FileOutputStream(ctx.cached(this))) { for (Fragment fragment : fragments) { fragment.writeTo(out, ctx); } - } finally { - out.close(); } } diff --git a/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/HelloSideEffect.java b/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/HelloSideEffect.java index d56d8aff..6989e1f4 100644 --- a/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/HelloSideEffect.java +++ b/essential/iwant-api-core/src/main/java/org/fluentjava/iwant/api/core/HelloSideEffect.java @@ -26,12 +26,12 @@ public String name() { @Override public void mutate(SideEffectContext ctx) throws Exception { - PrintWriter err = new PrintWriter(ctx.err()); - err.print(name() + " mutating.\n"); - if (message != null) { - err.print(message); + try (PrintWriter err = new PrintWriter(ctx.err())) { + err.print(name() + " mutating.\n"); + if (message != null) { + err.print(message); + } } - err.close(); } } diff --git a/essential/iwant-eclipse-settings/src/main/java/org/fluentjava/iwant/eclipsesettings/EclipseSettings.java b/essential/iwant-eclipse-settings/src/main/java/org/fluentjava/iwant/eclipsesettings/EclipseSettings.java index 9fb672e0..de870359 100644 --- a/essential/iwant-eclipse-settings/src/main/java/org/fluentjava/iwant/eclipsesettings/EclipseSettings.java +++ b/essential/iwant-eclipse-settings/src/main/java/org/fluentjava/iwant/eclipsesettings/EclipseSettings.java @@ -47,11 +47,11 @@ public void mutate(SideEffectContext ctx) { generateEclipseSettings(ctx); ensureSrcDirs(ctx); if (refreshFailure != null) { - PrintWriter err = new PrintWriter(ctx.err()); - err.println( - "WARNING: Refresh of eclipse settings references failed:\n" - + refreshFailure); - err.close(); + try (PrintWriter err = new PrintWriter(ctx.err())) { + err.println( + "WARNING: Refresh of eclipse settings references failed:\n" + + refreshFailure); + } } } catch (RuntimeException e) { throw e; diff --git a/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java b/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java index ac1dd246..90160f33 100644 --- a/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java +++ b/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java @@ -61,8 +61,7 @@ public PathOrContent(Target target) { } public File asPath() { - try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { OutputStream err = System.err; File iwantApiClasses = null; Iwant iwant = Iwant.usingRealNetwork(); @@ -77,7 +76,6 @@ public File asPath() { wsRoot, iwant, wsInfo, caches, workerCount, wsdefdefJavaModule, wsdefJavaModule, wsdefCtx); evaluator.asPath(target); - out.close(); String cachedTarget = out.toString(); // TODO refactoring needed, we shouldn't be parsing // strings from our own code diff --git a/essential/iwant-entry/as-some-developer/with/java/org/fluentjava/iwant/entry/Iwant.java b/essential/iwant-entry/as-some-developer/with/java/org/fluentjava/iwant/entry/Iwant.java index 846ef8a4..713a8b6f 100644 --- a/essential/iwant-entry/as-some-developer/with/java/org/fluentjava/iwant/entry/Iwant.java +++ b/essential/iwant-entry/as-some-developer/with/java/org/fluentjava/iwant/entry/Iwant.java @@ -462,30 +462,30 @@ public File compiledClasses(File dest, List src, String classpath, } DiagnosticListener diagnosticListener = null; Locale locale = null; - StandardJavaFileManager fileManager = compiler + try (StandardJavaFileManager fileManager = compiler .getStandardFileManager(diagnosticListener, locale, - encoding); - Iterable compilationUnits = fileManager - .getJavaFileObjectsFromFiles(src); - Writer compilerTaskOut = null; - Iterable classes = null; - - List options = new ArrayList<>(); - options.addAll(javacOptions); - options.add("-d"); - options.add(dest.getCanonicalPath()); - options.add("-classpath"); - options.add(classpath); - options.add("-encoding"); - options.add(encoding.toString()); - - CompilationTask compilerTask = compiler.getTask(compilerTaskOut, - fileManager, diagnosticListener, options, classes, - compilationUnits); - Boolean compilerTaskResult = compilerTask.call(); - fileManager.close(); - if (!compilerTaskResult) { - throw new IwantException("Compilation failed."); + encoding)) { + Iterable compilationUnits = fileManager + .getJavaFileObjectsFromFiles(src); + Writer compilerTaskOut = null; + Iterable classes = null; + + List options = new ArrayList<>(); + options.addAll(javacOptions); + options.add("-d"); + options.add(dest.getCanonicalPath()); + options.add("-classpath"); + options.add(classpath); + options.add("-encoding"); + options.add(encoding.toString()); + + CompilationTask compilerTask = compiler.getTask(compilerTaskOut, + fileManager, diagnosticListener, options, classes, + compilationUnits); + Boolean compilerTaskResult = compilerTask.call(); + if (!compilerTaskResult) { + throw new IwantException("Compilation failed."); + } } return dest; } catch (RuntimeException e) { @@ -769,9 +769,9 @@ public void downloaded(URL from, File to) { debugLog("Downloading", "from " + from); log("Downloading", to); byte[] bytes = downloadBytes(from); - FileOutputStream cachedOut = new FileOutputStream(to); - cachedOut.write(bytes); - cachedOut.close(); + try (FileOutputStream cachedOut = new FileOutputStream(to)) { + cachedOut.write(bytes); + } } catch (RuntimeException e) { throw e; } catch (Exception e) { @@ -798,10 +798,10 @@ private static byte[] downloadBytes(URL url) return downloadBytes(new URL(location)); } } - InputStream in = conn.getInputStream(); - byte[] respBody = readBytes(in); - in.close(); - return respBody; + try (InputStream in = conn.getInputStream()) { + byte[] respBody = readBytes(in); + return respBody; + } } private static boolean isRedirect(int status) { @@ -855,32 +855,32 @@ public File unmodifiableZipUnzipped(UnmodifiableZip src) { File tmp = new File(dest + ".tmp"); del(tmp); mkdirs(tmp); - ZipInputStream zip = new ZipInputStream( - src.location().openStream()); - ZipEntry e = null; - byte[] buffer = new byte[32 * 1024]; - boolean zipHasContent = false; - while ((e = zip.getNextEntry()) != null) { - zipHasContent = true; - File entryFile = new File(tmp, e.getName()); - if (e.isDirectory()) { - mkdirs(entryFile); - continue; - } - OutputStream out = new FileOutputStream(entryFile); - while (true) { - int bytesRead = zip.read(buffer); - if (bytesRead <= 0) { - break; + try (ZipInputStream zip = new ZipInputStream( + src.location().openStream())) { + ZipEntry e = null; + byte[] buffer = new byte[32 * 1024]; + boolean zipHasContent = false; + while ((e = zip.getNextEntry()) != null) { + zipHasContent = true; + File entryFile = new File(tmp, e.getName()); + if (e.isDirectory()) { + mkdirs(entryFile); + continue; + } + try (OutputStream out = new FileOutputStream(entryFile)) { + while (true) { + int bytesRead = zip.read(buffer); + if (bytesRead <= 0) { + break; + } + out.write(buffer, 0, bytesRead); + } } - out.write(buffer, 0, bytesRead); } - out.close(); - } - zip.close(); - if (!zipHasContent) { - throw new IwantException( - "Corrupt (or empty, no way to tell): " + src); + if (!zipHasContent) { + throw new IwantException( + "Corrupt (or empty, no way to tell): " + src); + } } fileRenamedTo(tmp, dest); return dest; diff --git a/essential/iwant-entry2/src/main/java/org/fluentjava/iwant/entry2/Iwant2.java b/essential/iwant-entry2/src/main/java/org/fluentjava/iwant/entry2/Iwant2.java index b66fb376..0638ed03 100644 --- a/essential/iwant-entry2/src/main/java/org/fluentjava/iwant/entry2/Iwant2.java +++ b/essential/iwant-entry2/src/main/java/org/fluentjava/iwant/entry2/Iwant2.java @@ -289,20 +289,10 @@ private String currentSourceDescriptorContent() { } public static String contentAsString(File file) { - InputStream in = null; - try { - in = new FileInputStream(file); + try (InputStream in = new FileInputStream(file)) { return toString(in); } catch (IOException e) { throw new RuntimeException(e); - } finally { - try { - if (in != null) { - in.close(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } } } diff --git a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/Iwant3.java b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/Iwant3.java index 32c6ee51..e805d7c6 100644 --- a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/Iwant3.java +++ b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/Iwant3.java @@ -215,13 +215,10 @@ private static UserPrefs parseUserPrefs(File iHaveConf) throws IOException { if (!userPrefsFile.exists()) { return new DefaultUserPrefs(userPrefsFile); } - FileReader reader = new FileReader(userPrefsFile); - try { + try (FileReader reader = new FileReader(userPrefsFile)) { Properties props = new Properties(); props.load(reader); return new UserPrefsImpl(props, userPrefsFile); - } finally { - reader.close(); } } diff --git a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/WishEvaluator.java b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/WishEvaluator.java index 5f35f057..c90936b0 100644 --- a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/WishEvaluator.java +++ b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/WishEvaluator.java @@ -87,20 +87,20 @@ public void iwant(String wish, Workspace ws) { TargetNameChecker.check(targets); if ("list-of/targets".equals(wish)) { - PrintWriter wr = new PrintWriter(out); - for (Target target : ws.targets(ctx)) { - wr.println(target.name()); + try (PrintWriter wr = new PrintWriter(out)) { + for (Target target : ws.targets(ctx)) { + wr.println(target.name()); + } + return; } - wr.close(); - return; } if ("list-of/side-effects".equals(wish)) { - PrintWriter wr = new PrintWriter(out); - for (SideEffect se : ws.sideEffects(ctx)) { - wr.println(se.name()); + try (PrintWriter wr = new PrintWriter(out)) { + for (SideEffect se : ws.sideEffects(ctx)) { + wr.println(se.name()); + } + return; } - wr.close(); - return; } for (Target target : targets) { if (wish.equals("target/" + target.name() + "/as-path")) { @@ -170,9 +170,9 @@ public void content(Target target) { public void asPath(Path path) { File cachedContent = freshCachedContent(path); - PrintWriter wr = new PrintWriter(out); - wr.println(cachedContent); - wr.close(); + try (PrintWriter wr = new PrintWriter(out)) { + wr.println(cachedContent); + } } private class Ctx implements IKnowWhatIAmDoingContext { diff --git a/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsPath.java b/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsPath.java index f5731626..c7a4333c 100644 --- a/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsPath.java +++ b/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsPath.java @@ -42,9 +42,9 @@ public InputStream content(TargetEvaluationContext ctx) throws Exception { @Override public void path(TargetEvaluationContext ctx) throws Exception { File path = ctx.cached(this); - OutputStream out = new FileOutputStream(path); - StreamUtil.pipe(content(ctx), out); - out.close(); + try (OutputStream out = new FileOutputStream(path)) { + StreamUtil.pipe(content(ctx), out); + } } @Override diff --git a/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsStream.java b/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsStream.java index 18da3e55..6ca9e24e 100644 --- a/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsStream.java +++ b/essential/iwant-entry3/src/test/java/org/fluentjava/iwant/entry3/TargetThatNeedsAnotherAsStream.java @@ -39,9 +39,9 @@ public InputStream content(TargetEvaluationContext ctx) throws Exception { @Override public void path(TargetEvaluationContext ctx) throws Exception { File path = ctx.cached(this); - OutputStream out = new FileOutputStream(path); - StreamUtil.pipe(content(ctx), out); - out.close(); + try (OutputStream out = new FileOutputStream(path)) { + StreamUtil.pipe(content(ctx), out); + } } @Override diff --git a/private/iwant-testarea/src/main/java/org/fluentjava/iwant/testarea/TestArea.java b/private/iwant-testarea/src/main/java/org/fluentjava/iwant/testarea/TestArea.java index 019e4d6f..1088d8fc 100644 --- a/private/iwant-testarea/src/main/java/org/fluentjava/iwant/testarea/TestArea.java +++ b/private/iwant-testarea/src/main/java/org/fluentjava/iwant/testarea/TestArea.java @@ -73,14 +73,12 @@ public String contentOf(String relativePath) { @SuppressWarnings("static-method") public String contentOf(File file) { - try { - FileReader reader = new FileReader(file); + try (FileReader reader = new FileReader(file)) { StringBuilder actual = new StringBuilder(); int c; while ((c = reader.read()) >= 0) { actual.append((char) c); } - reader.close(); return actual.toString(); } catch (RuntimeException e) { throw e;