You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/09/13 02:39:00 UTC

incubator-groovy git commit: GROOVY-7572: Add withAutoClosable method (closes #103)

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 0a1e71cb7 -> 1eda6ed05


GROOVY-7572: Add withAutoClosable method (closes #103)


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

Branch: refs/heads/master
Commit: 1eda6ed050a3131ed56f0fe92f44559a6276a8dc
Parents: 0a1e71c
Author: Dominik Przybysz <dp...@touk.pl>
Authored: Wed Aug 26 23:22:21 2015 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sun Sep 13 02:36:30 2015 +0200

----------------------------------------------------------------------
 .../groovy/runtime/NioGroovyMethods.java        | 45 ++++++++++++++++++++
 .../groovy/runtime/NioGroovyMethodsTest.groovy  | 26 +++++++++++
 2 files changed, 71 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/1eda6ed0/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java b/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
index 8c9271f..66f8f8a 100644
--- a/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
+++ b/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java
@@ -50,6 +50,7 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Logger;
 import java.util.regex.Pattern;
 
 import groovy.io.FileType;
@@ -59,6 +60,7 @@ import groovy.lang.Closure;
 import groovy.lang.MetaClass;
 import groovy.lang.Writable;
 import groovy.transform.stc.ClosureParams;
+import groovy.transform.stc.FirstParam;
 import groovy.transform.stc.FromString;
 import groovy.transform.stc.PickFirstResolver;
 import groovy.transform.stc.SimpleType;
@@ -111,6 +113,8 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 public class NioGroovyMethods extends DefaultGroovyMethodsSupport {
 
+    private static final Logger LOG = Logger.getLogger(NioGroovyMethods.class.getName());
+
     /**
      * Provide the standard Groovy <code>size()</code> method for <code>Path</code>.
      *
@@ -1754,4 +1758,45 @@ public class NioGroovyMethods extends DefaultGroovyMethodsSupport {
     public static <T> T withCloseable(Closeable self, @ClosureParams(value = SimpleType.class, options = "java.io.Closeable") Closure<T> action) throws IOException {
         return IOGroovyMethods.withCloseable(self, action);
     }
+
+    /**
+     * Allows this autocloseable to be used within the closure, ensuring that it
+     * is closed once the closure has been executed and before this method returns.
+     *
+     * @param self the AutoCloseable
+     * @param action the closure taking the AutoCloseable as parameter
+     * @return the value returned by the closure
+     * @throws Exception if an Exception occurs.
+     * @since 2.5.0
+     */
+    public static <T, U extends AutoCloseable> T withAutoCloseable(U self, @ClosureParams(value=FirstParam.class) Closure<T> action) throws Exception {
+        try {
+            T result = action.call(self);
+
+            AutoCloseable temp = self;
+            self = null;
+            temp.close();
+
+            return result;
+        } finally {
+            closeWithWarning(self);
+        }
+    }
+
+    /**
+     * Close the AutoCloseable. Logging a warning if any problems occur.
+     *
+     * @param c the thing to close
+     */
+    public static void closeWithWarning(AutoCloseable c) {
+        if (c != null) {
+            try {
+                c.close();
+            } catch (Exception e) {
+                LOG.warning("Caught exception during close(): " + e);
+            }
+        }
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/1eda6ed0/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy b/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
index 7ec7d36..0831163 100644
--- a/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
+++ b/subprojects/groovy-nio/src/test/groovy/org/codehaus/groovy/runtime/NioGroovyMethodsTest.groovy
@@ -380,6 +380,23 @@ class NioGroovyMethodsTest extends Specification {
         closeable.closed
     }
 
+    def testWithAutoCloseable() {
+        setup:
+            def closeable = new DummyAutoCloseable()
+
+        when:
+            def closeableParam = null
+            def result = closeable.withAutoCloseable {
+                closeableParam = it
+                123
+            }
+
+        then:
+            closeableParam == closeable
+            result == 123
+            closeable.closed
+    }
+
     def testWithCloseableAndException() {
         setup:
         def closeable = new ExceptionDummyCloseable()
@@ -401,6 +418,15 @@ class DummyCloseable implements Closeable {
     }
 }
 
+class DummyAutoCloseable implements AutoCloseable {
+    boolean closed = false
+
+    @Override
+    void close() throws IOException {
+        closed = true
+    }
+}
+
 class ExceptionDummyCloseable implements Closeable {
     @Override
     void close() throws IOException {