Skip to content

Fix print idempotency for annotated array type casts#6861

Merged
knutwannheden merged 1 commit intomainfrom
agile-iguana
Mar 3, 2026
Merged

Fix print idempotency for annotated array type casts#6861
knutwannheden merged 1 commit intomainfrom
agile-iguana

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Summary

  • Fix garbled output when parsing annotated array type casts like (@Nullable Object[]) which was printed as (Objectble Object[])
  • When javac represents the cast type as JCArrayTypeTree(JCAnnotatedType(@Nullable, JCIdent(Object))), the arrayTypeTree method stripped the JCAnnotatedType wrapper without collecting its annotations, causing the cursor to advance over annotation text and produce corrupt output
  • The fix collects inner annotations during the tree walk, consumes them as leading annotations before the element type, and wraps in J.AnnotatedType when needed
  • Applied to all parser versions (Java 8, 11, 17, 21, 25)

Test plan

  • Added annotatedArrayTypeCast test for simple cast (@Nullable Object[]) o
  • Added annotatedArrayTypeCastInExpression test matching the original bug report pattern
  • All existing TypeCastTest and AnnotationTest tests pass across all parser versions
  • Full compatibility test suites pass for Java 8, 11, 17, 21, and 25

When javac represents `(@nullable Object[])` as
`JCArrayTypeTree(JCAnnotatedType(@nullable, JCIdent(Object)))`, the
`arrayTypeTree` method stripped the JCAnnotatedType wrapper without
collecting its annotations, causing the cursor to advance over the
annotation text and produce garbled output like `(Objectble Object[])`.

The fix collects annotations from inner JCAnnotatedType wrappers during
the tree walk (guarded by `count > 0` to avoid double-collecting when
called from `visitAnnotatedType`), consumes them as leading annotations
before the element type, and wraps the result in `J.AnnotatedType` when
leading annotations are present.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Mar 3, 2026
@knutwannheden knutwannheden merged commit de4f1d9 into main Mar 3, 2026
1 check passed
@knutwannheden knutwannheden deleted the agile-iguana branch March 3, 2026 21:27
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant