Skip to content

Conversation

@timtebeek
Copy link
Member

Summary

  • Fixes issue ReplaceLambdaWithMethodReference using invalid import for nested classes #744 where ReplaceLambdaWithMethodReference was incorrectly handling imports for nested classes
  • When an imported nested class like Map.Entry was used in an instanceof check, the transformation would generate Map.Entry.class::isInstance instead of Entry.class::isInstance
  • This fix preserves the original form of the class reference from the source code

Changes

  • Modified the ReplaceLambdaWithMethodReference recipe to directly use the original expression from the instanceof/cast rather than reconstructing it
  • Added a getClassType helper method to the visitor to extract the java.lang.Class type
  • Added comprehensive test cases covering nested class imports, fully qualified references, and cast operations

Test plan

  • Added test methodReferenceNestedClassImportInvalid that reproduces the original issue
  • Added test methodReferenceNestedClassFullyQualified to verify fully qualified references still work
  • Added test methodReferenceNestedClassCast to verify cast transformations also preserve imports correctly
  • All existing ReplaceLambdaWithMethodReferenceTest tests continue to pass

🤖 Generated with Claude Code

When transforming lambdas with instanceof checks or casts to method references,
preserve the original form of the class reference instead of always generating
a fully qualified name. This ensures that if a nested class like Map.Entry is
already imported as `import java.util.Map.Entry`, the transformed code uses
`Entry.class::isInstance` instead of `Map.Entry.class::isInstance`.

The fix directly uses the original expression from the instanceof/cast rather
than reconstructing it, allowing the ImportService to properly handle any
necessary import adjustments while preserving the user's preferred form.

Fixes #744

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

Co-Authored-By: Claude <[email protected]>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Sep 29, 2025
@timtebeek timtebeek marked this pull request as ready for review September 29, 2025 09:26
@timtebeek timtebeek self-assigned this Sep 29, 2025
@timtebeek timtebeek merged commit eb91699 into main Sep 29, 2025
2 checks passed
@timtebeek timtebeek deleted the fix/issue-744-nested-class-import branch September 29, 2025 10:14
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Sep 29, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Oct 15, 2025
… 2.18.0 to 2.19.0 [skip ci]

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

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

> 2.19.0
> ------
>
> What's Changed
> --------------
>
> * Fix FinalClass recipe to correctly handle nested static subclasses by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#742](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/742)
> * Add `Objects.requireNonNullElse/ElseGet` support to `AnnotateNullableParameters` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#743](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/743)
> * Fix ReplaceLambdaWithMethodReference handling of nested class imports by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#745](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/745)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#746](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/746)
> * Update recipe documentation examples by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#747](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/747)
> * Support for-each loop in NeedBraces by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#749](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/749)
> * Update documentation examples by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#750](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/750)
> * Update documentation examples by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-static-analysis#751](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/751)
> * fixed test that got changed behavior in rewrite-core by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-static-analysis#753](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/753)
> * No longer remove String.valueof when it's called on a method invocation by [`@​Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-static-analysis#752](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/752)
>
> New Contributors
> ----------------
>
> * [`@​Jenson3210`](https://github.com/Jenson3210) made their first contribution in [openrewrite/rewrite-static-analysis#753](https://redirect.github.com/openrewrite/rewrite-static-analysis/pull/753)
>
> **Full Changelog**: <openrewrite/rewrite-static-analysis@v2.18.0...v2.19.0>


Commits

* [`3c1a313`](openrewrite/rewrite-static-analysis@3c1a313) No longer remove String.valueof when it's called on a method invocation ([#752](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/752))
* [`1d8d504`](openrewrite/rewrite-static-analysis@1d8d504) fixed test that got changed behavior in rewrite-core ([#753](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/753))
* [`504a582`](openrewrite/rewrite-static-analysis@504a582) Update documentation examples ([#751](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/751))
* [`cd26b74`](openrewrite/rewrite-static-analysis@cd26b74) Update documentation examples ([#750](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/750))
* [`b80e4b6`](openrewrite/rewrite-static-analysis@b80e4b6) Support for-each loop in NeedBraces ([#749](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/749))
* [`b93d869`](openrewrite/rewrite-static-analysis@b93d869) Update recipe documentation examples ([#747](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/747))
* [`7165caf`](openrewrite/rewrite-static-analysis@7165caf) OpenRewrite recipe best practices ([#746](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/746))
* [`eb91699`](openrewrite/rewrite-static-analysis@eb91699) Fix ReplaceLambdaWithMethodReference handling of nested class imports ([#745](https://redirect.github.com/openrewrite/rewrite-static-analysis/issues/745))
* [`a5d78d7`](openrewrite/rewrite-static-analysis@a5d78d7) Polish MoveFieldAnnotationToType
* [`0bd9340`](openrewrite/rewrite-static-analysis@0bd9340) Add `Objects.requireNonNullElse/ElseGet` support to `AnnotateNullableParamete...
* Additional commits viewable in [compare view](openrewrite/rewrite-static-analysis@v2.18.0...v2.19.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.18.0&new-version=2.19.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

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants