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 2022/01/30 19:27:28 UTC

[groovy] branch GROOVY_3_0_X updated (7d21ea5 -> 098937b)

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

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


    from 7d21ea5  GROOVY-10184: fix for NPE
     new 9527d16  GROOVY-10234: handle raw types in class signature without looping
     new 098937b  GROOVY-10236: add test case

The 2 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/ast/decompiled/DecompiledClassNode.java | 16 ++++----
 .../groovy/ast/decompiled/TypeSignatureParser.java | 43 ++++++++++++++--------
 .../bugs/{Groovy9566.groovy => Groovy10236.groovy} |  8 ++--
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 40 ++++++++++++++++++++
 4 files changed, 81 insertions(+), 26 deletions(-)
 copy src/test/groovy/bugs/{Groovy9566.groovy => Groovy10236.groovy} (81%)

[groovy] 02/02: GROOVY-10236: add test case

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

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

commit 098937b33d4583a25eb5f54fa529ab716a02aa08
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Sep 16 10:30:40 2021 +0800

    GROOVY-10236: add test case
---
 src/test/groovy/bugs/Groovy10236.groovy | 34 +++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/src/test/groovy/bugs/Groovy10236.groovy b/src/test/groovy/bugs/Groovy10236.groovy
new file mode 100644
index 0000000..07ee797
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10236.groovy
@@ -0,0 +1,34 @@
+/*
+ *  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
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy10236 {
+    @Test
+    void testOmittingParenthesesInLambdaBody() {
+        assertScript '''
+            def same(obj) { obj }
+            def list = [1,2,3].stream().map(num -> same "$num").toList()
+            assert list == ['1','2','3']
+        '''
+    }
+}

[groovy] 01/02: GROOVY-10234: handle raw types in class signature without looping

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

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

commit 9527d16452b8c08bd656c01650d1e163fde99b5a
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Oct 1 12:01:44 2021 -0500

    GROOVY-10234: handle raw types in class signature without looping
    
    interface ConversionService<Impl extends ConversionService>
    
    https://github.com/micronaut-projects/micronaut-core/blob/3.0.x/core/src/main/java/io/micronaut/core/convert/ConversionService.java
---
 .../groovy/ast/decompiled/DecompiledClassNode.java | 16 ++++----
 .../groovy/ast/decompiled/TypeSignatureParser.java | 43 ++++++++++++++--------
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 40 ++++++++++++++++++++
 3 files changed, 76 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java b/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
index c53029f..232e70f 100644
--- a/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/decompiled/DecompiledClassNode.java
@@ -44,9 +44,9 @@ public class DecompiledClassNode extends ClassNode {
     private volatile boolean supersInitialized;
     private volatile boolean membersInitialized;
 
-    public DecompiledClassNode(ClassStub data, AsmReferenceResolver resolver) {
-        super(data.className, getFullModifiers(data), null, null, MixinNode.EMPTY_ARRAY);
-        classData = data;
+    public DecompiledClassNode(final ClassStub classData, final AsmReferenceResolver resolver) {
+        super(classData.className, getFullModifiers(classData), null, null, MixinNode.EMPTY_ARRAY);
+        this.classData = classData;
         this.resolver = resolver;
         isPrimaryNode = false;
     }
@@ -167,6 +167,10 @@ public class DecompiledClassNode extends ClassNode {
         throw new UnsupportedOperationException();
     }
 
+    public boolean isParameterized() {
+        return (classData.signature != null && classData.signature.charAt(0) == '<');
+    }
+
     @Override
     public boolean isResolved() {
         return true;
@@ -182,12 +186,11 @@ public class DecompiledClassNode extends ClassNode {
 
         synchronized (lazyInitLock) {
             if (!supersInitialized) {
-                ClassSignatureParser.configureClass(this, this.classData, this.resolver);
+                ClassSignatureParser.configureClass(this, classData, resolver);
                 addAnnotations(classData, this);
                 supersInitialized = true;
             }
         }
-
     }
 
     private void lazyInitMembers() {
@@ -262,5 +265,4 @@ public class DecompiledClassNode extends ClassNode {
         }
         return node;
     }
-
-}
\ No newline at end of file
+}
diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/TypeSignatureParser.java b/src/main/java/org/codehaus/groovy/ast/decompiled/TypeSignatureParser.java
index 691d7e8..43cc00e 100644
--- a/src/main/java/org/codehaus/groovy/ast/decompiled/TypeSignatureParser.java
+++ b/src/main/java/org/codehaus/groovy/ast/decompiled/TypeSignatureParser.java
@@ -58,7 +58,7 @@ abstract class TypeSignatureParser extends SignatureVisitor {
         final TypeSignatureParser outer = this;
         return new TypeSignatureParser(resolver) {
             @Override
-            void finished(ClassNode result) {
+            void finished(final ClassNode result) {
                 outer.finished(result.makeArray());
             }
         };
@@ -91,14 +91,6 @@ abstract class TypeSignatureParser extends SignatureVisitor {
         };
     }
 
-    private static GenericsType createWildcard(final ClassNode[] upper, final ClassNode lower) {
-        ClassNode base = ClassHelper.makeWithoutCaching("?");
-        base.setRedirect(ClassHelper.OBJECT_TYPE);
-        GenericsType t = new GenericsType(base, upper, lower);
-        t.setWildcard(true);
-        return t;
-    }
-
     @Override
     public void visitInnerClassType(final String name) {
         baseName += "$" + name;
@@ -107,14 +99,33 @@ abstract class TypeSignatureParser extends SignatureVisitor {
 
     @Override
     public void visitEnd() {
-        ClassNode base = resolver.resolveClass(baseName);
-        if (arguments.isEmpty()) {
-            finished(base);
-            return;
+        ClassNode baseType = resolver.resolveClass(baseName);
+        if (arguments.isEmpty() && isNotParameterized(baseType)) {
+            finished(baseType);
+        } else {
+            ClassNode parameterizedType = baseType.getPlainNodeReference();
+            if (!arguments.isEmpty()) { // else GROOVY-10234: no type arguments -> raw type
+                parameterizedType.setGenericsTypes(arguments.toArray(GenericsType.EMPTY_ARRAY));
+            }
+            finished(parameterizedType);
         }
+    }
 
-        ClassNode bound = base.getPlainNodeReference();
-        bound.setGenericsTypes(arguments.toArray(GenericsType.EMPTY_ARRAY));
-        finished(bound);
+    //--------------------------------------------------------------------------
+
+    private static GenericsType createWildcard(final ClassNode[] upper, final ClassNode lower) {
+        ClassNode base = ClassHelper.makeWithoutCaching("?");
+        base.setRedirect(ClassHelper.OBJECT_TYPE);
+        GenericsType t = new GenericsType(base, upper, lower);
+        t.setWildcard(true);
+        return t;
+    }
+
+    private static boolean isNotParameterized(final ClassNode cn) {
+        // DecompiledClassNode may not have generics initialized
+        if (cn instanceof DecompiledClassNode) {
+            return !((DecompiledClassNode) cn).isParameterized();
+        }
+        return (cn.getGenericsTypes() == null);
     }
 }
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 03f9aa5..ea04949 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -2743,6 +2743,46 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         }
     }
 
+    // GROOVY-10234
+    void testSelfReferentialTypeParameter() {
+        config.with {
+            targetDirectory = File.createTempDir()
+            jointCompilationOptions = [memStub: true]
+        }
+        File parentDir = File.createTempDir()
+        try {
+            def a = new File(parentDir, 'Main.groovy')
+            a.write '''
+                def <T> T getBean(Class<T> beanType) {
+                    { obj, Class target -> Optional.of(obj.toString()) } as Service
+                }
+
+                def result = getBean(Service).convert(new ArrayList(), String)
+                assert result.get() == '[]'
+            '''
+            def b = new File(parentDir, 'Service.java')
+            b.write '''
+                import java.util.Optional;
+                import java.util.function.Function;
+
+                public interface Service<Impl extends Service> {
+                    <T> Optional<T> convert(Object object, Class<T> targetType);
+                    <S, T> Impl addConverter(Class<S> sourceType, Class<T> targetType, Function<S, T> typeConverter);
+                }
+            '''
+
+            def loader = new GroovyClassLoader(this.class.classLoader)
+            def cu = new JavaAwareCompilationUnit(config, loader)
+            cu.addSources(a, b)
+            cu.compile()
+
+            loader.loadClass('Main').main()
+        } finally {
+            parentDir.deleteDir()
+            config.targetDirectory.deleteDir()
+        }
+    }
+
     // GROOVY-7804
     void testParameterlessClosureToGenericSAMTypeArgumentCoercion() {
         assertScript '''