You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/01/27 23:52:34 UTC

[groovy] branch master updated: GROOVY-9777: stubgen: add cast for Object parameter in special ctor call

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4d795cd  GROOVY-9777: stubgen: add cast for Object parameter in special ctor call
4d795cd is described below

commit 4d795cda670ce7f85f0c46da225ec9a062c6b4ff
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      | 12 ++---
 .../groovy/tools/stubgenerator/Groovy9777.groovy   | 60 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 6 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 b72be73..3d4158c 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -627,15 +627,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(", ");
                 }
             }
@@ -819,15 +819,15 @@ public class JavaStubGenerator {
         }
     }
 
-    private void printDefaultValue(PrintWriter out, ClassNode type) {
-        if (type.redirect() != ClassHelper.OBJECT_TYPE && type.redirect() != ClassHelper.boolean_TYPE) {
+    private void printDefaultValue(final PrintWriter out, final ClassNode type) {
+        if (!type.equals(ClassHelper.boolean_TYPE)) {
             out.print("(");
             printType(out, type);
             out.print(")");
         }
 
         if (ClassHelper.isPrimitiveType(type)) {
-            if (type == ClassHelper.boolean_TYPE) {
+            if (type.equals(ClassHelper.boolean_TYPE)) {
                 out.print("false");
             } else {
                 out.print("0");
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 0000000..d4fcd48
--- /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'
+    }
+}