Skip to content

Fix AddTimeUnitArgument crash on arithmetic argument expressions#139

Merged
timtebeek merged 4 commits into
mainfrom
fix/add-time-unit-argument-arithmetic
Jul 23, 2026
Merged

Fix AddTimeUnitArgument crash on arithmetic argument expressions#139
timtebeek merged 4 commits into
mainfrom
fix/add-time-unit-argument-arithmetic

Conversation

@timtebeek

@timtebeek timtebeek commented Jul 23, 2026

Copy link
Copy Markdown
Member

Problem

AddTimeUnitArgument (invoked by UpgradeApacheHttpClient_5_TimeUnit) throws IllegalArgumentException: Could not parse as Java when the timeout argument is a binary/arithmetic expression such as 60 * 1000:

RequestConfig.custom()
    .setConnectionRequestTimeout(60 * 1000) // arithmetic expression
    .build();

Plain integer literals and variables were handled fine — only binary expressions crashed.

Root cause

The recipe rebuilt the whole argument list through a contextSensitive() JavaTemplate of the form #{any()}, TimeUnit.#{} with replaceArguments(). Re-parsing the original argument inside the synthetic template stub placed the __TEMPLATE_STOP__ boundary incorrectly for binary expressions, producing unparseable Java.

Fix

Append the TimeUnit argument to the existing argument LST instead of re-templating all arguments via #{any()}. The original expression is never re-parsed. The invocation's method type is extended with the new TimeUnit parameter so the recipe stays idempotent (the matcher no longer matches after the argument is added).

Tests

Added arithmeticTimeoutArgument reproducing the reported crash; all existing AddTimeUnitArgumentTest and httpclient5 tests still pass.

Rebuilding the entire argument list through a #{any()} JavaTemplate
re-parsed the original argument, which misplaced the __TEMPLATE_STOP__
boundary for binary/arithmetic expressions (e.g. 60 * 1000) and produced
unparseable Java. Instead, append the TimeUnit argument to the existing
argument LST and extend the method type so the original expression is
never re-parsed.

Fixes #138
@timtebeek
timtebeek merged commit 109b9c1 into main Jul 23, 2026
1 check passed
@timtebeek
timtebeek deleted the fix/add-time-unit-argument-arithmetic branch July 23, 2026 10:30
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

AddTimeUnitArgument fails with "Could not parse as Java" when the timeout argument is an arithmetic expression (e.g. 60 * 1000)

1 participant