groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [4/4] groovy git commit: Minor refactoring: 'try finally' replaceable with 'try' with resources
Date Sat, 25 Aug 2018 17:02:04 GMT
Minor refactoring: 'try finally' replaceable with 'try' with resources

(cherry picked from commit 80a8b199037258523deb52b429dcaf0a51c517f3)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6c615579
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6c615579
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6c615579

Branch: refs/heads/GROOVY_2_5_X
Commit: 6c615579f18d6ed439bba97cab6d8a07a3f524ba
Parents: 5b4de90
Author: Daniel Sun <sunlan@apache.org>
Authored: Sun Aug 26 00:38:03 2018 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Sun Aug 26 00:47:23 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/util/CharsetToolkit.java | 13 +++-------
 .../groovy/groovy/util/GroovyScriptEngine.java  | 11 +--------
 .../groovy/control/CompilationUnit.java         | 12 +--------
 .../groovy/control/SourceExtensionHandler.java  |  6 +----
 .../codehaus/groovy/runtime/WritableFile.java   | 11 +++------
 .../groovy/tools/LoaderConfiguration.java       |  5 +---
 .../groovy/tools/javac/JavaStubGenerator.java   | 26 +++++++-------------
 .../transform/ASTTransformationVisitor.java     |  7 +-----
 8 files changed, 20 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/groovy/groovy/util/CharsetToolkit.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/util/CharsetToolkit.java b/src/main/groovy/groovy/util/CharsetToolkit.java
index fd7893b..b5f5ee6 100644
--- a/src/main/groovy/groovy/util/CharsetToolkit.java
+++ b/src/main/groovy/groovy/util/CharsetToolkit.java
@@ -75,25 +75,18 @@ public class CharsetToolkit {
         this.file = file;
         this.defaultCharset = getDefaultSystemCharset();
         this.charset = null;
-        InputStream input = new FileInputStream(file);
-        try {
+        try (InputStream input = new FileInputStream(file)) {
             byte[] bytes = new byte[4096];
             int bytesRead = input.read(bytes);
             if (bytesRead == -1) {
                 this.buffer = EMPTY_BYTE_ARRAY;
-            }
-            else if (bytesRead < 4096) {
+            } else if (bytesRead < 4096) {
                 byte[] bytesToGuess = new byte[bytesRead];
                 System.arraycopy(bytes, 0, bytesToGuess, 0, bytesRead);
                 this.buffer = bytesToGuess;
-            }
-            else {
+            } else {
                 this.buffer = bytes;
             }
-        } finally {
-            try {input.close();} catch (IOException e){
-                // IGNORE
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/groovy/groovy/util/GroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/util/GroovyScriptEngine.java b/src/main/groovy/groovy/util/GroovyScriptEngine.java
index 1bc79b4..c9e0c5b 100644
--- a/src/main/groovy/groovy/util/GroovyScriptEngine.java
+++ b/src/main/groovy/groovy/util/GroovyScriptEngine.java
@@ -450,16 +450,7 @@ public class GroovyScriptEngine implements ResourceConnector {
     }
 
     private static void verifyInputStream(URLConnection urlConnection) throws IOException
{
-        InputStream in = null;
-        try {
-            in = urlConnection.getInputStream();
-        } finally {
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (IOException ignore) {
-                }
-            }
+        try (InputStream in = urlConnection.getInputStream()) {
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
index 8648a31..4a4f0ff 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
@@ -733,20 +733,10 @@ public class CompilationUnit extends ProcessingUnit {
             //
             byte[] bytes = gclass.getBytes();
 
-            FileOutputStream stream = null;
-            try {
-                stream = new FileOutputStream(path);
+            try (FileOutputStream stream = new FileOutputStream(path)) {
                 stream.write(bytes, 0, bytes.length);
             } catch (IOException e) {
                 getErrorCollector().addError(Message.create(e.getMessage(), CompilationUnit.this));
-            } finally {
-                if (stream != null) {
-                    try {
-                        stream.close();
-                    } catch (Exception e) {
-                        // Ignore
-                    }
-                }
             }
         }
     };

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/control/SourceExtensionHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/SourceExtensionHandler.java b/src/main/java/org/codehaus/groovy/control/SourceExtensionHandler.java
index 0d85ebe..0ca38f5 100644
--- a/src/main/java/org/codehaus/groovy/control/SourceExtensionHandler.java
+++ b/src/main/java/org/codehaus/groovy/control/SourceExtensionHandler.java
@@ -43,10 +43,8 @@ public class SourceExtensionHandler {
                 globalServices = loader.getResources("META-INF/services/org.codehaus.groovy.source.Extensions");
             }
             while (globalServices.hasMoreElements()) {
-                BufferedReader svcIn = null;
                 URL service = globalServices.nextElement();
-                try {
-                    svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service)));
+                try (BufferedReader svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service))))
{
                     String extension = svcIn.readLine();
                     while (extension != null) {
                         extension = extension.trim();
@@ -58,8 +56,6 @@ public class SourceExtensionHandler {
                 } catch (IOException ex) {
                     throw new GroovyRuntimeException("IO Exception attempting to load registered
source extension " +
                             service.toExternalForm() + ". Exception: " + ex.toString());
-                } finally {
-                    if (svcIn != null) svcIn.close();
                 }
             }
         } catch (IOException ex) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/runtime/WritableFile.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/WritableFile.java b/src/main/java/org/codehaus/groovy/runtime/WritableFile.java
index b4aef73..ca7b597 100644
--- a/src/main/java/org/codehaus/groovy/runtime/WritableFile.java
+++ b/src/main/java/org/codehaus/groovy/runtime/WritableFile.java
@@ -42,12 +42,10 @@ public class WritableFile extends File implements Writable {
     }
 
     public Writer writeTo(final Writer out) throws IOException {
-        final Reader reader =
-            (this.encoding == null)
-                ? ResourceGroovyMethods.newReader(this)
-                : ResourceGroovyMethods.newReader(this, this.encoding);
 
-        try {
+        try (Reader reader = (this.encoding == null)
+                ? ResourceGroovyMethods.newReader(this)
+                : ResourceGroovyMethods.newReader(this, this.encoding)) {
             int c = reader.read();
 
             while (c != -1) {
@@ -55,9 +53,6 @@ public class WritableFile extends File implements Writable {
                 c = reader.read();
             }
         }
-        finally {
-            reader.close();
-        }
         return out;
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/tools/LoaderConfiguration.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/tools/LoaderConfiguration.java b/src/main/java/org/codehaus/groovy/tools/LoaderConfiguration.java
index de0fb49..dcd1aa6 100644
--- a/src/main/java/org/codehaus/groovy/tools/LoaderConfiguration.java
+++ b/src/main/java/org/codehaus/groovy/tools/LoaderConfiguration.java
@@ -104,8 +104,7 @@ public class LoaderConfiguration {
      * @throws IOException if reading or parsing the contents of the stream fails
      */
     public void configure(InputStream is) throws IOException {
-        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
-        try {
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
             int lineNumber = 0;
 
             while (true) {
@@ -139,8 +138,6 @@ public class LoaderConfiguration {
                     throw new IOException("unexpected line in " + lineNumber + " : " + line);
                 }
             }
-        } finally {
-            reader.close();
         }
 
         if (requireMain && main == null) throw new IOException("missing main class
definition in config file");

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index 581e723..0c1acbd 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -117,18 +117,16 @@ public class JavaStubGenerator {
 
         File file = new File(outputPath, fileName + ".java");
         Charset charset = Charset.forName(encoding);
-        PrintWriter out =
-                new PrintWriter(
-                        new OutputStreamWriter(
-                                new BufferedOutputStream(
-                                        new FileOutputStream(file),
-                                        DEFAULT_BUFFER_SIZE
-                                ),
-                                charset
-                        )
-                );
 
-        try {
+        try (PrintWriter out = new PrintWriter(
+                new OutputStreamWriter(
+                        new BufferedOutputStream(
+                                new FileOutputStream(file),
+                                DEFAULT_BUFFER_SIZE
+                        ),
+                        charset
+                )
+        )) {
             String packageName = classNode.getPackageName();
             if (packageName != null) {
                 out.println("package " + packageName + ";\n");
@@ -137,12 +135,6 @@ public class JavaStubGenerator {
             printImports(out, classNode);
             printClassContents(out, classNode);
 
-        } finally {
-            try {
-                out.close();
-            } catch (Exception e) {
-                // ignore
-            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/6c615579/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
index 63819e9..1e6de12 100644
--- a/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/ASTTransformationVisitor.java
@@ -214,9 +214,7 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport
{
             while (globalServices.hasMoreElements()) {
                 URL service = globalServices.nextElement();
                 String className;
-                BufferedReader svcIn = null;
-                try {
-                    svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service),
"UTF-8"));
+                try (BufferedReader svcIn = new BufferedReader(new InputStreamReader(URLStreams.openUncachedStream(service),
"UTF-8"))) {
                     try {
                         className = svcIn.readLine();
                     } catch (IOException ioe) {
@@ -265,9 +263,6 @@ public final class ASTTransformationVisitor extends ClassCodeVisitorSupport
{
                             continue;
                         }
                     }
-                } finally {
-                    if (svcIn != null)
-                        svcIn.close();
                 }
             }
         } catch (IOException e) {


Mime
View raw message