groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 01/02: Add docs for try-with-resources
Date Wed, 15 Jan 2020 07:07:09 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 496312a20747fd779eca049aa264fdbb1d40685e
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Wed Jan 15 14:46:20 2020 +0800

    Add docs for try-with-resources
---
 src/spec/doc/core-semantics.adoc | 57 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/src/spec/doc/core-semantics.adoc b/src/spec/doc/core-semantics.adoc
index f6ac125..bfcc72c 100644
--- a/src/spec/doc/core-semantics.adoc
+++ b/src/spec/doc/core-semantics.adoc
@@ -288,6 +288,63 @@ try {
 }
 ----
 
+==== ARM Try with resources
+
+Groovy often provides better alternatives to Java 7's `try`-with-resources statement for
Automatic Resource Management (ARM).
+That syntax is now supported for Java programmers migrating to Groovy and still wanting to
use the old style:
+
+[source,groovy]
+--------------------------------------
+class FromResource extends ByteArrayInputStream {
+    @Override
+    void close() throws IOException {
+        super.close()
+        println "FromResource closing"
+    }
+
+    FromResource(String input) {
+        super(input.toLowerCase().bytes)
+    }
+}
+
+class ToResource extends ByteArrayOutputStream {
+    @Override
+    void close() throws IOException {
+        super.close()
+        println "ToResource closing"
+    }
+}
+
+def wrestle(s) {
+    try (
+            FromResource from = new FromResource(s)
+            ToResource to = new ToResource()
+    ) {
+        to << from
+        return to.toString()
+    }
+}
+
+def wrestle2(s) {
+    FromResource from = new FromResource(s)
+    try (from; ToResource to = new ToResource()) { // Enhanced try-with-resources in Java
9+
+        to << from
+        return to.toString()
+    }
+}
+
+assert wrestle("ARM was here!").contains('arm')
+assert wrestle2("ARM was here!").contains('arm')
+--------------------------------------
+Which yields the following output:
+--------------------------------------
+ToResource closing
+FromResource closing
+ToResource closing
+FromResource closing
+--------------------------------------
+
+
 === Power assertion
 
 Unlike Java with which Groovy shares the `assert` keyword, the latter in Groovy behaves very
differently. First of all,


Mime
View raw message