Skip to content

Support annotations as members of annotations - #499

Open
igel-devin-ai wants to merge 8 commits into
java-helpers:mainfrom
igel-devin-ai:devin/494-nested-annotation-members
Open

igel-devin-ai wants to merge 8 commits into
java-helpers:mainfrom
igel-devin-ai:devin/494-nested-annotation-members

Conversation

@igel-devin-ai

Copy link
Copy Markdown
Contributor

Fixes #494 — annotations as members of annotations: @OuterMeta(inner = @InnerMeta(text = "x")).

  • AnnotationMember: new Kind.ANNOTATION carrying a nested AnnotationDto. The deriving constructor sets the simple-name source expression and uses the annotation type as referenced type — so import collection and collision qualification come for free. equals/hashCode extended
  • Helpers: AnnotationDto.addMemberAnnotation(name, dto) and builder addMemberAnnotation(key, a -> a.annotationType(...)...) in consumer style, matching the other helpers
  • Rendering: recursive in RoasterMapper — the nested annotation renders through the same mapAnnotation path, so typed members inside it (class literals, enum constants) get collision-qualified too
  • Imports: ImportCollector recurses into nested annotations
  • Tests: plain nested member, typed members inside nested annotations, collision qualification of the nested annotation type — 31/31 mapper tests green, full reactor 1,262 green

Design note: annotation arrays (inner = {@A, @B}) are expressible today by combining RAW array braces with nested members per element in the common case; a dedicated array kind can follow if real usage demands it.

- AnnotationMember: new Kind.ANNOTATION carrying a nested
  AnnotationDto; constructor derives the simple-name source expression
  and referenced type from it; equals/hashCode extended
- AnnotationDto.addMemberAnnotation + builder helper with consumer
  style for the nested annotation
- rendering: nested annotations render recursively in RoasterMapper,
  inheriting collision qualification of the annotation type and typed
  members
- ImportCollector recurses into nested annotations for import
  collection
- tests: plain nested member, typed members inside nested annotation,
  collision qualification of the nested annotation type
@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@igel-devin-ai

igel-devin-ai commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Strict review

changes needed

Reviewed head 0fc863041f98dd1be1ddd42009ebffa96c7b29ea against upstream/main 4b696141058c97b8130c3ce3920a7faeafbb3bbd, including issue #494 and both PR comment streams. Existing comments are bot reports, not the findings below. The current diff correctly implements the feature on the renamed shared ValueExpression model, despite older AnnotationMember terminology in the body.

  1. minor — verified — codegen-core/src/main/java/org/javahelpers/codegen/model/expression/ValueExpression.java:116–120; codegen-roaster/src/main/java/org/javahelpers/codegen/roaster/RoasterMapper.java:261–263: the new kind is accepted without its required payload. The public text constructors accept Kind.ANNOTATION but unconditionally set annotation = null. Passing the resulting expression to mapValueExpression dispatches to mapAnnotation(null, ...) and throws NullPointerException. The structured constructor works; the inconsistent state is reachable through the existing public (String, Kind) and (String, TypeName, Kind) constructors, whose documented contracts do not exclude the new kind. Reject ANNOTATION in those constructors with a precise argument error, or redesign construction so every annotation-kind expression necessarily contains an AnnotationDto. Add a constructor invariant/negative-path test. Probe output: Kind.ANNOTATION via public text constructor: NullPointerException: Cannot invoke "org.javahelpers.codegen.model.annotation.AnnotationDto.getAnnotationType()" because "annotationDto" is null.

  2. minor — verified by test/code inspection — codegen-core/src/main/java/org/javahelpers/codegen/util/ImportCollector.java:109–119; codegen-roaster/src/test/java/org/javahelpers/codegen/roaster/RoasterMapperTest.java:333–375: the acceptance-critical import recursion has no regression test. All three added tests call RoasterMapper.mapAnnotation directly and supply the collision set themselves. They never invoke ImportCollector, generate a compilation unit, or compile the result. Consequently reverting the collector recursion would leave these new tests green while losing imports for typed members inside nested annotations. Add a full generated-source text-block comparison and compilation test, preferably two levels deep with enum/class members and a real type-name collision, plus core tests for recursive import collection. Also cover the newly extended equality/hashCode and copy paths: different nested member values must remain distinguishable and a no-op copy must preserve them. These are coverage gaps, not a claim that ordinary rendering currently fails: an independent generation-and-compilation probe with nested enum/class members passed with 0 warnings and 0 errors.

  3. minor — verified — codegen-roaster/src/test/java/org/javahelpers/codegen/roaster/RoasterMapperTest.java:344,361,374: expected Java source uses ordinary escaped strings rather than text blocks. Convert all three new expected annotation expressions to text blocks, with trailing-newline handling as needed. There is no new + concatenation in the added tests, but the expected-source strings still do not follow the required text-block convention.

Verification and scope

  • Java 17 / Maven 3.9.11: mvn -q install -pl codegen-core,codegen-roaster -am -Dfmt.skip=true exited 0. Surefire XML totals: 474 tests, 0 failures, 0 errors, 0 skipped. Formatting was skipped to keep this review read-only; no source file was modified.
  • External text-block probe generated @OuterMeta(inner = @InnerMeta(mode = RetentionPolicy.RUNTIME, type = Instant.class)), collected the InnerMeta, OuterMeta, RetentionPolicy, and Instant imports, and compiled it against real annotation declarations: SUCCESS, warnings=0, errors=0.
  • git_pr_checks: 6 passed, 0 failed, 0 pending. Retrieved JDK 25 job 145858099968 using git_ci_job_logs; Maven reports BUILD SUCCESS. Sonar API reports OK, new coverage 87.5%.
  • This changes codegen annotation rendering, not simple-builder processor output; no committed example-builder drift was introduced. No release-note/API migration requirement was identified beyond the constructor invariant above.
  • A dedicated annotation-array representation remains deferred. Issue Support annotations as members of annotations (nested annotation values) #494 explicitly makes that kind optional, so its absence is not treated as a blocker for single nested values. Raw arrays do not provide structured per-element import handling.

Other-PR integration review

Read bodies/file lists for #430, #480, #482, #483, #484, #492, #497, #498, and #500. Read-only pairwise merge-tree checks find one textual conflict: #480, codegen-roaster/src/test/java/org/javahelpers/codegen/roaster/RoasterMapperTest.java. Preserve both #480's nested-name collision tests and these recursive annotation cases. The production changes to ImportCollector/RoasterMapper merge textually; that does not establish combined collision correctness, which is another reason for a real collector/generator regression test. The other eight pairwise merges are clean. No combined branch build was performed.

Written by Devin


Recheck at afeee541 (2026-12-06)

Verdict: minor changes

# Original finding (short) Status Remaining action
1 ANNOTATION without payload fixed —
2 Recursive annotation regression coverage partially fixed — minor Compile nested annotation output; test no-op copy (RoasterCodeGeneratorTest.java:872–906).
3 Expected-expression text blocks fixed —

Build: Java 17 module install — BUILD SUCCESS; 479 tests, 0 failures/errors; recursive-annotation probe passes without warnings.


Recheck at 2a8e3fa2 (2026-12-06)

Verdict: ready

Severity What is missing Where (file:line)
— None. —

…cursion/equality regression tests, text blocks
… review)

- Regression: generated nested annotation usage compiles against real annotation
  definitions (OuterMeta/InnerMeta/DeepMeta stubs), verified with the JDK compiler
- No-op copy test: copying an AnnotationDto with nested members through the builder
  reproduces an equal instance, guarding the copy path the With-pattern relies on
@igel-devin-ai

Copy link
Copy Markdown
Contributor Author

Recheck items addressed (finding 2 completed):

  • Compile nested annotation output: nestedAnnotationOutputCompiles writes the generated Target plus real OuterMeta/InnerMeta/DeepMeta definitions and compiles them together with the JDK compiler — the recursive member chain is now compile-verified, not just text-matched
  • No-op copy test: builderCopyPreservesNestedAnnotationMembers copies an annotation with nested members through the builder unchanged and asserts equality, guarding the copy path

@sonarqubecloud

sonarqubecloud Bot commented Dec 6, 2026

Copy link
Copy Markdown

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.

Support annotations as members of annotations (nested annotation values)

1 participant