Skip to content

Add UsesMethod and UsesType preconditions to recipes that visit every file - #1227

Merged
timtebeek merged 1 commit into
mainfrom
tim/add-missing-usesmethod-preconditions
Oct 25, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/add-missing-usesmethod-preconditions

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Eleven recipes visited every Java (or Kotlin) source file without a precondition, so they walked whole trees, JUnitTryFailToAssertThatThrownBy every try block, even in files that could never change. The option-driven AssertJ recipes multiply this: SimplifyChainedAssertJAssertion is instantiated about 63 times in YAML and SimplifyAssertJAssertion 8 times.

Each visitor is now wrapped in Preconditions.check(..), reusing the matchers the visitor itself checks, so the gate can't exclude anything the visitor changes:

  • UsesMethod on the existing static matcher: AssertEqualsBooleanToAssertBoolean, AssertFalseNegationToAssertTrue, AssertNotEqualsBooleanToAssertBoolean, AssertThatBooleanToAssertJ.
  • Preconditions.or(UsesMethod..) over the visitor's matchers: JUnitTryFailToAssertThatThrownBy (the four fail(..) matchers) and AssertTrueInstanceofToAssertInstanceOf. The latter built its two matchers on every method invocation; they are now private static final fields.
  • MockConstructionToTryWithResources is gated on the ScopedMock.close() matcher rather than mockConstruction(..). Both of its transformations need a close() call, and removing a redundant close() inside try-with-resources does not need mockConstruction(..) in the file.
  • SimplifyAssertJAssertion gates on its assertToReplace matcher, and SimplifyChainedAssertJAssertion on both its chained-method and assertToReplace matchers. These matchers were already built from the options in getVisitor(), so a null option still gives a matcher that never matches (a no-op, as before) and adds no new failure mode.
  • Type gates where the recipe works on annotations or imports: MinimumJreConditions uses Preconditions.or over UsesType for the four JRE condition annotations, and CleanupKotlinJUnit5AssertionImports uses UsesType on org.junit.jupiter.api.Assertions. I added a Kotlin test to confirm that the type gate still lets through a file whose Assertions import is unused (an unused wildcard import also passed when I checked it locally).

Nothing from the item was skipped.

Verified with ./gradlew test --tests 'org.openrewrite.java.testing.assertj.*' --tests '...cleanup.*' --tests '...hamcrest.*' --tests '...junit5.*' --tests '...junit6.*' --tests '...mockito.*': 1868 tests, 0 failures, 6 skipped. This covers the tests for every touched recipe, the YAML instances of the AssertJ recipes, and the composites that include the others.

Fixes item M1 of #1211.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 25, 2026
@timtebeek
timtebeek force-pushed the tim/add-missing-usesmethod-preconditions branch from 9b2932d to a58536f Compare October 25, 2026 12:48
@timtebeek
timtebeek changed the base branch from main to tim/static-final-matchers October 25, 2026 12:48
Base automatically changed from tim/static-final-matchers to main October 25, 2026 12:50
@timtebeek
timtebeek force-pushed the tim/add-missing-usesmethod-preconditions branch from a58536f to 53b9682 Compare October 25, 2026 12:52
@timtebeek
timtebeek merged commit 4cbed4c into main Oct 25, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/add-missing-usesmethod-preconditions branch October 25, 2026 13:15
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant