Skip to content

Fix AssertThrowsOnLastStatement to preserve parameterized types when extracting arguments.#908

Open
motlin wants to merge 1 commit intoopenrewrite:mainfrom
motlin:AssertThrowsOnLastStatement
Open

Fix AssertThrowsOnLastStatement to preserve parameterized types when extracting arguments.#908
motlin wants to merge 1 commit intoopenrewrite:mainfrom
motlin:AssertThrowsOnLastStatement

Conversation

@motlin
Copy link
Contributor

@motlin motlin commented Feb 5, 2026

What's changed?

Fix AssertThrowsOnLastStatement to preserve parameterized types when extracting method arguments to local variables.

Input code:

assertThrows(Exception.class, () -> {
    doA();
    testThing(Map.of(), List.of(), Set.of("value"));
});

Replacement before fix:

doA();
Map of = Map.of();
List of1 = List.of();
Set of2 = Set.of("value");
assertThrows(Exception.class, () ->
    testThing(of, of1, of2));

Replacement after fix:

doA();
Map<String, String> map = Map.of();
List<String> list = List.of();
Set<String> set = Set.of("value");
assertThrows(Exception.class, () ->
    testThing(map, list, set));

Improvements:

  1. Preserve types (Map to Map<String, String>)
  2. Improve variable names (of, of1, of2 to map, list, and set) derived from method name.

What's your motivation?

When migrating JUnit 4 tests with @Test(expected = ...) that use Map.of(), List.of(), or Set.of() as arguments, the recipe produces code with raw types that causes compilation errors and poor variable names.

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

  • The buildParameterizedTypeName() method constructs type strings including generic parameters
  • The isShortFactoryMethodName() check for of, from, copyOf to derive variable names from return types instead

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

The variable naming improvement could be split out or dropped.

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Comment on lines +236 to +238
} else if (type instanceof JavaType.FullyQualified) {
return ((JavaType.FullyQualified) type).getClassName();
} else if (type instanceof JavaType.Primitive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (type instanceof JavaType.FullyQualified) {
return ((JavaType.FullyQualified) type).getClassName();
} else if (type instanceof JavaType.Primitive) {
}
if (type instanceof JavaType.FullyQualified) {
}
if (type instanceof JavaType.Primitive) {
}
if (type instanceof JavaType.Array) {

return "x";
}

private String buildParameterizedTypeName(JavaType.Parameterized paramType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering, how does it differ from org.openrewrite.java.tree.TypeUtils#toString(org.openrewrite.java.tree.JavaType)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it: TypeUtils.toString() returns fully qualified names (e.g., java.util.Map<java.lang.String, java.lang.String>), which doesn't work together with JavaTemplate string interpolation (#{}) which uses the value literally.

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.

2 participants