groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-6764: Problem Referring To Statically Imported Constants (closes #404)
Date Thu, 01 Sep 2016 22:47:56 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 08af17911 -> 831550cc3


GROOVY-6764: Problem Referring To Statically Imported Constants (closes #404)


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 831550cc39dab18b986ec95c65885dadaf5fb197
Parents: 08af179
Author: paulk <paulk@asert.com.au>
Authored: Wed Aug 31 17:22:43 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Fri Sep 2 08:47:42 2016 +1000

----------------------------------------------------------------------
 .../groovy/control/StaticImportVisitor.java     | 24 +++++++++-
 src/test/groovy/bugs/Groovy6764Bug.groovy       | 48 ++++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/831550cc/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
index 2841d7a..e5b2e6f 100644
--- a/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -50,8 +50,10 @@ import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.Statement;
 import org.codehaus.groovy.syntax.Types;
 
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import static org.codehaus.groovy.runtime.MetaClassHelper.capitalize;
 
@@ -227,7 +229,7 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
                 ClassExpression ce = (ClassExpression) pe.getObjectExpression();
                 ClassNode type = ce.getType();
                 if (type.isEnum()) return exp;
-                Expression constant = findConstant(type.getField(pe.getPropertyAsString()));
+                Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
                 if (constant != null) return constant;
             }
         } else if (exp instanceof ListExpression) {
@@ -565,13 +567,31 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
 
     private static Expression findStaticField(ClassNode staticImportType, String fieldName)
{
         if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
-            FieldNode field = staticImportType.getField(fieldName);
+            FieldNode field = getField(staticImportType, fieldName);
             if (field != null && field.isStatic())
                 return new PropertyExpression(new ClassExpression(staticImportType), fieldName);
         }
         return null;
     }
 
+    private static FieldNode getField(ClassNode classNode, String fieldName) {
+        ClassNode node = classNode;
+        Set<String> visited = new HashSet<String>();
+        while (node != null) {
+            FieldNode fn = node.getDeclaredField(fieldName);
+            if (fn != null) return fn;
+            ClassNode[] interfaces = node.getInterfaces();
+            for (ClassNode iNode : interfaces) {
+                if (visited.contains(iNode.getName())) continue;
+                FieldNode ifn = getField(iNode, fieldName);
+                visited.add(iNode.getName());
+                if (ifn != null) return ifn;
+            }
+            node = node.getSuperClass();
+        }
+        return null;
+    }
+
     private static Expression findStaticMethod(ClassNode staticImportType, String methodName,
Expression args) {
         if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
             if (staticImportType.hasPossibleStaticMethod(methodName, args)) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/831550cc/src/test/groovy/bugs/Groovy6764Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy6764Bug.groovy b/src/test/groovy/bugs/Groovy6764Bug.groovy
new file mode 100644
index 0000000..c33c791
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6764Bug.groovy
@@ -0,0 +1,48 @@
+/*
+ *  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 Groovy6764Bug extends GroovyTestCase {
+    void testStaticImportViaInheritedInterface() {
+        assertScript '''
+            import static Constants.ANSWER as CA
+            import static Helper.ANSWER as HA
+
+            class Foo {
+                static method(arg1 = CA, arg2 = HA) {
+                    assert CA == 42
+                    assert HA == 42
+                    "$arg1 $arg2"
+                }
+                static Closure closure = { arg1 = CA, arg2 = HA ->
+                    assert CA == 42
+                    assert HA == 42
+                    "$arg1 $arg2"
+                }
+            }
+
+            interface ConstantsBase { int ANSWER = 42 }
+            interface Constants extends ConstantsBase {}
+            class Helper implements Constants { }
+
+            assert Foo.closure() == '42 42'
+            assert Foo.method() == '42 42'
+        '''
+    }
+}


Mime
View raw message