You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/04/17 02:23:17 UTC

groovy git commit: GROOVY-8546: Parrot Parser: multiple Reader instances opened from SourceUnit; many left open

Repository: groovy
Updated Branches:
  refs/heads/master c15b4c55d -> e19a93242


GROOVY-8546: Parrot Parser: multiple Reader instances opened from SourceUnit; many left open


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

Branch: refs/heads/master
Commit: e19a93242dcc204cd94e034242a348c113cc1130
Parents: c15b4c5
Author: sunlan <su...@apache.org>
Authored: Tue Apr 17 10:22:09 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue Apr 17 10:22:09 2018 +0800

----------------------------------------------------------------------
 .../parser/antlr4/Antlr4ParserPlugin.java       | 15 ++++++----
 .../parser/antlr4/GroovyParserTest.groovy       | 29 ++++++++++++++++++--
 2 files changed, 36 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/e19a9324/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
index 25131bf..0f35f1d 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
@@ -30,6 +30,7 @@ import org.codehaus.groovy.syntax.ParserException;
 import org.codehaus.groovy.syntax.Reduction;
 
 import java.io.IOException;
+import java.io.Reader;
 
 /**
  * A parser plugin for the new parser
@@ -42,9 +43,10 @@ public class Antlr4ParserPlugin implements ParserPlugin {
 
     @Override
     public Reduction parseCST(SourceUnit sourceUnit, java.io.Reader reader) throws CompilationFailedException {
-        try {
-            ReaderSource readerSource = sourceUnit.getSource();
-            if (null != readerSource && null != readerSource.getReader()) {
+        ReaderSource readerSource = sourceUnit.getSource();
+
+        try (Reader sourceReader = null != readerSource ? readerSource.getReader() : null) {
+            if (null != readerSource && null != sourceReader) {
                 this.readerSource = readerSource;
             } else {
                 this.readerSource = new StringReaderSource(IOGroovyMethods.getText(reader), sourceUnit.getConfiguration());
@@ -58,9 +60,10 @@ public class Antlr4ParserPlugin implements ParserPlugin {
 
     @Override
     public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
-        try {
-            ReaderSource readerSource = sourceUnit.getSource();
-            if (null == readerSource || null == readerSource.getReader()) {
+        ReaderSource readerSource = sourceUnit.getSource();
+
+        try (Reader sourceReader = null != readerSource ? readerSource.getReader() : null) {
+            if (null == readerSource || null == sourceReader) {
                 sourceUnit.setSource(this.readerSource);
             }
         } catch (IOException e) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/e19a9324/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 8dae27e..1602fa7 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -27,9 +27,9 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
 import org.codehaus.groovy.ast.stmt.ExpressionStatement
 import org.codehaus.groovy.syntax.Token
 
-import static org.apache.groovy.parser.antlr4.TestUtils.doTest
-import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTestAntlr4
 import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTest
+import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTestAntlr4
+import static org.apache.groovy.parser.antlr4.TestUtils.doTest
 
 /**
  * Some basic test cases for the new parser
@@ -45,6 +45,31 @@ class GroovyParserTest extends GroovyTestCase {
         doTestAttachedComments()
     }
 
+    void "test IO stream/reader closed by the parser properly"() {
+        def f = File.createTempFile("Script${System.nanoTime()}", ".groovy")
+        f.text = '''
+            def a = 123
+        '''
+
+        def antlr4Parser = new org.apache.groovy.parser.Antlr4Parser()
+        antlr4Parser.parse(f)
+
+        boolean deleted = f.delete()
+        assert deleted: "Failed to delete file: ${f.getAbsolutePath()}"
+    }
+
+    void "test IO stream/reader closed by the compiler properly"() {
+        def f = File.createTempFile("Script${System.nanoTime()}", ".groovy")
+        f.text = '''
+            def a = 123
+        '''
+
+        TestUtils.createAntlr4Shell().evaluate(f)
+
+        boolean deleted = f.delete()
+        assert deleted: "Failed to delete file: ${f.getAbsolutePath()}"
+    }
+
     private static doTestAttachedComments() {
         def (newAST, oldAST) = doTest('core/Comments_02.groovy')
         List<ClassNode> classes = new ArrayList<>(newAST.classes).sort { c1, c2 -> c1.name <=> c2.name }