Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 983486b5a -> bec0f4c90


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/bec0f4c9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/bec0f4c9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/bec0f4c9

Branch: refs/heads/GROOVY_2_5_X
Commit: bec0f4c902acd4fb9ac65e49197debc65d3b4abb
Parents: 7c72a32
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:47:36 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/bec0f4c9/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 4133634..80f226d 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/bec0f4c9/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/bec0f4c9/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
+        '''
+    }
+}

Reply via email to