Skip to content

Commit 5d936fa

Browse files
committed
GROOVY-10894
1 parent 285dd31 commit 5d936fa

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/TraitsTests.java

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2022 the original author or authors.
2+
* Copyright 2009-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,12 @@
1616
package org.eclipse.jdt.groovy.core.tests.basic;
1717

1818
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertTrue;
2021

22+
import org.codehaus.groovy.ast.AnnotationNode;
2123
import org.codehaus.groovy.ast.ClassNode;
24+
import org.codehaus.groovy.ast.FieldNode;
2225
import org.codehaus.groovy.control.CompilationUnit;
2326
import org.junit.Ignore;
2427
import org.junit.Test;
@@ -3045,4 +3048,56 @@ public void testTraits10521() {
30453048

30463049
runConformTest(sources, "90[x]");
30473050
}
3051+
3052+
@Test
3053+
public void testTraits10894() {
3054+
//@formatter:off
3055+
String[] sources = {
3056+
"Script.groovy",
3057+
"new C()\n",
3058+
3059+
"C.groovy",
3060+
"class C implements T {\n" +
3061+
"}\n",
3062+
3063+
"T.groovy",
3064+
"trait T {\n" +
3065+
" @Validate String foo\n" +
3066+
"}\n",
3067+
3068+
"Validate.java",
3069+
"import java.lang.annotation.*;\n" +
3070+
"@Target(ElementType.TYPE_USE) \n" +
3071+
"@Retention(RetentionPolicy.RUNTIME)\n" +
3072+
"public @interface Validate { /**/ }\n" ,
3073+
};
3074+
//@formatter:on
3075+
3076+
if (isAtLeastGroovy(40)) {
3077+
runConformTest(sources);
3078+
3079+
ClassNode helper = getCUDeclFor("T.groovy").getCompilationUnit().getClassNode("T$Trait$FieldHelper");
3080+
for (FieldNode field : helper.getFields()) {
3081+
if (field.getName().endsWith("__foo")) {
3082+
java.util.List<AnnotationNode> typeTags =
3083+
field.getOriginType().getTypeAnnotations();
3084+
assertEquals(1, typeTags.size()); // no duplication
3085+
}
3086+
}
3087+
} else {
3088+
runNegativeTest(sources,
3089+
"----------\n" +
3090+
"1. ERROR in C.groovy (at line 1)\n" +
3091+
"\tclass C implements T {\n" +
3092+
"\t^^^^^^^^^^^^^^^^^^^^^\n" +
3093+
"Groovy:Annotation @Validate is not allowed on element FIELD\n" +
3094+
"----------\n" +
3095+
"----------\n" +
3096+
"1. ERROR in T.groovy (at line 2)\n" +
3097+
"\t@Validate String foo\n" +
3098+
"\t^^^^^^^^^\n" +
3099+
"Groovy:Annotation @Validate is not allowed on element FIELD\n" +
3100+
"----------\n");
3101+
}
3102+
}
30483103
}

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f
597597
dummyField = new FieldNode(
598598
dummyFieldName,
599599
ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC,
600-
field.getOriginType(),
600+
field.getOriginType().getPlainNodeReference(),
601601
fieldHelper,
602602
null
603603
);

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f
573573
dummyField = new FieldNode(
574574
dummyFieldName,
575575
ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC,
576-
field.getOriginType(),
576+
field.getOriginType().getPlainNodeReference(),
577577
fieldHelper,
578578
null
579579
);

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f
583583
dummyField = new FieldNode(
584584
dummyFieldName,
585585
ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC,
586-
field.getOriginType(),
586+
field.getOriginType().getPlainNodeReference(),
587587
fieldHelper,
588588
null
589589
);

0 commit comments

Comments
 (0)