Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0C95B200B82 for ; Fri, 2 Sep 2016 00:48:18 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 08896160AB7; Thu, 1 Sep 2016 22:48:08 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 28F9B160AB5 for ; Fri, 2 Sep 2016 00:48:07 +0200 (CEST) Received: (qmail 70947 invoked by uid 500); 1 Sep 2016 22:47:56 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 70938 invoked by uid 99); 1 Sep 2016 22:47:56 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Sep 2016 22:47:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4030FDFD4C; Thu, 1 Sep 2016 22:47:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulk@apache.org To: commits@groovy.apache.org Message-Id: <9a5d675a96604526b9e494d7d1b319d0@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: groovy git commit: GROOVY-6764: Problem Referring To Statically Imported Constants (closes #404) Date: Thu, 1 Sep 2016 22:47:56 +0000 (UTC) archived-at: Thu, 01 Sep 2016 22:48:18 -0000 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 Authored: Wed Aug 31 17:22:43 2016 +1000 Committer: paulk 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 visited = new HashSet(); + 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' + ''' + } +}