Skip to content
Permalink
Browse files
GROOVY-10571: add test case
  • Loading branch information
eric-milles committed Apr 7, 2022
1 parent cb202ee commit 4545a8bafe0804d66a7c3380f6cc752234e5ceca
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
@@ -61,7 +61,6 @@
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 @@ protected Expression transformVariableExpression(final VariableExpression ve) {
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);
@@ -77,6 +77,7 @@
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 void generateClass(ClassNode classNode) throws FileNotFoundException {
if ((classNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return;


if (null == outputPath) {
if (outputPath == null) {
generateMemStub(classNode);
} else {
generateFileStub(classNode);
@@ -941,40 +942,31 @@ private void printParams(PrintWriter out, MethodNode methodNode) {
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();
@@ -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()
}
}

0 comments on commit 4545a8b

Please sign in to comment.