Skip to content

⚡ Bolt: Replace regex-based replaceFirst with indexOf/substring#291

Merged
RoiSoleil merged 2 commits into
masterfrom
bolt-replaceFirst-to-indexOf-852307941229828869
May 22, 2026
Merged

⚡ Bolt: Replace regex-based replaceFirst with indexOf/substring#291
RoiSoleil merged 2 commits into
masterfrom
bolt-replaceFirst-to-indexOf-852307941229828869

Conversation

@RoiSoleil
Copy link
Copy Markdown
Contributor

💡 What:
Replaced regex-based String.replaceFirst() with direct substring operations (startsWith, indexOf, and substring) in the following files:

  • FilterMethodVisitor.java
  • TestMethodDivinerJunit3Praefix.java
  • TestMethodDivinerNoPraefix.java

🎯 Why:
String.replaceFirst() compiles a regular expression under the hood, which creates significant execution overhead for simple prefix/suffix and literal replacements. Since FilterMethodVisitor frequently processes AST structures, this optimization speeds up method filtering and renaming operations. Additionally, it eliminates edge cases where literal string contents could be mistakenly parsed as regex capture groups or metacharacters.

📊 Impact:
Eliminates regular expression overhead, improving string manipulation speed across the AST visitors and test diviner utilities.

🔬 Measurement:
The correctness of the logic is proven by passing the test suite: xvfb-run -a mvn -f org.moreunit.build/pom.xml test.


PR created automatically by Jules for task 852307941229828869 started by @RoiSoleil

**What:**
Replaced `String.replaceFirst()` with `startsWith()` and `indexOf()` combined with `substring()` in `FilterMethodVisitor.java`, `TestMethodDivinerJunit3Praefix.java`, and `TestMethodDivinerNoPraefix.java`.

**Why:**
`String.replaceFirst()` implicitly compiles a regex `Pattern` and invokes the matcher logic, which is slow for simple literal substring removal. Given that `FilterMethodVisitor` frequently parses AST structures and processes string names, this regex overhead was unnecessary. Literal substring removal is far faster.

**Impact:**
Improves execution performance within plugin string parsers and resolves potential bugs where literal regex characters (e.g. `$`) might cause exceptions.

**Measurement:**
Run `xvfb-run -a mvn -f org.moreunit.build/pom.xml test` to verify changes build successfully.

Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.67%. Comparing base (dcfcc6a) to head (6a389ba).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...src/org/moreunit/elements/FilterMethodVisitor.java 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #291      +/-   ##
============================================
+ Coverage     75.58%   75.67%   +0.09%     
- Complexity     3362     3378      +16     
============================================
  Files           427      428       +1     
  Lines         14759    14827      +68     
  Branches       1279     1288       +9     
============================================
+ Hits          11155    11221      +66     
- Misses         3067     3068       +1     
- Partials        537      538       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

**What:**
Added unit tests covering the `index == -1` (no match) edge cases in `TestMethodDivinerNoPraefixTest` and `TestMethodDivinerJunit3PraefixTest`.

**Why:**
The previous refactoring from `replaceFirst` to `indexOf` introduced a new `if (index != -1)` branch which was not covered by existing tests, causing the Codecov CI check to fail due to a drop in diff coverage.

**Impact:**
Increases code coverage and ensures that renaming methods correctly fallback when the expected prefix/suffix string does not exist in the target string.

Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
@RoiSoleil RoiSoleil merged commit c45130b into master May 22, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant