[ 
https://issues.apache.org/jira/browse/GROOVY-12787?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150287#comment-18150287
 ] 

ASF GitHub Bot commented on GROOVY-12787:
-----------------------------------------

paulk-asert commented on PR #3352:
URL: https://github.com/apache/groovy/pull/3352#issuecomment-7131552554

   Thanks @swampie for the clear report, the reproducer and the regression 
test. They made this much easier to track down.
   
   We've gone with a different fix from the GROOVY-11838 backport, so I'll 
close this PR, but your test commit has been included with you as the author.
   
   Root cause on 5.x. @NamedParam is repeatable, so ExtendedVerifier wraps the 
repeated annotations in an implicit @NamedParams container. On 5.x that 
container node never reads its own @Target(PARAMETER). It falls back to "all 
targets", which includes TYPE_USE, so it gets copied onto the Map parameter's 
type. That type node is shared, so each @MapConstructor class adds another 
copy, and you end up with the duplicate type annotations.
   
   Why not backport GROOVY-11838. It hides the problem by narrowing the default 
targets for every annotation without @Target. That would make some code that 
compiles on 5.1.x today fail to compile, which we'd rather avoid in a patch 
release. The fix we've used reads the container's declared targets. It's a 
two-line change in ExtendedVerifier, and default target handling on 5.x stays 
as it is.
   
   What landed where:
   - master and GROOVY_6_0_X: your 
testNamedParamsAreNotCopiedOntoSharedMapType, plus a test for an implicit 
container with an explicit, narrower @Target. These are tests only: 
GROOVY-11884 already reads @Target from the annotation class there, so neither 
branch has the bug. That also answers your question about adding the test to 
master.
   - GROOVY_5_1_X: the ExtendedVerifier fix plus both tests (<5.1 commit 
SHAs>). Both tests fail on 5.1 without the fix and pass with it. This will be 
in 5.1.4.
   
   If you get a chance, it would be great to hear whether a 5.1.4 snapshot 
clears the duplicates on your ~8,500-class application.
   
   Thanks again!




> @MapConstructor constructors get duplicate @NamedParams type annotations 
> (AnnotationFormatError) on 5.x
> -------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12787
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12787
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 5.0.8, 5.1.3
>            Reporter: Matteo Fiandesio
>            Priority: Minor
>
> When a compilation unit contains more than one {{@MapConstructor}} class, 
> each generated {{Map}} constructor carries the {{@NamedParams}} type 
> annotation several times on its parameter. Reflecting on it fails:
> {noformat}
> java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: 
> interface groovy.transform.NamedParams: 
> @groovy.transform.NamedParams({@groovy.transform.NamedParam(type=java.lang.String.class,
>  value="title", required=false), 
> @groovy.transform.NamedParam(type=java.lang.Integer.class, value="size", 
> required=false)})
> {noformat}
> This breaks anything that reads type annotations, for example Hibernate 
> Validator or {{Constructor.getAnnotatedParameterTypes()}}. The data is also 
> wrong: the error above is raised for {{First}}, but the {{@NamedParams}} it 
> reports lists {{Second}}'s parameters ({{title}}, {{size}}).
> h3. Reproducer
> {code:groovy}
> import groovy.transform.MapConstructor
> @MapConstructor
> class First { String name; Long id }
> @MapConstructor
> class Second { String title; Integer size }
> @MapConstructor
> class Third { String label }
> ['First', 'Second', 'Third'].each { n ->
>     Class.forName(n).declaredConstructors.findAll { it.parameterCount == 1 
> }.each { c ->
>         try { c.annotatedParameterTypes*.annotations; println "ok   
> ${n}(Map)" }
>         catch (Throwable e) { println "FAIL ${n}(Map): $e" }
>     }
> }
> {code}
> Compiled and run with each version (JDK 25):
> ||Version||Result||
> |4.0.33|ok|
> |5.0.8|FAIL (all three)|
> |5.1.3|FAIL (all three)|
> |GROOVY_5_1_X tip (5.1.4-SNAPSHOT)|FAIL (all three)|
> |6.0.0, 6.0.1-SNAPSHOT, 7.0.0-SNAPSHOT|ok|
> h3. Cause
> Since GROOVY-11684 (5.0.0-beta-2) the {{@MapConstructor}} constructor carries 
> {{@NamedParams}}. {{@NamedParams}} has no explicit {{@Target}}, and on 5.x an 
> annotation without {{@Target}} is also treated as applicable to {{TYPE_USE}}, 
> so it is written as a type annotation onto the {{Map}} type node that all 
> {{@MapConstructor}} classes in the compilation share.
> GROOVY-11838 ({{71b6eb9a53}}, fixed in 6.0.0-alpha-1) applies JLS 9.6.4.1, 
> under which an annotation without {{@Target}} applies to no type contexts. A 
> {{git bisect}} between the {{GROOVY_5_1_X}} branch point and {{GROOVY_6_0_0}} 
> identifies it as the commit that fixes this.
> h3. Backport to GROOVY_5_1_X
> {{71b6eb9a53}} cherry-picks onto {{GROOVY_5_1_X}} with one conflict, 
> formatting only: {{AnnotationNode}}'s constants were reformatted on master. 
> The change itself is {{allowedTargets = 0x4FF}} in place of {{ALL_TARGETS}}, 
> and {{ALL_TARGETS}} can then be removed. The {{AnnotationNodeTest}} the 
> commit adds does not exist on 5.1 and has to be created (JUnit 4 there).
> On {{GROOVY_5_1_X}} with the backport:
> * the reproducer above passes;
> * the annotation-related test suites pass: 1,446 tests ({{*Annotation*}}, 
> {{*TypeAnnotation*}}, {{*MapConstructor*}}, {{*NamedParam*}}, 
> {{*NamedVariant*}}, {{bugs.Groovy5041}}), 0 failures;
> * a Micronaut 5 application of about 8,500 Groovy classes compiles with no 
> duplicate type annotation left. Without the backport, 157 of its classes have 
> duplicate {{@NamedParams}} (4,615 duplicates), which it currently strips with 
> a bytecode post-processor.
> Behaviour change to weigh: with the backport, an annotation without 
> {{@Target}} can no longer be placed on a type use. That is what JLS 9.6.4.1 
> requires and what javac enforces, but existing Groovy 5 code could rely on it.
> Would you consider backporting GROOVY-11838 to 5.1.x (and 5.0.x if 
> appropriate)?



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to