You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/18 22:11:16 UTC

[groovy] 04/05: GROOVY-5364: find static members from static script methods

This is an automated email from the ASF dual-hosted git repository.

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

commit c44841b4255274d3574906e40b1ef3782002517d
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Feb 18 15:56:54 2022 -0600

    GROOVY-5364: find static members from static script methods
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
---
 .../groovy/classgen/VariableScopeVisitor.java      |   4 -
 src/test/groovy/bugs/Groovy5364.groovy             | 104 +++++++++++++++++++++
 2 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 6774500..5c8a0b7 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -168,10 +168,6 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         final boolean abstractType = node.isAbstract();
 
         for (ClassNode cn = node; cn != null && !cn.equals(ClassHelper.OBJECT_TYPE); cn = cn.getSuperClass()) {
-            if (cn.isScript()) {
-                return new DynamicVariable(name, false);
-            }
-
             for (FieldNode fn : cn.getFields()) {
                 if (name.equals(fn.getName())) return fn;
             }
diff --git a/src/test/groovy/bugs/Groovy5364.groovy b/src/test/groovy/bugs/Groovy5364.groovy
new file mode 100644
index 0000000..5ab849e
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy5364.groovy
@@ -0,0 +1,104 @@
+/*
+ *  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 org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy5364 {
+
+    @Test
+    void testStaticScriptMethodAsProperty1() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            assert 'x' == getStaticProperty()
+            assert 'x' == staticProperty
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty2() {
+        assertScript '''
+            static getSomething() { 'x' }
+            something = 'y' // script var
+
+            assert 'x' == getSomething()
+            assert 'y' == something
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty3() {
+        new GroovyShell(new Binding(something: 'y')).evaluate '''
+            static getSomething() { 'x' }
+
+            assert 'x' == getSomething()
+            assert 'y' == something
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty4() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            void test() {
+                assert 'x' == getStaticProperty()
+                assert 'x' == staticProperty
+            }
+            test()
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty5() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            static void test() {
+                assert 'x' == getStaticProperty()
+                assert 'x' == staticProperty // Apparent variable 'staticProperty' was found in a static scope but doesn't refer to a local variable, static field or class
+            }
+            test()
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty6() {
+        def err = shouldFail '''
+            def getNonStaticProperty() {
+                'x'
+            }
+
+            static void test() {
+                nonStaticProperty
+            }
+        '''
+        assert err =~ /Apparent variable 'nonStaticProperty' was found in a static scope but doesn't refer to a local variable, static field or class/
+    }
+}