groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 07/17: GROOVY-9294: check for array length on each receiver
Date Wed, 30 Oct 2019 15:55:31 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit ceb2462e7ec7166aace8bbbefdbb1a88f2d1ef88
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Tue Oct 29 12:00:23 2019 -0500

    GROOVY-9294: check for array length on each receiver
    
    (cherry picked from commit dfdd326f004680aa74e3e52538c2cb0e8f4a8b70)
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 19 +++++-----
 src/test/groovy/bugs/Groovy9294.groovy             | 43 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 92c3c07..db68405 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1544,15 +1544,6 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
             }
         }
 
-        if (objectExpressionType.isArray() && "length".equals(pexp.getPropertyAsString()))
{
-            storeType(pexp, int_TYPE);
-            if (visitor != null) {
-                PropertyNode node = new PropertyNode("length", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
int_TYPE, objectExpressionType, null, null, null);
-                visitor.visitProperty(node);
-            }
-            return true;
-        }
-
         boolean foundGetterOrSetter = false;
         List<Receiver<String>> receivers = new LinkedList<>();
         List<Receiver<String>> owners = makeOwnerList(objectExpression);
@@ -1563,6 +1554,16 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
         HashSet<ClassNode> handledNodes = new HashSet<>();
         for (Receiver<String> receiver : receivers) {
             ClassNode testClass = receiver.getType();
+
+            if (testClass.isArray() && "length".equals(propertyName)) {
+                storeType(pexp, int_TYPE);
+                if (visitor != null) {
+                    PropertyNode length = new PropertyNode("length", Opcodes.ACC_PUBLIC |
Opcodes.ACC_FINAL, int_TYPE, testClass, null, null, null);
+                    visitor.visitProperty(length);
+                }
+                return true;
+            }
+
             LinkedList<ClassNode> queue = new LinkedList<>();
             queue.add(testClass);
             if (isPrimitiveType(testClass)) {
diff --git a/src/test/groovy/bugs/Groovy9294.groovy b/src/test/groovy/bugs/Groovy9294.groovy
new file mode 100644
index 0000000..40e2c1c
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9294.groovy
@@ -0,0 +1,43 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy9294 {
+
+    @Test
+    void testArrayLengthOfDelegate() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            def test(String[] strings) {
+                strings.with {
+                    return length
+                }
+            }
+
+            assert test(new String[0]) == 0
+            assert test(['one','two'] as String[]) == 2
+        '''
+    }
+}


Mime
View raw message