Skip to content

Preserve cast on generic method invocation in overloaded outer call#891

Merged
timtebeek merged 2 commits into
mainfrom
tim/cast-generic-method-overload
May 16, 2026
Merged

Preserve cast on generic method invocation in overloaded outer call#891
timtebeek merged 2 commits into
mainfrom
tim/cast-generic-method-overload

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

  • RemoveRedundantTypeCast stripped (Object) from sb.append((Object) cursor.getValue()) where Cursor.getValue() is <T> T getValue(), breaking compilation because StringBuilder.append has many single-arg overloads and the free generic return makes resolution ambiguous.
  • The LST resolves the invocation's return to its bound (Object), so the existing castType.equals(expressionType) shortcut took over and the hasMethodOverloading check on the parent call never ran.
  • Distinct from Detect overloads on parameterized receiver in hasMethodOverloading #890 (which widens hasMethodOverloading for parameterized receivers and null operands) — here the receiver is StringBuilder (already a JavaType.Class) and the operand is a generic method call.

Test plan

  • New regression test RemoveRedundantTypeCastTest.doNotRemoveObjectCastOnGenericMethodCallWithOverloads modeled on the failing rewrite-analysis shape.
  • ./gradlew test --tests RemoveRedundantTypeCastTest --tests LambdaBlockToExpressionTest passes.

When a cast wraps a generic method invocation whose declared return is a
type variable (e.g. `(Object) cursor.getValue()` where `<T> T getValue()`),
the cast pins the inferred return type so an overloaded outer call like
`StringBuilder.append` resolves unambiguously. The LST resolves the
invocation's return to its bound (Object), so the existing
`castType.equals(expressionType)` shortcut dropped the cast before the
`hasMethodOverloading` check could run. Detect the generic return on the
declaring class and preserve the cast when the parent call is overloaded.
Dedupe the declaring-type method-loop between the new MethodCall branch
and the existing Lambda-body branch. Also hoist the cheap
`hasMethodOverloading` check above the loop so non-overloaded parent
calls short-circuit without scanning the declaring type's methods.
@timtebeek timtebeek merged commit d99ed6f into main May 16, 2026
1 check passed
@timtebeek timtebeek deleted the tim/cast-generic-method-overload branch May 16, 2026 11:28
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 16, 2026
mergify Bot added a commit to robfrank/linklift that referenced this pull request Jun 4, 2026
… 2.34.0 to 2.36.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-static-analysis](https://github.com/openrewrite/rewrite-static-analysis) from 2.34.0 to 2.36.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-static-analysis's releases](https://github.com/openrewrite/rewrite-static-analysis/releases).*

> 2.36.0
> ------
>
> What's Changed
> --------------
>
> * Guard `instanceof` to `Class::isInstance` conversion on lambda arity by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#903](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/903)
> * Preserve cast on argument to overloaded method in `RemoveRedundantTypeCast` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#904](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/904)
> * Preserve raw cast on parameterized expression in `RemoveRedundantTypeCast` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#905](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/905)
> * Skip Python sources in `CombineSemanticallyEqualCatchBlocks` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#908](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/908)
> * Use receiver type for unbound instance method reference in `ReplaceLambdaWithMethodReference` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#909](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/909)
> * Regenerate recipes.csv by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#910](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/910)
> * Retain load-bearing type witness in select position in `UnnecessaryExplicitTypeArguments` by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-static-analysis#911](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/911)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.35.0...v2.36.0>
>
> 2.35.0
> ------
>
> What's Changed
> --------------
>
> * Document identity-semantics caveat on PrimitiveWrapperClassConstructorToValueOf by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#883](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/883)
> * Skip `FinalClass` when class has anonymous inner subclasses by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#886](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/886)
> * Skip `ReplaceLambdaWithMethodReference` for parameterized types with multiple constructors by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#887](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/887)
> * Preserve `null` cast at varargs position in `RemoveRedundantTypeCast` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#888](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/888)
> * Retain explicit type arguments on no-arg static methods in `UnnecessaryExplicitTypeArguments` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#889](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/889)
> * Detect overloads on parameterized receiver in `hasMethodOverloading` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#890](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/890)
> * Preserve cast on generic method invocation in overloaded outer call by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#891](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/891)
> * Guard cast to `JavaType.Annotation` in `ReorderAnnotations` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#894](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/894)
> * Strip diamond operator from cast in `UseLambdaForFunctionalInterface` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#893](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/893)
> * Replace `var` with interface type in `UseLambdaForFunctionalInterface` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#896](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/896)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.34.1...v2.35.0>
>
> v2.34.1
> -------
>
> What's Changed
> --------------
>
> * OpenRewrite [v8.81.6](https://github.com/openrewrite/rewrite/releases/tag/v8.81.6)
> * Preserve statement order in `UseTryWithResources` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#866](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/866)
> * Skip meta-annotated `@Configuration` in `FinalClass` and `HideUtilityClassConstructor` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#867](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/867)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#869](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/869)
> * Revert to using rewrite-java-21 for test runtime by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#870](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/870)
> * Preserve casts pinning generic-method return type in lambdas by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#872](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/872)
> * Preserve `(Object)` bridge cast for unchecked generic casts by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#876](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/876)
> * Skip `Objects::nonNull`/`isNull` when lambda parameter does not match by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#875](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/875)
> * Regression tests for ReplaceStackWithDeque ControlFlow crashes by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-static-analysis#879](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/879)
> * Skip `SimplifyConsecutiveAssignments` when initializer would self-reference by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#882](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/882)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.34.0...v2.34.1>


Commits

* [`01d4a78`](openrewrite/rewrite-static-analysis@01d4a78) Update Gradle wrapper 9.5.1
* [`421318a`](openrewrite/rewrite-static-analysis@421318a) Retain load-bearing type witness in select position in `UnnecessaryExplicitTy...
* [`c488082`](openrewrite/rewrite-static-analysis@c488082) Regenerate recipes.csv ([#910](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/910))
* [`829a5a5`](openrewrite/rewrite-static-analysis@829a5a5) Use receiver type for unbound instance method reference in `ReplaceLambdaWith...
* [`5ab23a2`](openrewrite/rewrite-static-analysis@5ab23a2) Skip Python sources in `CombineSemanticallyEqualCatchBlocks` ([#908](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/908))
* [`e4bcb09`](openrewrite/rewrite-static-analysis@e4bcb09) Preserve raw cast on parameterized expression in `RemoveRedundantTypeCast` (#...
* [`62e6a08`](openrewrite/rewrite-static-analysis@62e6a08) Preserve cast on argument to overloaded method in `RemoveRedundantTypeCast` (...
* [`3ef523d`](openrewrite/rewrite-static-analysis@3ef523d) Guard `instanceof` to `Class::isInstance` conversion on lambda arity ([#903](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/903))
* [`03d153e`](openrewrite/rewrite-static-analysis@03d153e) Replace `var` with interface type in `UseLambdaForFunctionalInterface` ([#896](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/896))
* [`8e930b0`](openrewrite/rewrite-static-analysis@8e930b0) Strip diamond operator from cast in `UseLambdaForFunctionalInterface` ([#893](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/893))
* Additional commits viewable in [compare view](openrewrite/rewrite-static-analysis@v2.34.0...v2.36.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-static-analysis&package-manager=maven&previous-version=2.34.0&new-version=2.36.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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.

1 participant