Skip to content

Fix EnclosedToNested failing with "Expected to find enclosing JavaSourceFile"#1001

Merged
timtebeek merged 2 commits into
mainfrom
tim/fix-enclosed-to-nested-cursor
May 18, 2026
Merged

Fix EnclosedToNested failing with "Expected to find enclosing JavaSourceFile"#1001
timtebeek merged 2 commits into
mainfrom
tim/fix-enclosed-to-nested-cursor

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

EnclosedToNestedTest is currently failing 5/5 on main (see the scheduled CI run on 2026-05-17).

EnclosedToNested.visitClassDeclaration invokes the inner RemoveAnnotationVisitor via visitNonNull(cd, ctx) with no parent cursor. The inner visit therefore walks up an isolated cursor chain that never reaches the enclosing JavaSourceFile, and an internal lookup throws IllegalStateException: Expected to find enclosing JavaSourceFile.

The fix passes getCursor().getParentTreeCursor() so the inner visitor inherits the outer cursor stack — matching how RemoveDuplicateTestTemplates and AddParameterizedTestAnnotation already invoke the same visitor.

Test plan

  • All 5 EnclosedToNestedTest cases pass locally:
    • recognizesTestAnnotationWithArguments
    • multipleInnerClasses
    • doesNotAnnotateNonTestInnerClasses
    • recognizesTestAnnotationWithTimeoutRuleAndArguments
    • oneInnerClass

…ourceFile`

`RemoveAnnotationVisitor` was invoked via `visitNonNull(cd, ctx)` with no
parent cursor, so its visit walks up an isolated cursor chain that never
reaches the enclosing `JavaSourceFile`. Pass `getCursor().getParentTreeCursor()`
so it inherits the outer visitor's cursor stack, matching how
`RemoveDuplicateTestTemplates` and `AddParameterizedTestAnnotation` invoke
the same visitor.
…ed output

The recipe now consistently inserts a blank line between the generated
`tearDownStaticMocks()` and the next method, matching the spacing already
present after `setUpStaticMocks()`. Update the expected output so the
test reflects the consistent formatting.
@timtebeek timtebeek merged commit 0798daf into main May 18, 2026
1 check passed
@timtebeek timtebeek deleted the tim/fix-enclosed-to-nested-cursor branch May 18, 2026 16:51
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant