Fix parsing of type-use annotations on qualified types in array declarations#7289
Merged
steve-aom-elliott merged 1 commit intomainfrom Apr 6, 2026
Conversation
…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.
17f38f0 to
4935d3b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
arrayTypeTree()in all Java parser versions (8, 11, 17, 21, 25) to useannotatedTypeTree()instead ofconvert()when there are unconsumed annotations and the element type is aJCFieldAccessOuter.@Nullable Inner[]) were being consumed as whitespace instead of being represented as proper AST nodesRemoveUnusedImportsincorrectly removing imports for annotations used in this positionContext
The best practices recipe run on
openrewrite/rewrite-migrate-javarevealed this: the@Nullableimport was removed fromMoveAnnotationToArrayType.javawhich usesJ.@Nullable Annotation[]. The root cause is the parser burying the annotation in whitespace, sotypesInUsenever includes the annotation type.The bug is in
arrayTypeTree(): after unwrappingJCAnnotatedTypeand recording the annotation positions, it callsconvert(typeIdent)for theJCFieldAccess. This dispatches tovisitMemberSelect, which doesn't know about annotation positions and eats@Nullableas whitespace viasourceBefore(). The fix routes throughannotatedTypeTree()instead, which already knows how to place annotations on theJ.Identifierwithin aJ.FieldAccesschain.Test plan
RemoveUnusedImportsTest.retainTypeUseAnnotationOnQualifiedArrayTypeverifiesOuter.@Nullable Inner[]retains the annotation importRemoveUnusedImportsTest,AnnotationTest,ArrayTypeTest,OrderImportsTest, and Java 21 module tests pass