Skip to content

Fix UnsupportedOperationException in Py.ExpressionTypeTree.withType()#7314

Merged
sambsnyd merged 1 commit intomainfrom
fix/changetype-withtype-on-method-types
Apr 10, 2026
Merged

Fix UnsupportedOperationException in Py.ExpressionTypeTree.withType()#7314
sambsnyd merged 1 commit intomainfrom
fix/changetype-withtype-on-method-types

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Motivation

Py.ExpressionTypeTree.withType() delegates to the wrapped reference's withType(), which throws UnsupportedOperationException for J.MethodInvocation and J.MethodDeclaration (they require withMethodType() instead).

This caused ChangeType to fail on repos containing Python files, because its postVisit() calls withType() on the ExpressionTypeTree via its catch-all TypedTree branch. Any recipe using ChangeType was affected — e.g., NoGuavaPredicate from the "Java best practices" flagship recipe.

Summary

  • Catch UnsupportedOperationException in ExpressionTypeTree.withType() and return this instead of propagating the exception
  • This is safe because the visitor already handles the inner node's type correctly when visiting the reference directly via visitExpressionTypeTreevisitAndCast(reference)
  • Added unit tests for ExpressionTypeTree.withType() with wrapped MethodInvocation, MethodDeclaration, and Identifier

Test plan

  • New unit tests: ExpressionTypeTreeTest.withTypeOnWrappedMethodInvocation, withTypeOnWrappedMethodDeclaration, withTypeOnWrappedIdentifier
  • Tests fail without the fix, pass with it
  • ChangeTypeTest suite passes (no regressions)
  • rewrite-python test suite passes

…e()`

`ExpressionTypeTree.withType()` delegates to the wrapped reference's
`withType()`, which throws for `J.MethodInvocation` and
`J.MethodDeclaration` (they require `withMethodType()` instead).

This caused `ChangeType` to fail on repos containing Python files,
because its `postVisit()` calls `withType()` on the `ExpressionTypeTree`
via its catch-all `TypedTree` branch. This affected any recipe using
`ChangeType` (e.g., `NoGuavaPredicate`) — 18 errors across
spring-data-commons, spring-data-rest, spring-framework, and
spring-security in flagship recipe runs.

The fix catches `UnsupportedOperationException` in
`ExpressionTypeTree.withType()`. This is safe because the visitor
already handles the inner node's type correctly when visiting the
reference directly.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Apr 8, 2026
@sambsnyd sambsnyd merged commit 0e0eaf0 into main Apr 10, 2026
1 check passed
@sambsnyd sambsnyd deleted the fix/changetype-withtype-on-method-types branch April 10, 2026 00:02
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Apr 10, 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.

2 participants