Skip to content

Hoist visitor matchers and regexes into private static final constants (K4 of #1330) - #1357

Merged
timtebeek merged 4 commits into
mainfrom
tim/k4-static-matchers
Oct 25, 2026
Merged

timtebeek merged 4 commits into
mainfrom
tim/k4-static-matchers

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Addresses K4 of #1330.

Several recipes rebuilt matchers, FQN strings and regexes for every visitor or every call, and a few constants used camelCase names that read like fields. This moves the option-independent ones into private static final UPPER_SNAKE constants on the recipe class; no recipe behavior changes.

Hoisted matchers and FQNs: JacksonProviderOverrides, AddTableGenerator, AddScopeToInjectedClass, JREThrowableFinalMethods (java.lang.Throwable), AddMockitoJavaAgentToMavenSurefirePlugin (configuration tag template), OptionalNotEmptyToIsPresent (whose isEmpty() matcher was misnamed optionalIsPresentMatcher), OptionalNotPresentToIsEmpty, FindInternalJavaxApis, RemoveTemporalAnnotation (FQNs, type set and do-not-remove map), ArrayStoreExceptionToTypeNotPresentException, IllegalArgumentExceptionToAlreadyConnectedException, and the @lombok.NonNull matcher in LombokUtils. UseJoinColumnForMapping's FQN fields are now static.

Precompiled patterns: LombokUtils, UpgradeDockerImageVersion, UpdateSdkMan, plus RemoveTemporalAnnotation and AddTransientAnnotationToCollections, which had the same per-visitor Pattern.compile. NoGuavaCreateTempDir compared FQNs with String.matches, compiling a regex per call; it now uses equals. UpdateSdkMan's major-version pattern depends on the matched input, so it stays per call.

Renamed constants: URLConstructorsToNewURI, URLConstructorToURICreate, OptionalStreamRecipe and NoGuavaCreateTempDir; the latter's matcher moved to the recipe class so its precondition reuses it.

Option-dependent matchers stay per instance: JREThrowableFinalMethods builds its matchers from constructor options, so they remain locals and drop their constant-style names instead. ReplaceLocalizedStreamMethods already uses camelCase locals built from options, so nothing changed there.

Already fixed on main and skipped: RemovedSecurityManagerMethods, JerseyLoggingFilterToFeature, UpdateAddAnnotatedTypes, UpdateBeanManagerMethods, UpdateGetRealPath, JettyConnectorGetPort and HttpSessionInvalidate already use static final matchers.

Verified with ./gradlew test filtered to the tests of every touched recipe plus org.openrewrite.java.migrate.lombok.* (for LombokUtils): 41 test classes, 492 tests, 0 failures (5 skipped, pre-existing).

Matchers, type patterns and FQN strings that were instance fields of
anonymous visitors or locals in `getVisitor()` are now constants on the
recipe class, so they are built once rather than per visitor. Matchers
built from recipe options stay per instance.
`LombokUtils`, `UpgradeDockerImageVersion`, `UpdateSdkMan`,
`RemoveTemporalAnnotation` and `AddTransientAnnotationToCollections`
compiled constant regexes per call or per visitor. `NoGuavaCreateTempDir`
used `String.matches` to compare fully qualified names, which compiles a
regex on every call; it now uses `equals`.
`static final` matchers in `URLConstructorsToNewURI`,
`URLConstructorToURICreate`, `OptionalStreamRecipe` and
`NoGuavaCreateTempDir` now follow constant naming; the latter's matcher
moves to the recipe class so the precondition reuses it.
`JREThrowableFinalMethods` builds its matchers from recipe options, so
those stay locals and lose their constant-style names instead.
…s-v1

# Conflicts:
#	src/main/java/org/openrewrite/java/migrate/UpgradeDockerImageVersion.java
#	src/main/java/org/openrewrite/java/migrate/javax/RemoveTemporalAnnotation.java
@timtebeek
timtebeek merged commit 4c56260 into main Oct 25, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/k4-static-matchers branch October 25, 2026 12:32
@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