Skip to content

GROOVY-12743: fix TraitComposer crash on closure-member annotations - #3278

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/trait-closure-annotation
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:fix/trait-closure-annotation

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Fixes GROOVY-12743.

Any annotation whose member is given a closure, placed on a trait method with a body, crashed the compiler with UnsupportedOperationException from AbstractList.add when a class implemented the trait. TraitComposer.createForwarderMethod passed Collections.emptyList() as the "not copied" list to GeneralUtils.copyAnnotatedNodeAnnotations, which adds every annotation with a closure member to that list (the closure is still a ClosureExpression in canonicalization; it becomes a class only at class generation). TraitASTTransformation.createInterfaceStaticForwarder already passes a real list.

The forwarder path now passes a real list as well. Behaviour is otherwise unchanged: the annotation stays on the trait method and the helper method and is, as before for closure members, not copied to the forwarder.

The groovy-contracts annotations @ThrowsIf and @Decreases on a trait method hit this crash first; whether they can be enforced on a trait method is a separate question that GROOVY-12741 answers by rejecting them for now.

Tests: a new test in TraitASTTransformationTest compiles a trait method carrying a closure-member annotation and checks the annotation on the trait and helper methods and its absence on the forwarder; it fails on master with the exception above. TraitASTTransformationTest passes with the fix (294 tests), and so does the groovy-contracts module.

createForwarderMethod passed Collections.emptyList() as the "not copied"
list to GeneralUtils.copyAnnotatedNodeAnnotations, which adds every
annotation with a closure member to that list, so any such annotation on
a trait method with a body threw UnsupportedOperationException when a
class implemented the trait. It now passes a real list, as
createInterfaceStaticForwarder already does; the annotation stays on the
trait method and the helper method and is, as before, not copied to the
forwarder.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.4115%. Comparing base (df1fe66) to head (b52a20c).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3278        +/-   ##
==================================================
- Coverage     74.4140%   74.4115%   -0.0025%     
- Complexity      44379      44395        +16     
==================================================
  Files            1699       1699                
  Lines          151067     151150        +83     
  Branches        28790      28799         +9     
==================================================
+ Hits           112415     112473        +58     
- Misses          28380      28393        +13     
- Partials        10272      10284        +12     
Files with missing lines Coverage Δ
...codehaus/groovy/transform/trait/TraitComposer.java 82.3151% <100.0000%> (ø)

... and 21 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.

@testlens-app

testlens-app Bot commented Nov 8, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: b52a20c
▶️ Tests: 117482 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

No deployments
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.

2 participants