Skip to content

RemoveRedundantTypeCast: keep casts on signature-polymorphic methods - #1025

Merged
timtebeek merged 2 commits into
mainfrom
tim/issue-1024
Aug 24, 2026
Merged

RemoveRedundantTypeCast: keep casts on signature-polymorphic methods#1025
timtebeek merged 2 commits into
mainfrom
tim/issue-1024

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 24, 2026

Copy link
Copy Markdown
Member

MethodHandle#invoke / invokeExact and the VarHandle accessors are signature-polymorphic (JLS 15.12.3): the call site has no fixed signature, and the enclosing cast is what supplies the return type. The recipe compared the attributed expression type against the cast type, but that attributed type only exists because of the cast, so the check always succeeded and the cast was always removed — leaving code that no longer compiles:

error: incompatible types: Object cannot be converted to String
        return handle.invoke();

Now a cast whose expression is a method invocation declared on java.lang.invoke.MethodHandle or java.lang.invoke.VarHandle is preserved.

MethodHandle#invoke/invokeExact and the VarHandle accessors are
signature-polymorphic (JLS 15.12.3): the enclosing cast is what gives the
call site its return type, so removing it reverts the call to Object and
breaks compilation.

Fixes #1024
@timtebeek
timtebeek merged commit 2d03845 into main Aug 24, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/issue-1024 branch August 24, 2026 19:23
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

RemoveRedundantTypeCast removes the cast that gives a MethodHandle#invoke call its return type

1 participant