Skip to content

Commit 653f746

Browse files
committed
GROOVY-11178
1 parent 248fc1d commit 653f746

File tree

9 files changed

+68
-2
lines changed

9 files changed

+68
-2
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/TypeReferenceSearchTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.eclipse.jdt.core.groovy.tests.search;
1717

18+
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
19+
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser;
1820
import static org.junit.Assert.assertEquals;
1921
import static org.junit.Assert.assertNotNull;
2022
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assume.assumeTrue;
2124

2225
import java.util.List;
2326

@@ -502,6 +505,32 @@ public void testInnerTypes5() throws Exception {
502505
assertEquals(secondContents.lastIndexOf("p.First"), match.getOffset());
503506
}
504507

508+
@Test // GROOVY-11178
509+
public void testTypeAnnotations() throws Exception {
510+
assumeTrue(isParrotParser());
511+
512+
String firstContents =
513+
"package p\n" +
514+
"import java.lang.annotation.*\n" +
515+
"@Target(ElementType.TYPE_USE)\n" +
516+
"public @interface First {\n" +
517+
"}\n";
518+
String secondContents =
519+
"package q\n" +
520+
"import p.*\n" +
521+
"Object o = new @First Object()\n";
522+
523+
List<SearchMatch> matches = searchForFirst(firstContents, secondContents, "p", "q");
524+
if (!isAtLeastGroovy(50)) {
525+
assertTrue(matches.isEmpty());
526+
} else {
527+
assertEquals(1, matches.size());
528+
SearchMatch match = matches.get(0);
529+
assertEquals("First".length(), match.getLength());
530+
assertEquals(secondContents.indexOf("First"), match.getOffset());
531+
}
532+
}
533+
505534
@Test
506535
public void testConstructorWithDefaultArgsInCompileStatic() throws Exception {
507536
String firstContents =

base/org.codehaus.groovy50/src/org/apache/groovy/parser/antlr4/AstBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3852,7 +3852,7 @@ public ClassNode visitCreatedName(final CreatedNameContext ctx) {
38523852
throw createParsingFailedException("Unsupported created name: " + ctx.getText(), ctx);
38533853
}
38543854

3855-
classNode.addAnnotations(this.visitAnnotationsOpt(ctx.annotationsOpt()));
3855+
classNode.addTypeAnnotations(this.visitAnnotationsOpt(ctx.annotationsOpt())); // GROOVY-11178
38563856

38573857
return classNode;
38583858
}

base/org.codehaus.groovy50/src/org/codehaus/groovy/classgen/ExtendedVerifier.java

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.codehaus.groovy.ast.RecordComponentNode;
3333
import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
3434
import org.codehaus.groovy.ast.expr.ClassExpression;
35+
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
3536
import org.codehaus.groovy.ast.expr.DeclarationExpression;
3637
import org.codehaus.groovy.ast.expr.Expression;
3738
import org.codehaus.groovy.ast.expr.PropertyExpression;
@@ -156,8 +157,21 @@ public void visitDeclarationExpression(DeclarationExpression expression) {
156157
visitTypeAnnotations(type);
157158
extractTypeUseAnnotations(expression.getAnnotations(), type, LOCAL_VARIABLE_TARGET);
158159
}
160+
// GRECLIPSE add
161+
expression.getRightExpression().visit(this);
162+
// GRECLIPSE end
159163
}
160164

165+
// GRECLIPSE add -- GROOVY-11178
166+
@Override
167+
public void visitConstructorCallExpression(ConstructorCallExpression expression) {
168+
if (!expression.isSpecialCall()) {
169+
visitTypeAnnotations(expression.getType());
170+
}
171+
super.visitConstructorCallExpression(expression);
172+
}
173+
// GRECLIPSE end
174+
161175
@Override
162176
public void visitConstructor(ConstructorNode node) {
163177
visitConstructorOrMethod(node, CONSTRUCTOR_TARGET);

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/integration/internal/GroovyIndexingVisitor.java

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ private void visitTypeReference(final ClassNode type, final boolean isAnnotation
354354
tokens = splitName(type, useQualifiedName);
355355
requestor.acceptAnnotationTypeReference(tokens, type.getStart(), type.getEnd());
356356
} else {
357+
if (useQualifiedName) visitAnnotations(type.getTypeAnnotations());
357358
tokens = splitName(GroovyUtils.getBaseType(type), useQualifiedName);
358359
requestor.acceptTypeReference(tokens, type.getStart(), type.getEnd());
359360
visitTypeParameters(type);

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/DepthFirstVisitor.java

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public void visitImport(ImportNode node) {
159159
@Override
160160
public void visitClass(ClassNode node) {
161161
visitAnnotations(node.getAnnotations());
162+
visitAnnotations(node.getTypeAnnotations());
162163

163164
// visit "<clinit>" statements before visitContents
164165
MethodNode clinit = node.getMethod("<clinit>", Parameter.EMPTY_ARRAY);

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ public void visitJDT(final IType type, final ITypeRequestor requestor) {
293293
// visit relocated @AnnotationCollector annotations
294294
visitAnnotations(AnnotationCollectorTransform.getMeta(node));
295295
}
296+
visitAnnotations(node.getTypeAnnotations()); // declaration TYPE_USE
296297

297298
// visit name "node"
298299
TypeLookupResult result = new TypeLookupResult(node, node, node, TypeConfidence.EXACT, scopes.getLast());

ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/OrganizeImportsTests.groovy

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.codehaus.groovy.eclipse.test.actions
1717

18+
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy
19+
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser
20+
1821
import groovy.test.NotYetImplemented
1922

2023
import org.junit.Test
@@ -580,6 +583,21 @@ final class OrganizeImportsTests extends OrganizeImportsTestSuite {
580583
doContentsCompareTest(contents)
581584
}
582585

586+
@Test // JSR 308
587+
void testRetainImport14() {
588+
String contents = '''\
589+
|import p.T
590+
|def m(List<@T ?> list) {}
591+
|'''
592+
if (!isParrotParser() || isAtLeastGroovy(40)) {
593+
doContentsCompareTest(contents)
594+
} else { // parses but annotation missed
595+
doContentsCompareTest(contents, '''\
596+
|def m(List<@T ?> list) {}
597+
|''')
598+
}
599+
}
600+
583601
@Test
584602
void testChoices() {
585603
String contents = '''\

ide/org.codehaus.groovy.eclipse.astviews/src/org/codehaus/groovy/eclipse/astviews/ASTView.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class ASTView extends ViewPart {
333333
}
334334
if (outer.redirectNode && label ==~ /annotations|compileUnit|declaredConstructors|enclosingMethod|fields|hasInconsistentHierarchy|/ +
335335
/innerClasses|interfaces|is(?!GenericsPlaceHolder|Synthetic(Public)?|UsingGenerics)\w+|methods|mixins|modifiers|/ +
336-
/outerClasses|package|permittedSubclasses|properties|superClass|typeAnnotations/) {
336+
/outerClasses|package|permittedSubclasses|properties|superClass/) {
337337
return false
338338
}
339339
if (!outer.redirectNode && label ==~ /unresolved(Name|Interfaces|SuperClass)/) {

ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/OrganizeGroovyImports.java

+2
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ protected void visitTypeParameters(GenericsType[] generics, String typeName) {
634634
if (generic.getStart() < 1) {
635635
continue;
636636
}
637+
visitAnnotations(generic.getType().getTypeAnnotations());
637638
if (!generic.isPlaceholder() && !generic.isWildcard()) {
638639
handleTypeReference(generic.getType(), false);
639640
}
@@ -668,6 +669,7 @@ private void handleStaticCall(ClassNode declaringClass, MethodCall call) {
668669
* that the import will be retained if the type is resolved.
669670
*/
670671
private void handleTypeReference(ClassNode node, boolean isAnnotation) {
672+
if (!isAnnotation) visitAnnotations(node.getTypeAnnotations());
671673
ClassNode type = GroovyUtils.getBaseType(node);
672674
if (ClassHelper.isPrimitiveType(type)) {
673675
return;

0 commit comments

Comments
 (0)