Skip to content

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Nov 10, 2025

Summary

The ReplaceLambdaWithMethodReference recipe was generating malformed method references when converting lambdas that use locally-defined records or classes. Instead of producing valid syntax like R::s, it generated invalid code like 1R::s.

Root Cause: Local classes in Java bytecode are named with patterns like OuterClass$1LocalName, where the digit indicates the method scope. The recipe was including this numeric prefix in the generated method reference.

Changes:

  • Modified JavaElementFactory.className() to detect local classes by checking for $<digit> patterns
  • Extract only the simple class name without the numeric prefix for local classes
  • Added comprehensive test coverage for local records and classes

Examples:

// Before (generates invalid code):
record R(String s) {}
stream.map(r -> r.s())  // would become: stream.map(1R::s) ❌

// After (generates valid code):
record R(String s) {}
stream.map(r -> r.s())  // becomes: stream.map(R::s) ✅

Test plan

  • Added test localRecordMethodReference - verifies lambda to method reference conversion for local records
  • Added test localClassMethodReference - verifies conversion for local classes
  • Added test localRecordWithInstanceOfCheck - verifies instanceof to isInstance conversion
  • Added test localRecordWithCast - verifies type cast to Class::cast conversion
  • All existing tests pass (2m 5s test suite)

🤖 Generated with Claude Code

…336)

The recipe was generating malformed method references like `1R::s` instead
of `R::s` when converting lambdas that reference locally-defined records or
classes. This occurred because local classes in Java bytecode are named with
patterns like `OuterClass$1LocalName`, where the digit indicates the method
scope.

The fix modifies `JavaElementFactory.className()` to detect local classes
(by checking for `$<digit>` patterns) and extract just the simple class name
without the numeric prefix.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Nov 10, 2025
@timtebeek timtebeek added bug Something isn't working recipe labels Nov 10, 2025
@timtebeek timtebeek marked this pull request as ready for review November 10, 2025 16:58
@timtebeek timtebeek merged commit 995c2d3 into main Nov 10, 2025
2 checks passed
@timtebeek timtebeek deleted the fix-336-local-record-method-reference branch November 10, 2025 16:58
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Nov 10, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Nov 24, 2025
… 2.21.0 to 2.22.0 [skip ci]

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

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

> 2.22.0
> ------
>
> What's Changed
> --------------
>
> * Prevent `AnnotateNullableMethods` from failing on TypeScript sources by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-static-analysis#773](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/773)
> * Fix CombineSemanticallyEqualCatchBlocks for inner exception types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#772](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/772)
> * Handle single quotes in ReplaceStringBuilderWithString by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#775](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/775)
> * Prevent `ReplaceLambdaWithMethodReference` in nested generic/overloaded contexts by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#776](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/776)
> * Fix ReplaceLambdaWithMethodReference for local records and classes by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#777](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/777)
> * Move nullable annotations to array types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#778](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/778)
> * Annotate nullable array type parameters by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#779](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/779)
> * Annotate required parameters with `@NonNull` and remove throws by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#782](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/782)
> * Skip UnnecessaryCatch after any missing types via a precondition by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#784](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/784)
> * Run `UnnecessaryCatch` after `UnnecessaryThrows` to remove unused catch clauses by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#785](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/785)
> * Add JavascriptFileChecker to selectively skip JavaScript files by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#787](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/787)
> * Fix `RemoveUnusedLocalVariables` to preserve assignments with side effects by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#741](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/741)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.21.0...v2.22.0>


Commits

* [`4d6fd83`](openrewrite/rewrite-static-analysis@4d6fd83) Fix `RemoveUnusedLocalVariables` to preserve assignments with side effects (#...
* [`16dc1da`](openrewrite/rewrite-static-analysis@16dc1da) Add JavascriptFileChecker to selectively skip JavaScript files ([#787](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/787))
* [`c1712c2`](openrewrite/rewrite-static-analysis@c1712c2) Update documentation examples
* [`af53ce1`](openrewrite/rewrite-static-analysis@af53ce1) Run `UnnecessaryCatch` after `UnnecessaryThrows` to remove unused catch claus...
* [`b670b5a`](openrewrite/rewrite-static-analysis@b670b5a) Skip UnnecessaryCatch after any missing types via a precondition ([#784](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/784))
* [`0f1d5db`](openrewrite/rewrite-static-analysis@0f1d5db) Annotate required parameters with `@NonNull` and remove throws ([#782](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/782))
* [`02e09d0`](openrewrite/rewrite-static-analysis@02e09d0) Annotate nullable array type parameters ([#779](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/779))
* [`0f25051`](openrewrite/rewrite-static-analysis@0f25051) Move nullable annotations to array types ([#778](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/778))
* [`995c2d3`](openrewrite/rewrite-static-analysis@995c2d3) Fix `ReplaceLambdaWithMethodReference` for local records and classes ([#336](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/336)) (...
* [`0ea5975`](openrewrite/rewrite-static-analysis@0ea5975) Prevent `ReplaceLambdaWithMethodReference` in nested generic/overloaded conte...
* Additional commits viewable in [compare view](openrewrite/rewrite-static-analysis@v2.21.0...v2.22.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.21.0&new-version=2.22.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 merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@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

bug Something isn't working recipe

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ReplaceLambdaWithMethodReference breaks with local record definition

2 participants