Skip to content

Hoist matchers created per visit to private static final constants - #1225

Merged
timtebeek merged 1 commit into
mainfrom
tim/static-final-matchers
Oct 25, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/static-final-matchers

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Several recipes built a new MethodMatcher or AnnotationMatcher for every visited method, class or annotation, or kept constant matchers as visitor instance fields. Matcher construction parses the signature, so these now live as private static final constants on the recipe class. Existing lower-camel static matcher fields in the same files are renamed to UPPER_SNAKE. Behaviour is unchanged.

What changed:

  • Matchers moved from inside visit methods to static constants: AssertTrueInstanceofToAssertInstanceOf, EnclosedToNested, UpdateBeforeAfterAnnotations (now a static map of JUnit 4 to Jupiter matchers), ReplaceMockitoTestExecutionListener (@ExtendWith), and MockitoJUnitRunnerToExtension. In MockitoJUnitRunnerToExtension the visitor's string fields became static AnnotationMatchers, which are passed to Annotated.Matcher directly.
  • Visitor instance fields moved to static constants: LifecycleNonPrivate, AssertionsArgumentOrder (newListMatchers), ReplaceInitMockToOpenMock (EXCEPTION_CLASS_NAME), ReplaceArquillianInSequenceAnnotation, and the option-independent matchers in HamcrestMatcherToAssertJ and HamcrestNotMatcherToAssertJ.
  • AddMissingNested.hasTestMethods ran FindAnnotations.find once per test annotation for every class declaration, and each call builds a recipe and a matcher. It now walks the class once with a small visitor that checks the annotations against static matchers. This matches the same annotations anywhere in the class subtree, as before.
  • Renamed to UPPER_SNAKE: UseWiremockExtension, AssertionsArgumentOrder, RemoveTimesZeroAndOne.

Not made static:

  • The Hamcrest matchersMatcher depends on the recipe option, so it stays a visitor instance field, which is built once per visitor. In HamcrestNotMatcherToAssertJ it is renamed from MATCHERS_MATCHER to lower camel case, since it isn't a constant.
  • MockitoWhenOnStaticToMockStatic builds its before/after matchers from the annotation found on the setup method. They are now built once per stubbing, before doAfterVisit, instead of once per statement and per method declaration visited by the follow-up visitor.

Verified with ./gradlew test --tests on the test classes of all 15 recipes plus JUnit5MigrationTest, MockitoJUnitToMockitoExtensionTest, MigrateHamcrestToAssertJTest, JunitMockitoUpgradeIntegrationTest and Mockito1to5MigrationTest: 331 tests, 0 failures, 5 skipped.

Fixes item N6 of #1211.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 25, 2026
@timtebeek
timtebeek merged commit ac5b545 into main Oct 25, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/static-final-matchers branch October 25, 2026 12:50
@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