You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/08/08 16:57:58 UTC

[groovy] branch GROOVY-9672 updated (d2d1c70 -> d93f75f)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY-9672
in repository https://gitbox.apache.org/repos/asf/groovy.git.


 discard d2d1c70  GROOVY-9672: replace "this" argument with "Type" for static super call
     new d93f75f  GROOVY-9672: replace "this" argument with "Type" for static super call

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (d2d1c70)
            \
             N -- N -- N   refs/heads/GROOVY-9672 (d93f75f)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovy/transform/trait/SuperCallTraitTransformer.java      | 10 +++++-----
 .../groovy/transform/traitx/TraitASTTransformationTest.groovy  |  6 +++---
 2 files changed, 8 insertions(+), 8 deletions(-)


[groovy] 01/01: GROOVY-9672: replace "this" argument with "Type" for static super call

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9672
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit d93f75f0a7a4738407e5d5de6f17aa9ad414127e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Aug 8 11:08:49 2020 -0500

    GROOVY-9672: replace "this" argument with "Type" for static super call
---
 .../transform/trait/SuperCallTraitTransformer.java |  32 +++--
 .../traitx/TraitASTTransformationTest.groovy       | 141 +++++++++++++++++++++
 2 files changed, 165 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/trait/SuperCallTraitTransformer.java b/src/main/java/org/codehaus/groovy/transform/trait/SuperCallTraitTransformer.java
index e8f1c35..8e9193d 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/SuperCallTraitTransformer.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/SuperCallTraitTransformer.java
@@ -33,13 +33,15 @@ import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
 import org.codehaus.groovy.ast.expr.TupleExpression;
-import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.tools.ClosureUtils;
 import org.codehaus.groovy.control.SourceUnit;
 import org.codehaus.groovy.syntax.Types;
 
+import java.util.List;
 import java.util.function.Function;
 
+import static org.codehaus.groovy.ast.tools.GeneralUtils.thisPropX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
 import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
 import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
 import static org.objectweb.asm.Opcodes.ACC_STATIC;
@@ -100,12 +102,13 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
                     String setterName = MetaProperty.getSetterName(leftExpression.getPropertyAsString());
                     for (MethodNode method : helperType.getMethods(setterName)) {
                         Parameter[] parameters = method.getParameters();
-                        if (parameters.length == 2 && parameters[0].getType().equals(traitType)) {
+                        if (parameters.length == 2 && isSelfType(parameters[0], traitType)) {
                             MethodCallExpression setterCall = new MethodCallExpression(
                                     new ClassExpression(helperType),
                                     setterName,
                                     new ArgumentListExpression(
-                                            new VariableExpression("this"),
+                                            parameters[0].getType().equals(ClassHelper.CLASS_Type)
+                                                ? thisPropX(false, "class") : varX("this"),
                                             bin.getRightExpression()
                                     )
                             );
@@ -143,7 +146,8 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
                             new ClassExpression(helperType),
                             methodNode.getName(),
                             new ArgumentListExpression(
-                                    new VariableExpression("this")
+                                    methodNode.getParameters()[0].getType().equals(ClassHelper.CLASS_Type)
+                                        ? thisPropX(false, "class") : varX("this")
                             )
                     );
                     methodCall.getObjectExpression().setSourcePosition(((PropertyExpression) exp.getObjectExpression()).getObjectExpression());
@@ -157,7 +161,7 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
                 String getterName = MetaProperty.getGetterName(exp.getPropertyAsString(), null);
                 for (MethodNode method : helperType.getMethods(getterName)) {
                     if (method.isStatic() && method.getParameters().length == 1
-                            && method.getParameters()[0].getType().equals(traitType)
+                            && isSelfType(method.getParameters()[0], traitType)
                             && !method.getReturnType().equals(ClassHelper.VOID_TYPE)) {
                         return xform.apply(method);
                     }
@@ -166,7 +170,7 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
                 String isserName = "is" + getterName.substring(3);
                 for (MethodNode method : helperType.getMethods(isserName)) {
                     if (method.isStatic() && method.getParameters().length == 1
-                            && method.getParameters()[0].getType().equals(traitType)
+                            && isSelfType(method.getParameters()[0], traitType)
                             && method.getReturnType().equals(ClassHelper.boolean_TYPE)) {
                         return xform.apply(method);
                     }
@@ -183,8 +187,12 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
             ClassNode helperType = getHelper(traitType);
             // TraitType.super.foo() -> TraitType$Trait$Helper.foo(this)
 
-            ArgumentListExpression newArgs = new ArgumentListExpression();
-            newArgs.addExpression(new VariableExpression("this"));
+            List<MethodNode> targets = helperType.getMethods(exp.getMethodAsString());
+            boolean isStatic = !targets.isEmpty() && targets.stream().map(MethodNode::getParameters)
+                .allMatch(params -> params.length > 0 && params[0].getType().equals(ClassHelper.CLASS_Type));
+
+            ArgumentListExpression newArgs = new ArgumentListExpression(
+                    isStatic ? thisPropX(false, "class") : varX("this"));
             Expression arguments = exp.getArguments();
             if (arguments instanceof TupleExpression) {
                 for (Expression expression : (TupleExpression) arguments) {
@@ -240,4 +248,12 @@ class SuperCallTraitTransformer extends ClassCodeExpressionTransformer {
         }
         return null;
     }
+
+    private static boolean isSelfType(final Parameter parameter, final ClassNode traitType) {
+        ClassNode paramType = parameter.getType();
+        if (paramType.equals(traitType)) return true;
+        return paramType.equals(ClassHelper.CLASS_Type)
+                && paramType.getGenericsTypes() != null
+                && paramType.getGenericsTypes()[0].getType().equals(traitType);
+    }
 }
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index 45a91b1..e785703 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -1066,6 +1066,72 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-9672
+    void testTraitSuperPropertyGetStatic() {
+        assertScript '''
+            trait T {
+                static x = 'value'
+            }
+            class C implements T {
+                def test() {
+                    T.super.x
+                }
+            }
+            assert new C().test() == 'value'
+        '''
+
+        assertScript '''
+            trait T {
+                static boolean x = true
+            }
+            class C implements T {
+                def test() {
+                    T.super.x
+                }
+            }
+            assert new C().test() == true
+        '''
+
+        assertScript '''
+            trait T {
+                static getX() { 'value' }
+            }
+            class C implements T {
+                def test() {
+                    T.super.x
+                }
+            }
+            assert new C().test() == 'value'
+        '''
+
+        assertScript '''
+            trait T {
+                static boolean isX() { true }
+            }
+            class C implements T {
+                def test() {
+                    T.super.x
+                }
+            }
+            assert new C().test() == true
+        '''
+
+        assertScript '''
+            trait A {
+                static getX() { 'A' }
+            }
+            trait B {
+                static getX() { 'B' }
+            }
+            class C implements A, B {
+                def test() {
+                    A.super.x + B.super.x
+                }
+            }
+            assert new C().test() == 'AB'
+        '''
+    }
+
     @Test
     void testTraitSuperPropertySet() {
         assertScript '''
@@ -1123,6 +1189,63 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-9672
+    void testTraitSuperPropertySetStatic() {
+        assertScript '''
+            trait T {
+                static x
+            }
+            class C implements T {
+                def test() {
+                    T.super.x = 'value'
+                    return x
+                }
+            }
+            assert new C().test() == 'value'
+        '''
+
+        def err = shouldFail '''
+            trait T {
+                static final x = 'const'
+            }
+            class C implements T {
+                def test() {
+                    T.super.x = 'value'
+                    return x
+                }
+            }
+            assert new C().test() == 'value'
+        '''
+        assert err =~ /No such property: super for class: T/
+
+        assertScript '''
+            trait T {
+                static setX(value) { 'retval' }
+            }
+            class C implements T {
+                def test() {
+                    T.super.x = 'value'
+                }
+            }
+            assert new C().test() == 'retval'
+        '''
+
+        assertScript '''
+            trait A {
+                static setX(value) { 'A' }
+            }
+            trait B {
+                static setX(value) { 'B' }
+            }
+            class C implements A, B {
+                def test() {
+                    (A.super.x = 'a') + (B.super.x = 'b')
+                }
+            }
+            assert new C().test() == 'AB'
+        '''
+    }
+
     @Test // GROOVY-9673
     void testTraitSuperPropertySetWithOverloads() {
         assertScript '''
@@ -1160,6 +1283,24 @@ final class TraitASTTransformationTest {
         '''
     }
 
+    @Test // GROOVY-9672
+    void testTraitSuperCallStatic() {
+        assertScript '''
+            trait A {
+                static m() { 'A' }
+            }
+            trait B {
+                static m() { 'B' }
+            }
+            class C implements A, B {
+                def test() {
+                    m() + A.super.m() + B.super.m()
+                }
+            }
+            assert new C().test() == 'BAB'
+        '''
+    }
+
     @Test
     void testTraitSuperCallWhenExtendingAnotherTrait() {
         assertScript '''