Skip to content

Leave AssertThrowsOnLastStatement arguments inline when their type can't be rendered#1066

Merged
timtebeek merged 2 commits into
mainfrom
fix/assertthrows-anonymous-class-inline
Jul 24, 2026
Merged

Leave AssertThrowsOnLastStatement arguments inline when their type can't be rendered#1066
timtebeek merged 2 commits into
mainfrom
fix/assertthrows-anonymous-class-inline

Conversation

@timtebeek

@timtebeek timtebeek commented Jul 24, 2026

Copy link
Copy Markdown
Member

What's changed?

AssertThrowsOnLastStatement intermittently crashed with:

Expected a template that would generate exactly one statement to replace one statement, but generated N. Template: #{} #{} = #{any()};

Root cause

  • When hoisting a last-statement argument into a preceding local, the recipe renders the argument's type into the declared type of #{} #{} = #{any()};. For an anonymous or local class the type is a fully-attributed JavaType.Class (not JavaType.Unknown), so the Fix AssertThrowsOnLastStatement crash on unattributed argument types #1059 guard (Unknown/None/Void/Null) doesn't catch it. getClassName() then produces invalid Java such as:
MyTest.1 x = __P__.<java.util.concurrent.Callable<java.lang.String>>p();

MyTest.1 doesn't parse as a type, so the stub yields the wrong statement count and the run fails. This only surfaced in real multi-module sources, which is why it was hard to isolate.

Fix

Guard the template apply so any failure leaves the argument inline instead of failing the whole recipe run — a conservative fallback that also covers other un-renderable-type variants. The recipe still moves the earlier lambda statements out; only the un-renderable argument stays inline.

Checklist

  • Regression test (argumentWithAnonymousClassTypeIsLeftInline) added and passing

  • Full AssertThrowsOnLastStatementTest passes

  • licenseFormat clean

  • Fixes moderneinc/customer-requests#2861

…n't be rendered

Anonymous and local class types (proper JavaType.Class, not JavaType.Unknown)
render an invalid declared type such as `MyTest.1 x = ...`, which makes the
extraction template generate the wrong number of statements and crashes the run.
The #1059 guard only covered Unknown/None/Void/Null. Guard the template apply so
any such failure leaves the argument inline instead of failing the recipe.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 24, 2026
@timtebeek
timtebeek marked this pull request as ready for review July 24, 2026 16:30
@timtebeek
timtebeek merged commit 8cfaee5 into main Jul 24, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 24, 2026
@timtebeek
timtebeek deleted the fix/assertthrows-anonymous-class-inline branch July 24, 2026 16:38
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.

1 participant