From 5d936fabf20da5718e8da407a1a18dacdf575539 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Wed, 4 Jan 2023 15:26:14 -0600 Subject: [PATCH] GROOVY-10894 --- .../groovy/core/tests/basic/TraitsTests.java | 57 ++++++++++++++++++- .../trait/TraitASTTransformation.java | 2 +- .../trait/TraitASTTransformation.java | 2 +- .../trait/TraitASTTransformation.java | 2 +- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/TraitsTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/TraitsTests.java index ccb10d0796..a9fe08e612 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/TraitsTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/TraitsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2022 the original author or authors. + * Copyright 2009-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,12 @@ package org.eclipse.jdt.groovy.core.tests.basic; import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import org.codehaus.groovy.ast.AnnotationNode; import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.FieldNode; import org.codehaus.groovy.control.CompilationUnit; import org.junit.Ignore; import org.junit.Test; @@ -3045,4 +3048,56 @@ public void testTraits10521() { runConformTest(sources, "90[x]"); } + + @Test + public void testTraits10894() { + //@formatter:off + String[] sources = { + "Script.groovy", + "new C()\n", + + "C.groovy", + "class C implements T {\n" + + "}\n", + + "T.groovy", + "trait T {\n" + + " @Validate String foo\n" + + "}\n", + + "Validate.java", + "import java.lang.annotation.*;\n" + + "@Target(ElementType.TYPE_USE) \n" + + "@Retention(RetentionPolicy.RUNTIME)\n" + + "public @interface Validate { /**/ }\n" , + }; + //@formatter:on + + if (isAtLeastGroovy(40)) { + runConformTest(sources); + + ClassNode helper = getCUDeclFor("T.groovy").getCompilationUnit().getClassNode("T$Trait$FieldHelper"); + for (FieldNode field : helper.getFields()) { + if (field.getName().endsWith("__foo")) { + java.util.List typeTags = + field.getOriginType().getTypeAnnotations(); + assertEquals(1, typeTags.size()); // no duplication + } + } + } else { + runNegativeTest(sources, + "----------\n" + + "1. ERROR in C.groovy (at line 1)\n" + + "\tclass C implements T {\n" + + "\t^^^^^^^^^^^^^^^^^^^^^\n" + + "Groovy:Annotation @Validate is not allowed on element FIELD\n" + + "----------\n" + + "----------\n" + + "1. ERROR in T.groovy (at line 2)\n" + + "\t@Validate String foo\n" + + "\t^^^^^^^^^\n" + + "Groovy:Annotation @Validate is not allowed on element FIELD\n" + + "----------\n"); + } + } } diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java index dcb76c6e8b..a667cb7e72 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java @@ -597,7 +597,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f dummyField = new FieldNode( dummyFieldName, ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, - field.getOriginType(), + field.getOriginType().getPlainNodeReference(), fieldHelper, null ); diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java index 4553d86449..917050a75a 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java @@ -573,7 +573,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f dummyField = new FieldNode( dummyFieldName, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, - field.getOriginType(), + field.getOriginType().getPlainNodeReference(), fieldHelper, null ); diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java index f9659ffcb7..1c7ff1c64c 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/trait/TraitASTTransformation.java @@ -583,7 +583,7 @@ private void processField(final FieldNode field, final MethodNode initializer, f dummyField = new FieldNode( dummyFieldName, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, - field.getOriginType(), + field.getOriginType().getPlainNodeReference(), fieldHelper, null );