You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/11/02 06:46:30 UTC

groovy git commit: GROOVY-8864: Backwards compatibility of trait generic behavior for static methods

Repository: groovy
Updated Branches:
  refs/heads/master c231e3187 -> c36ddc969


GROOVY-8864: Backwards compatibility of trait generic behavior for static methods


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

Branch: refs/heads/master
Commit: c36ddc96943f84d53fe12006f98c8e498f03b007
Parents: c231e31
Author: Paul King <pa...@asert.com.au>
Authored: Fri Nov 2 16:46:05 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Fri Nov 2 16:46:22 2018 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/control/ResolveVisitor.java |  4 +--
 .../transform/trait/TraitASTTransformation.java |  2 +-
 src/test/groovy/bugs/Groovy8864Bug.groovy       | 38 ++++++++++++++++++++
 3 files changed, 41 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/c36ddc96/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 5492d1a..aabcaf3 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -231,7 +231,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         VariableScope oldScope = currentScope;
         currentScope = node.getVariableScope();
         Map<GenericsTypeName, GenericsType> oldPNames = genericParameterNames;
-        genericParameterNames = node.isStatic()
+        genericParameterNames = node.isStatic() && !Traits.isTrait(node.getDeclaringClass())
                 ? new HashMap<GenericsTypeName, GenericsType>()
                 : new HashMap<GenericsTypeName, GenericsType>(genericParameterNames);
 
@@ -268,7 +268,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
 
     public void visitProperty(PropertyNode node) {
         Map<GenericsTypeName, GenericsType> oldPNames = genericParameterNames;
-        if (node.isStatic()) {
+        if (node.isStatic() && !Traits.isTrait(node.getDeclaringClass())) {
             genericParameterNames = new HashMap<GenericsTypeName, GenericsType>();
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/c36ddc96/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
index 6eeecdc..02c0f08 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/TraitASTTransformation.java
@@ -441,7 +441,7 @@ public class TraitASTTransformation extends AbstractASTTransformation implements
 
         Expression initialExpression = field.getInitialExpression();
         MethodNode selectedMethod = field.isStatic()?staticInitializer:initializer;
-        ClassNode target = fieldHelper;
+        ClassNode target = field.isStatic() && staticFieldHelper != null ? staticFieldHelper : fieldHelper;
         if (initialExpression != null) {
             VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
             ExpressionStatement initCode = new ExpressionStatement(initialExpression);

http://git-wip-us.apache.org/repos/asf/groovy/blob/c36ddc96/src/test/groovy/bugs/Groovy8864Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8864Bug.groovy b/src/test/groovy/bugs/Groovy8864Bug.groovy
new file mode 100644
index 0000000..2c84055
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8864Bug.groovy
@@ -0,0 +1,38 @@
+/*
+ *  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 Groovy8864Bug extends GroovyTestCase {
+    void testGenericsAppliedToStaticMethodsForTraits() {
+        assertScript '''
+            trait Foo<T> {
+                static T INSTANCE
+                static T get(T arg) { 
+                    null
+               }
+            }
+
+            class Bar implements Foo<Bar> {}
+            assert Bar.getMethod("get", [Bar] as Class[]).returnType == Bar
+            assert Bar.getDeclaredField("Foo__INSTANCE").type == Bar
+            assert Bar.getMethod("setINSTANCE", [Bar] as Class[])
+            assert Bar.getMethod("getINSTANCE").returnType == Bar
+        '''
+    }
+}