Skip to content

GROOVY-12787: backport GROOVY-11838 to GROOVY_5_1_X - #3352

Closed
swampie wants to merge 2 commits into
apache:GROOVY_5_1_Xfrom
swampie:GROOVY-12787
Closed

swampie wants to merge 2 commits into
apache:GROOVY_5_1_Xfrom
swampie:GROOVY-12787

Conversation

@swampie

@swampie swampie commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Backport of 71b6eb9 (GROOVY-11838, already on master) to GROOVY_5_1_X, to fix GROOVY-12787.

On 5.x, a compilation unit with several @MapConstructor classes gives each generated Map constructor duplicate @NamedParams type annotations. Reflecting on the constructor throws AnnotationFormatError, and the @NamedParams it reports can list another class's parameters. GROOVY-11838 makes an annotation without @Target apply to no type contexts (JLS 9.6.4.1), so @NamedParams is no longer copied onto the shared Map type. 4.0.x and 6.0.x are not affected.

Commits:

  • 749835f: the GROOVY-11838 change, cherry-picked. AnnotationNode is identical to master. AnnotationNodeTest does not exist on this branch, so the commit creates it with master's testIsTargetAllowed method (JUnit 5).
  • 4fed239: adds MapConstructorTransformTest.testNamedParamsAreNotCopiedOntoSharedMapType for GROOVY-12787. It fails without the fix (AnnotationFormatError: Duplicate annotation ... NamedParams) and passes with it.

Testing:

  • ./gradlew test: 16,616 tests, 0 failures, 79 skipped.
  • The reproducer from GROOVY-12787 fails on 5.1.3 and passes with this change.
  • On an application of about 8,500 classes compiled with the 5.1.4 snapshot, this removes all remaining duplicate type annotations (157 classes before).

Behaviour change, same as on master: annotations without @Target are no longer accepted in type-use positions.

The GROOVY-12787 regression test is new and is not on master. Should I open a separate pull request adding it to master too?

@swampie
swampie marked this pull request as draft November 23, 2026 15:23
@swampie
swampie marked this pull request as ready for review November 23, 2026 16:25
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.3644%. Comparing base (796f904) to head (4fed239).

Additional details and impacted files

Impacted file tree graph

@@                  Coverage Diff                   @@
##             GROOVY_5_1_X      #3352        +/-   ##
======================================================
- Coverage         67.3669%   67.3644%   -0.0025%     
+ Complexity          29767      29763         -4     
======================================================
  Files                1384       1384                
  Lines              117384     117384                
  Branches            20677      20677                
======================================================
- Hits                79078      79075         -3     
- Misses              31742      31748         +6     
+ Partials             6564       6561         -3     
Files with missing lines Coverage Δ
...n/java/org/codehaus/groovy/ast/AnnotationNode.java 76.4706% <100.0000%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@paulk-asert

paulk-asert commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 (<51e10da4ff>). 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants