Fix UnsupportedOperationException in Py.ExpressionTypeTree.withType()#7314
Merged
Fix UnsupportedOperationException in Py.ExpressionTypeTree.withType()#7314
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Py.ExpressionTypeTree.withType()delegates to the wrapped reference'swithType(), which throwsUnsupportedOperationExceptionforJ.MethodInvocationandJ.MethodDeclaration(they requirewithMethodType()instead).This caused
ChangeTypeto fail on repos containing Python files, because itspostVisit()callswithType()on theExpressionTypeTreevia its catch-allTypedTreebranch. Any recipe usingChangeTypewas affected — e.g.,NoGuavaPredicatefrom the "Java best practices" flagship recipe.Summary
UnsupportedOperationExceptioninExpressionTypeTree.withType()and returnthisinstead of propagating the exceptionvisitExpressionTypeTree→visitAndCast(reference)ExpressionTypeTree.withType()with wrappedMethodInvocation,MethodDeclaration, andIdentifierTest plan
ExpressionTypeTreeTest.withTypeOnWrappedMethodInvocation,withTypeOnWrappedMethodDeclaration,withTypeOnWrappedIdentifierChangeTypeTestsuite passes (no regressions)rewrite-pythontest suite passes