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 2023/01/18 00:02:43 UTC

[groovy] 01/05: GROOVY-9777: stubgen: add cast for Object parameter in special ctor call

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

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

commit 3986dfb39e0b348cb3af91d5a265c03b361d9814
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jan 27 14:04:57 2021 -0600

    GROOVY-9777: stubgen: add cast for Object parameter in special ctor call
---
 .../groovy/tools/javac/JavaStubGenerator.java      | 16 +++---
 .../groovy/tools/stubgenerator/Groovy9777.groovy   | 60 ++++++++++++++++++++++
 2 files changed, 67 insertions(+), 9 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 eb10193ac7..9580265cfb 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -574,15 +574,15 @@ public class JavaStubGenerator {
 
     private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) {
         // 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 crapo
+        // then write out an invoke w/nulls using casts to avoid ambiguous calls
 
         Parameter[] params = selectAccessibleConstructorFromSuper(node);
         if (params != null) {
             out.print("super (");
 
-            for (int i = 0; i < params.length; i++) {
+            for (int i = 0, n = params.length; i < n; i += 1) {
                 printDefaultValue(out, params[i].getType());
-                if (i + 1 < params.length) {
+                if (i + 1 < n) {
                     out.print(", ");
                 }
             }
@@ -762,7 +762,7 @@ public class JavaStubGenerator {
     }
 
     private void printDefaultValue(final PrintWriter out, final ClassNode type) {
-        if (!type.equals(ClassHelper.OBJECT_TYPE) && !type.equals(ClassHelper.boolean_TYPE)) {
+        if (!type.equals(ClassHelper.boolean_TYPE)) {
             out.print("(");
             printType(out, type);
             out.print(")");
@@ -869,13 +869,11 @@ public class JavaStubGenerator {
 
     private void printAnnotation(PrintWriter out, AnnotationNode annotation) {
         out.print("@" + annotation.getClassNode().getName().replace('$', '.') + "(");
-        boolean first = true;
+        int i = 0;
         Map<String, Expression> members = annotation.getMembers();
         for (Map.Entry<String, Expression> entry : members.entrySet()) {
-            String key = entry.getKey();
-            if (first) first = false;
-            else out.print(", ");
-            out.print(key + "=" + getAnnotationValue(entry.getValue()));
+            if (i++ != 0) out.print(", ");
+            out.print(entry.getKey() + "=" + getAnnotationValue(entry.getValue()));
         }
         out.print(") ");
     }
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy9777.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy9777.groovy
new file mode 100644
index 0000000000..d4fcd48e48
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy9777.groovy
@@ -0,0 +1,60 @@
+/*
+ *  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 Groovy9777 extends StringSourcesStubTestCase {
+
+    @Override
+    Map<String, String> provideSources() {
+        [
+            'A9777.groovy': '''
+                abstract class A9777 {
+                    A9777(Object o) {
+                        this(o.class.name)
+                    }
+                    A9777(Class  c) {
+                        this(c.name)
+                    }
+                    A9777(String s) {
+                    }
+                }
+            ''',
+            'C9777.groovy': '''
+                class C9777 extends A9777 {
+                    C9777() {
+                        super(C9777.class.name) // generates ambiguous "super (null);" in stub
+                    }
+                }
+            ''',
+            'Main.java': '''
+                public class Main {
+                    public static void main(String[] args) {
+                        new C9777();
+                    }
+                }
+            ''',
+        ]
+    }
+
+    @Override
+    void verifyStubs() {
+        def specialCtorCall = (stubJavaSourceFor('C9777') =~ /super\s*\((.+?)\);/)
+        assert specialCtorCall.find() && specialCtorCall.group(1) == '(java.lang.Object)null'
+    }
+}