Skip to content

Fix parsing of type-use annotations on qualified types in array declarations#7289

Merged
steve-aom-elliott merged 1 commit intomainfrom
fix/parse-type-use-annotations-on-qualified-array-types
Apr 6, 2026
Merged

Fix parsing of type-use annotations on qualified types in array declarations#7289
steve-aom-elliott merged 1 commit intomainfrom
fix/parse-type-use-annotations-on-qualified-array-types

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

Summary

  • Fix arrayTypeTree() in all Java parser versions (8, 11, 17, 21, 25) to use annotatedTypeTree() instead of convert() when there are unconsumed annotations and the element type is a JCFieldAccess
  • Type-use annotations on qualified types within array declarations (e.g., Outer.@Nullable Inner[]) were being consumed as whitespace instead of being represented as proper AST nodes
  • This also fixes RemoveUnusedImports incorrectly removing imports for annotations used in this position

Context

The best practices recipe run on openrewrite/rewrite-migrate-java revealed this: the @Nullable import was removed from MoveAnnotationToArrayType.java which uses J.@Nullable Annotation[]. The root cause is the parser burying the annotation in whitespace, so typesInUse never includes the annotation type.

The bug is in arrayTypeTree(): after unwrapping JCAnnotatedType and recording the annotation positions, it calls convert(typeIdent) for the JCFieldAccess. This dispatches to visitMemberSelect, which doesn't know about annotation positions and eats @Nullable as whitespace via sourceBefore(). The fix routes through annotatedTypeTree() instead, which already knows how to place annotations on the J.Identifier within a J.FieldAccess chain.

Test plan

  • New test RemoveUnusedImportsTest.retainTypeUseAnnotationOnQualifiedArrayType verifies Outer.@Nullable Inner[] retains the annotation import
  • All existing RemoveUnusedImportsTest, AnnotationTest, ArrayTypeTest, OrderImportsTest, and Java 21 module tests pass

…rations

Type-use annotations on qualified types within array declarations (e.g.,
`Outer.@nullable Inner[]`) were being consumed as whitespace instead of
being represented as proper AST nodes. This happened because
`arrayTypeTree()` correctly unwrapped `JCAnnotatedType` and recorded
annotations in the position table, but then called `convert(typeIdent)`
for the underlying `JCFieldAccess`. This dispatched to
`visitMemberSelect`, which doesn't know about the annotation position
table and consumed the annotation text as whitespace via
`sourceBefore()`.

The fix uses `annotatedTypeTree()` instead of `convert()` when there are
unconsumed annotations and the element type is a `JCFieldAccess`. The
`annotatedTypeTree` method already handles interleaving annotations into
the `J.FieldAccess` chain by calling `leadingAnnotations()` between the
dot and the identifier name.

This also fixes `RemoveUnusedImports` incorrectly removing imports for
type-use annotations in this position, since the annotation type was
never added to `typesInUse` when it was buried in whitespace.
@steve-aom-elliott steve-aom-elliott force-pushed the fix/parse-type-use-annotations-on-qualified-array-types branch from 17f38f0 to 4935d3b Compare April 6, 2026 20:50
@steve-aom-elliott steve-aom-elliott added the bug Something isn't working label Apr 6, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Apr 6, 2026
@steve-aom-elliott steve-aom-elliott marked this pull request as ready for review April 6, 2026 21:02
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find and minimal fix!

@steve-aom-elliott steve-aom-elliott merged commit 456976e into main Apr 6, 2026
1 check passed
@steve-aom-elliott steve-aom-elliott deleted the fix/parse-type-use-annotations-on-qualified-array-types branch April 6, 2026 21:31
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants