Skip to content

Add recipe to avoid repeated Pattern.compile calls - #1005

Open
raj7-dev wants to merge 2 commits into
openrewrite:mainfrom
raj7-dev:fix-622-repeated-pattern-compile
Open

Add recipe to avoid repeated Pattern.compile calls#1005
raj7-dev wants to merge 2 commits into
openrewrite:mainfrom
raj7-dev:fix-622-repeated-pattern-compile

Conversation

@raj7-dev

@raj7-dev raj7-dev commented Aug 14, 2026

Copy link
Copy Markdown

What's changed?

Adds a new AvoidRepeatedPatternCompile recipe that moves method-local Pattern.compile() calls with constant regex literals into reusable private static final Pattern fields.

The current implementation:

  • Handles Pattern.compile(String) with literal regexes.
  • Handles Pattern.compile(String, int) when the flags are constant.
  • Does not change dynamically constructed regexes.
  • Does not change dynamic flags.
  • Uses variable type information when replacing references.
  • Handles multiple patterns in the same class.
  • Avoids generated field-name collisions.

Unit tests cover both positive and negative cases.

What's your motivation?

Repeatedly calling Pattern.compile() for the same constant regular expression recompiles the regex on each method invocation.

This recipe moves such patterns to reusable class-level constants.

Anything in particular you'd like reviewers to focus on?

I'd particularly appreciate feedback on:

  • Whether the initial scope of supporting literal regexes is appropriate.
  • Whether support for other hard-coded constant expressions should be included in this recipe.
  • The generated constant naming strategy.
  • Whether there are additional Pattern.compile() cases that should be handled.

Anyone you would like to review specifically?

No specific reviewer.

Have you considered any alternatives or workarounds?

The initial implementation intentionally keeps the detection conservative rather than attempting to evaluate arbitrary expressions.

Dynamic regexes such as:

Pattern.compile(regex);

or:

Pattern.compile(createRegex());

are left unchanged.

This avoids moving expressions to class scope when their value depends on runtime state.

Any additional context

Recipe-specific tests pass locally:

./gradlew test --tests "*AvoidRepeatedPatternCompileTest"

7 tests pass successfully.

I also ran:

./gradlew build

and generated the required recipes.csv entry.

The full repository test suite currently fails locally in unrelated RPC tests. Existing Python tests attempt to install openrewrite==8.90.0, which is unavailable from the configured Python package index.

These failures are unrelated to AvoidRepeatedPatternCompile.

Checklist

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Aug 14, 2026
@raj7-dev
raj7-dev marked this pull request as ready for review August 15, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Recipe to avoid calling Pattern.compile() too many times

1 participant