Matteo Fiandesio created GROOVY-12787:
-----------------------------------------
Summary: @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.1.3, 5.0.8
Reporter: Matteo Fiandesio
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)