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 2021/08/19 16:12:36 UTC

[groovy] branch GROOVY_3_0_X updated (5daebe3 -> a04fbed)

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 5daebe3  remove test case
     new 7d3c756  GROOVY-6282, GROOVY-10122: stubgen: find type of helper method in special ctor call
     new 822b807  GROOVY-10141: retain parse order of anon. inner types in module classes
     new a04fbed  GROOVY-10199: ASTTest: evaluate with compliation unit's transform loader

The 3 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/ASTTestTransformation.groovy  |  3 +-
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   |  2 +-
 .../groovy/tools/javac/JavaStubGenerator.java      | 76 +++++++++-------------
 src/test/gls/innerClass/InnerClassTest.groovy      | 20 ++++++
 .../bugs/{Groovy9566.groovy => Groovy10199.groovy} | 15 +++--
 .../{Groovy7306.groovy => Groovy10122.groovy}      | 25 +++----
 .../apache/groovy/parser/antlr4/AstBuilder.java    |  3 +-
 7 files changed, 72 insertions(+), 72 deletions(-)
 copy src/test/groovy/bugs/{Groovy9566.groovy => Groovy10199.groovy} (69%)
 copy src/test/org/codehaus/groovy/tools/stubgenerator/{Groovy7306.groovy => Groovy10122.groovy} (67%)

[groovy] 02/03: GROOVY-10141: retain parse order of anon. inner types in module classes

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 822b8079668997f1dd45f269e0198f3cda24ae02
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jun 16 15:14:02 2021 -0500

    GROOVY-10141: retain parse order of anon. inner types in module classes
    
    - InnerClassVisitor checks this$0 which isn't set up if C$1$1 before C$1
    
    Conflicts:
    	src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
---
 .../org/codehaus/groovy/antlr/AntlrParserPlugin.java |  2 +-
 src/test/gls/innerClass/InnerClassTest.groovy        | 20 ++++++++++++++++++++
 .../org/apache/groovy/parser/antlr4/AstBuilder.java  |  3 +--
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index 855675b..df003ee 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -616,13 +616,13 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
         ((InnerClassNode) classNode).setAnonymous(true);
         classNode.setEnclosingMethod(methodNode);
         configureAST(classNode, node);
+        output.addClass(classNode);
 
         assertNodeType(OBJBLOCK, node);
         objectBlock(node);
 
         AnonymousInnerClassCarrier ret = new AnonymousInnerClassCarrier();
         ret.innerClass = classNode;
-        output.addClass(classNode);
         classNode = oldNode;
         return ret;
     }
diff --git a/src/test/gls/innerClass/InnerClassTest.groovy b/src/test/gls/innerClass/InnerClassTest.groovy
index c280d6f..32fd362 100644
--- a/src/test/gls/innerClass/InnerClassTest.groovy
+++ b/src/test/gls/innerClass/InnerClassTest.groovy
@@ -1609,6 +1609,26 @@ final class InnerClassTest {
         '''
     }
 
+    @Test // GROOVY-10141
+    void testInnerClassIn2xAIC() {
+        assertScript '''
+            class Outer {
+                class Inner {
+                }
+                def obj = new Object() {
+                    String toString() {
+                        new Object() {
+                            String toString() {
+                                new Inner()
+                            }
+                        }
+                    }
+                }
+            }
+            new Outer().obj.toString()
+        '''
+    }
+
     @Test // GROOVY-8274
     void testMissingMethodHandling() {
         assertScript '''
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index e4051c1..e990877 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -3040,14 +3040,13 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
         anonymousInnerClass.setAnonymous(true);
         anonymousInnerClass.putNodeMetaData(CLASS_NAME, innerClassName);
         configureAST(anonymousInnerClass, ctx);
+        classNodeList.add(anonymousInnerClass);
 
         classNodeStack.push(anonymousInnerClass);
         ctx.classBody().putNodeMetaData(CLASS_DECLARATION_CLASS_NODE, anonymousInnerClass);
         this.visitClassBody(ctx.classBody());
         classNodeStack.pop();
 
-        classNodeList.add(anonymousInnerClass);
-
         return anonymousInnerClass;
     }
 

[groovy] 01/03: GROOVY-6282, GROOVY-10122: stubgen: find type of helper method in special ctor 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_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 7d3c75602e0354cbb98c05c91782144d83348995
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jun 2 16:03:59 2021 -0500

    GROOVY-6282, GROOVY-10122: stubgen: find type of helper method in
    special ctor call
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
---
 .../groovy/tools/javac/JavaStubGenerator.java      | 76 +++++++++-------------
 .../groovy/tools/stubgenerator/Groovy10122.groovy  | 53 +++++++++++++++
 2 files changed, 82 insertions(+), 47 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index fab74c0..f07007d 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -18,6 +18,7 @@
  */
 package org.codehaus.groovy.tools.javac;
 
+import org.apache.groovy.ast.tools.ExpressionUtils;
 import org.apache.groovy.io.StringBuilderWriter;
 import org.codehaus.groovy.ast.AnnotatedNode;
 import org.codehaus.groovy.ast.AnnotationNode;
@@ -40,6 +41,7 @@ import org.codehaus.groovy.ast.expr.ConstantExpression;
 import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.ListExpression;
+import org.codehaus.groovy.ast.expr.MethodCallExpression;
 import org.codehaus.groovy.ast.expr.PropertyExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.BlockStatement;
@@ -109,8 +111,6 @@ public class JavaStubGenerator {
         dir.mkdirs();
     }
 
-
-    private static final int DEFAULT_BUFFER_SIZE = 8 * 1024; // 8K
     public void generateClass(ClassNode classNode) throws FileNotFoundException {
         // Only attempt to render our self if our super-class is resolved, else wait for it
         if (requireSuperResolved && !classNode.getSuperClass().isResolved()) {
@@ -154,6 +154,8 @@ public class JavaStubGenerator {
         javaStubCompilationUnitSet.add(new RawJavaFileObject(createJavaStubFile(fileName).toPath().toUri()));
     }
 
+    private static final int DEFAULT_BUFFER_SIZE = 8 * 1024; // 8K
+
     private String generateStubContent(ClassNode classNode) {
         Writer writer = new StringBuilderWriter(DEFAULT_BUFFER_SIZE);
 
@@ -432,16 +434,7 @@ public class JavaStubGenerator {
     }
 
     private static boolean sameParameterTypes(final Parameter[] firstParams, final Parameter[] secondParams) {
-        boolean sameParams = firstParams.length == secondParams.length;
-        if (sameParams) {
-            for (int i = 0; i < firstParams.length; i++) {
-                if (!firstParams[i].getType().equals(secondParams[i].getType())) {
-                    sameParams = false;
-                    break;
-                }
-            }
-        }
-        return sameParams;
+        return org.codehaus.groovy.ast.tools.ParameterUtils.parametersEqual(firstParams, secondParams);
     }
 
     private void printConstructors(PrintWriter out, ClassNode classNode) {
@@ -624,73 +617,64 @@ public class JavaStubGenerator {
         return true;
     }
 
-    private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) {
+    private void printSpecialConstructorArgs(final PrintWriter out, final ConstructorNode ctor, final ConstructorCallExpression ctorCall) {
         // Select a constructor from our class, or super-class which is legal to call,
         // then write out an invoke w/nulls using casts to avoid ambiguous calls
-
-        Parameter[] params = selectAccessibleConstructorFromSuper(node);
+        Parameter[] params = selectAccessibleConstructorFromSuper(ctor);
         if (params != null) {
             out.print("super (");
-
             for (int i = 0, n = params.length; i < n; i += 1) {
                 printDefaultValue(out, params[i].getType());
                 if (i + 1 < n) {
                     out.print(", ");
                 }
             }
-
             out.println(");");
             return;
         }
 
         // Otherwise try the older method based on the constructor's call expression
-        Expression arguments = constrCall.getArguments();
-
-        if (constrCall.isSuperCall()) {
+        Expression arguments = ctorCall.getArguments();
+        if (ctorCall.isSuperCall()) {
             out.print("super(");
         } else {
             out.print("this(");
         }
-
-        // Else try to render some arguments
         if (arguments instanceof ArgumentListExpression) {
-            ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments;
-            List<Expression> args = argumentListExpression.getExpressions();
-
+            List<Expression> args = ((ArgumentListExpression) arguments).getExpressions();
+            int i = 0, n = args.size();
             for (Expression arg : args) {
                 if (arg instanceof ConstantExpression) {
-                    ConstantExpression expression = (ConstantExpression) arg;
-                    Object o = expression.getValue();
-
-                    if (o instanceof String) {
+                    Object value = ((ConstantExpression) arg).getValue();
+                    if (value instanceof String) {
                         out.print("(String)null");
                     } else {
-                        out.print(expression.getText());
+                        out.print(arg.getText());
                     }
                 } else {
-                    ClassNode type = getConstructorArgumentType(arg, node);
-                    printDefaultValue(out, type);
+                    printDefaultValue(out, getConstructorArgumentType(arg, ctor));
                 }
-
-                if (arg != args.get(args.size() - 1)) {
+                if (++i < n) {
                     out.print(", ");
                 }
             }
         }
-
         out.println(");");
     }
 
-    private static ClassNode getConstructorArgumentType(Expression arg, ConstructorNode node) {
-        if (!(arg instanceof VariableExpression)) return arg.getType();
-        VariableExpression vexp = (VariableExpression) arg;
-        String name = vexp.getName();
-        for (Parameter param : node.getParameters()) {
-            if (param.getName().equals(name)) {
-                return param.getType();
+    private static ClassNode getConstructorArgumentType(final Expression arg, final ConstructorNode ctor) {
+        if (arg instanceof VariableExpression) {
+            return ((VariableExpression) arg).getAccessedVariable().getType();
+        }
+        if (arg instanceof MethodCallExpression) { // GROOVY-10122
+            MethodCallExpression mce = (MethodCallExpression) arg;
+            if (ExpressionUtils.isThisExpression(mce.getObjectExpression())) {
+                MethodNode mn = ctor.getDeclaringClass().tryFindPossibleMethod(mce.getMethodAsString(), mce.getArguments());
+                if (mn != null) return mn.getReturnType();
             }
+            return null;
         }
-        return vexp.getType();
+        return arg.getType();
     }
 
     private void printMethod(PrintWriter out, ClassNode clazz, MethodNode methodNode) {
@@ -819,13 +803,13 @@ public class JavaStubGenerator {
     }
 
     private void printDefaultValue(final PrintWriter out, final ClassNode type) {
-        if (!type.equals(ClassHelper.boolean_TYPE)) {
+        if (type != null && !type.equals(ClassHelper.boolean_TYPE)) {
             out.print("(");
             printType(out, type);
             out.print(")");
         }
 
-        if (ClassHelper.isPrimitiveType(type)) {
+        if (type != null && ClassHelper.isPrimitiveType(type)) {
             if (type.equals(ClassHelper.boolean_TYPE)) {
                 out.print("false");
             } else {
@@ -1059,14 +1043,12 @@ public class JavaStubGenerator {
 
     private static String escapeSpecialChars(String value) {
         return InvokerHelper.escapeBackslashes(value).replace("\"", "\\\"");
-
     }
 
     private static boolean isInterfaceOrTrait(ClassNode cn) {
         return cn.isInterface() || Traits.isTrait(cn);
     }
 
-
     private final Set<JavaFileObject> javaStubCompilationUnitSet = new HashSet<>();
 
     public Set<JavaFileObject> getJavaStubCompilationUnitSet() {
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10122.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10122.groovy
new file mode 100644
index 0000000..0ba0ba4
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10122.groovy
@@ -0,0 +1,53 @@
+/*
+ *  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 org.codehaus.groovy.tools.stubgenerator
+
+final class Groovy10122 extends StringSourcesStubTestCase {
+
+    @Override
+    Map<String, String> provideSources() {
+        [
+            'A.java': '''
+                public abstract class A {
+                    A(Integer i) {
+                    }
+                }
+            ''',
+            'C.groovy': '''
+                class C extends A {
+                    C() { super(m()) }
+                    static Integer m() { 42 }
+                }
+            ''',
+            'Main.java': '''
+                public class Main {
+                    public static void main(String[] args) {
+                        new C();
+                    }
+                }
+            ''',
+        ]
+    }
+
+    @Override
+    void verifyStubs() {
+        def specialCtorCall = (stubJavaSourceFor('C') =~ /super\s*\((.+?)\);/)
+        assert specialCtorCall.find() && specialCtorCall.group(1) == '(java.lang.Integer)null'
+    }
+}

[groovy] 03/03: GROOVY-10199: ASTTest: evaluate with compliation unit's transform loader

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 a04fbedf9b3aaf429c7ffe2de5f39249b3f59899
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Aug 12 15:25:51 2021 -0500

    GROOVY-10199: ASTTest: evaluate with compliation unit's transform loader
    
    Conflicts:
    	src/main/groovy/org/codehaus/groovy/transform/ASTTestTransformation.groovy
---
 .../groovy/transform/ASTTestTransformation.groovy  |  3 +-
 src/test/groovy/bugs/Groovy10199.groovy            | 35 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/main/groovy/org/codehaus/groovy/transform/ASTTestTransformation.groovy b/src/main/groovy/org/codehaus/groovy/transform/ASTTestTransformation.groovy
index 15ca578..cfdbe51 100644
--- a/src/main/groovy/org/codehaus/groovy/transform/ASTTestTransformation.groovy
+++ b/src/main/groovy/org/codehaus/groovy/transform/ASTTestTransformation.groovy
@@ -111,7 +111,8 @@ class ASTTestTransformation implements ASTTransformation, CompilationUnitAware {
 
                     def config = new CompilerConfiguration()
                     config.addCompilationCustomizers(customizer)
-                    new GroovyShell(binding, config).evaluate(testSource)
+                    def loader = compilationUnit.transformLoader
+                    new GroovyShell(loader, binding, config).evaluate(testSource)
                 }
             }
         }
diff --git a/src/test/groovy/bugs/Groovy10199.groovy b/src/test/groovy/bugs/Groovy10199.groovy
new file mode 100644
index 0000000..526f1ae
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10199.groovy
@@ -0,0 +1,35 @@
+/*
+ *  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
+
+final class Groovy10199 {
+    @Test
+    void testTransformClasspath() {
+        def shell = new GroovyShell()
+        shell.classLoader.addClasspath('gradle/wrapper/gradle-wrapper.jar')
+        shell.evaluate '''
+            @groovy.transform.ASTTest(value={
+                org.gradle.wrapper.WrapperConfiguration // Cannot get property 'gradle' on null object
+            })
+            def var = null
+        '''
+    }
+}