groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-8389: Static import of a property messes with instance method
Date Fri, 29 Jun 2018 17:40:34 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 0d4b52f53 -> ab33599c5


GROOVY-8389: Static import of a property messes with instance method


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

Branch: refs/heads/master
Commit: ab33599c5a1537027fb2953f8893a5eaa40d0054
Parents: 0d4b52f
Author: danielsun1106 <realbluesun@hotmail.com>
Authored: Sat Jun 30 01:40:14 2018 +0800
Committer: danielsun1106 <realbluesun@hotmail.com>
Committed: Sat Jun 30 01:41:04 2018 +0800

----------------------------------------------------------------------
 .../groovy/control/StaticImportVisitor.java     |  24 ++--
 src/test/groovy/bugs/Groovy8389Bug.groovy       | 109 +++++++++++++++++++
 2 files changed, 123 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ab33599c/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
index 86ff461..f6a130a 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -272,20 +272,24 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
 
         if (mce.isImplicitThis() || isExplicitThisOrSuper) {
             if (mce.isImplicitThis()) {
-                Expression ret = findStaticMethodImportFromModule(method, args);
-                if (ret != null) {
-                    setSourcePosition(ret, mce);
-                    return ret;
-                }
-                if (method instanceof ConstantExpression && !inLeftExpression) {
-                    // could be a closure field
-                    String methodName = (String) ((ConstantExpression) method).getValue();
-                    ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
+                if (!currentClass.hasPossibleMethod(mce.getMethodAsString(), args)
+                        && !currentClass.hasPossibleStaticMethod(mce.getMethodAsString(),
args)) {
+
+                    Expression ret = findStaticMethodImportFromModule(method, args);
                     if (ret != null) {
-                        ret = new MethodCallExpression(ret, "call", args);
                         setSourcePosition(ret, mce);
                         return ret;
                     }
+                    if (method instanceof ConstantExpression && !inLeftExpression)
{
+                        // could be a closure field
+                        String methodName = (String) ((ConstantExpression) method).getValue();
+                        ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
+                        if (ret != null) {
+                            ret = new MethodCallExpression(ret, "call", args);
+                            setSourcePosition(ret, mce);
+                            return ret;
+                        }
+                    }
                 }
             } else if (currentMethod!=null && currentMethod.isStatic() &&
isExplicitSuper) {
                 MethodCallExpression ret = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()),
method, args);

http://git-wip-us.apache.org/repos/asf/groovy/blob/ab33599c/src/test/groovy/bugs/Groovy8389Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8389Bug.groovy b/src/test/groovy/bugs/Groovy8389Bug.groovy
new file mode 100644
index 0000000..bf56fbd
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8389Bug.groovy
@@ -0,0 +1,109 @@
+/*
+ *  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
+
+class Groovy8389Bug extends GroovyTestCase {
+    void testLocalMethodInvoked() {
+        assertScript '''
+            import static A.bar
+            
+            class A {
+              static bar = "A"
+            }
+            
+            def bar() {
+              "bar"
+            }
+            
+            @groovy.transform.CompileStatic
+            def usage() {
+              bar()
+            }
+            
+            assert "bar" == usage() 
+        '''
+    }
+
+    void testLocalMethodInvoked2() {
+        assertScript '''
+            import static A.bar
+            
+            class A {
+              static bar = "A"
+            }
+            
+            static bar() {
+              "bar"
+            }
+            
+            @groovy.transform.CompileStatic
+            def usage() {
+              bar()
+            }
+            
+            assert "bar" == usage() 
+        '''
+    }
+
+    void testLocalMethodInvoked3() {
+        assertScript '''
+            import static A.baz
+            import static A.bar
+            
+            class A {
+              static bar() { "Abar" }
+              static baz() { "Abaz" }
+            }
+            
+            def bar() {
+              "bar"
+            }
+            
+            @groovy.transform.CompileStatic
+            def usage() {
+              "${bar()}${baz()}"
+            }
+            
+            assert usage() == 'barAbaz'
+        '''
+    }
+
+    void testLocalMethodInvoked4() {
+        assertScript '''
+            import static A.baz
+            import static A.bar
+            
+            class A {
+              static bar() { "Abar" }
+              static baz() { "Abaz" }
+            }
+            
+            static bar() {
+              "bar"
+            }
+            
+            @groovy.transform.CompileStatic
+            def usage() {
+              "${bar()}${baz()}"
+            }
+            
+            assert usage() == 'barAbaz'
+        '''
+    }
+}


Mime
View raw message