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/04/07 19:53:18 UTC

[groovy] branch master updated: GROOVY-10571: add test case

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

emilles 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 4545a8bafe GROOVY-10571: add test case
4545a8bafe is described below

commit 4545a8bafe0804d66a7c3380f6cc752234e5ceca
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Apr 7 14:35:03 2022 -0500

    GROOVY-10571: add test case
---
 .../codehaus/groovy/control/ResolveVisitor.java    |  5 +-
 .../groovy/tools/javac/JavaStubGenerator.java      | 32 ++++------
 .../groovy/tools/stubgenerator/Groovy10571.groovy  | 74 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 56c65e0322..d001b3f9dc 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -61,7 +61,6 @@ import org.codehaus.groovy.ast.stmt.ForStatement;
 import org.codehaus.groovy.ast.stmt.Statement;
 import org.codehaus.groovy.classgen.VariableScopeVisitor;
 import org.codehaus.groovy.runtime.memoize.UnlimitedConcurrentCache;
-import org.codehaus.groovy.syntax.SyntaxException;
 import org.codehaus.groovy.syntax.Types;
 import org.codehaus.groovy.transform.trait.Traits;
 import org.codehaus.groovy.vmplugin.VMPluginFactory;
@@ -1036,7 +1035,9 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
                 for (VariableScope scope = currentScope; scope != null && !scope.isRoot(); scope = scope.getParent()) {
                     if (scope.removeReferencedClassVariable(ve.getName()) == null) break;
                 }
-                return new ClassExpression(t);
+                ClassExpression ce = new ClassExpression(t);
+                ce.setSourcePosition(ve);
+                return ce;
             }
         }
         resolveOrFail(ve.getType(), ve);
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 be10047194..9c6a78676f 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -77,6 +77,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.StringJoiner;
 import java.util.stream.Stream;
 
 import static org.apache.groovy.ast.tools.ConstructorNodeUtils.getFirstIfSpecialConstructorCall;
@@ -148,7 +149,7 @@ public class JavaStubGenerator {
         if ((classNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return;
 
 
-        if (null == outputPath) {
+        if (outputPath == null) {
             generateMemStub(classNode);
         } else {
             generateFileStub(classNode);
@@ -941,40 +942,31 @@ public class JavaStubGenerator {
         out.print(")");
     }
 
-    private void printAnnotations(PrintWriter out, AnnotatedNode annotated) {
+    private void printAnnotations(final PrintWriter out, final AnnotatedNode annotated) {
         if (!java5) return;
         for (AnnotationNode annotation : annotated.getAnnotations()) {
             printAnnotation(out, annotation);
         }
     }
 
-    private void printAnnotation(PrintWriter out, AnnotationNode annotation) {
-        out.print("@" + annotation.getClassNode().getName().replace('$', '.') + "(");
-        boolean first = true;
-        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()));
+    private void printAnnotation(final PrintWriter out, final AnnotationNode annotation) {
+        StringJoiner sj = new StringJoiner(", ", "@" + annotation.getClassNode().getName().replace('$', '.') + "(", ") ");
+        for (Map.Entry<String, Expression> entry : annotation.getMembers().entrySet()) {
+            sj.add(entry.getKey() + "=" + getAnnotationValue(entry.getValue()));
         }
-        out.print(") ");
+        out.print(sj.toString());
     }
 
-    private String getAnnotationValue(Object memberValue) {
+    private String getAnnotationValue(final Object memberValue) {
         String val = "null";
         boolean replaceDollars = true;
         if (memberValue instanceof ListExpression) {
-            StringBuilder sb = new StringBuilder("{");
-            boolean first = true;
+            StringJoiner sj = new StringJoiner(",", "{", "}");
             ListExpression le = (ListExpression) memberValue;
             for (Expression e : le.getExpressions()) {
-                if (first) first = false;
-                else sb.append(",");
-                sb.append(getAnnotationValue(e));
+                sj.add(getAnnotationValue(e));
             }
-            sb.append("}");
-            val = sb.toString();
+            val = sj.toString();
         } else if (memberValue instanceof ConstantExpression) {
             ConstantExpression ce = (ConstantExpression) memberValue;
             Object constValue = ce.getValue();
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10571.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10571.groovy
new file mode 100644
index 0000000000..a761a6ba32
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10571.groovy
@@ -0,0 +1,74 @@
+/*
+ *  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
+
+import groovy.test.NotYetImplemented
+
+final class Groovy10571 extends StringSourcesStubTestCase {
+
+    @Override
+    Map<String, String> provideSources() {
+        [
+            'A.groovy': '''
+                public @interface A {
+                    Class[] value()
+                }
+            ''',
+            'B.java': '''
+                public class B {
+                }
+            ''',
+            'C.groovy': '''
+                import groovy.transform.*
+                import java.lang.annotation.*
+
+                @stc.POJO
+                @A([ B, Object ])
+                @AnnotationCollector
+                @Target(ElementType.TYPE)
+                @Retention(RetentionPolicy.SOURCE)
+                public @interface C {
+                }
+            ''',
+            'D.groovy': '''
+                @C class D {
+                }
+            ''',
+            'Main.java': '''
+                public class Main {
+                    public static void main(String[] args) {
+                        new D();
+                    }
+                }
+            ''',
+        ]
+    }
+
+    @Override
+    void verifyStubs() {
+        String stub = stubJavaSourceFor('C')
+        assert stub.contains('Object.class')
+        assert stub.contains('B.class')//bug
+    }
+
+    @Override @NotYetImplemented
+    void testRun() {
+        super.testRun()
+    }
+}