Skip to content

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Oct 20, 2025

Summary

Problem

The NoGuavaSetsNewHashSet recipe was transforming code like:

Sets.newHashSet(Iterables.filter(myExceptions, ClassCastException.class))

Into broken code:

new HashSet<>(Arrays.asList(Iterables.filter(...)))

This caused compilation errors because:

  1. Arrays.asList() doesn't accept Iterable parameters
  2. Type inference failed for HashSet<>

Root Cause

The recipe only checked if the argument was assignable to Collection, but Guava's Sets.newHashSet() has three overloads:

  • newHashSet() - no args ✓
  • newHashSet(E... elements) - varargs ✓
  • newHashSet(Iterable<? extends E> elements) - Missing!

When an Iterable (not a Collection) was passed, it fell through to the varargs case and generated invalid code.

Solution

Modified the recipe to:

  1. Check if argument is a Collection first → transform to new HashSet<>(collection)
  2. Check if argument is only an Iterableskip transformation (preserve Guava code)
  3. Otherwise, treat as varargs → transform to new HashSet<>(Arrays.asList(...))

This conservative approach ensures we never generate broken code.

Test Plan

  • setsNewHashSetWithIterablesFilter: Verifies the reported issue is fixed
  • setsNewHashSetWithCustomIterable: Verifies other Iterable types are skipped
  • setsNewHashSetWithCollectionStillWorks: Verifies Collection transformation still works
  • ✅ All existing tests pass

Alternative Approaches Considered

🤖 Generated with Claude Code

Addresses issue #881 where Sets.newHashSet(Iterables.filter(...))
was incorrectly transformed to broken code using Arrays.asList().

The recipe now:
- Only transforms Sets.newHashSet(Collection) to new HashSet<>(Collection)
- Skips transformation for Iterable-only arguments (not Collection)
- Preserves the original Guava code when a safe transformation is not possible

This conservative approach prevents generating code with type inference
failures and compilation errors.

Test cases added:
- setsNewHashSetWithIterablesFilter: Verifies Iterables.filter() is skipped
- setsNewHashSetWithCustomIterable: Verifies custom Iterable is skipped
- setsNewHashSetWithCollectionStillWorks: Verifies Collection case still works

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

Co-Authored-By: Claude <[email protected]>
@timtebeek timtebeek marked this pull request as ready for review October 20, 2025 20:34
@timtebeek timtebeek self-assigned this Oct 20, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Oct 20, 2025
@timtebeek timtebeek changed the title Fix NoGuavaSetsNewHashSet to skip Iterable-only arguments Fix NoGuavaSetsNewHashSet to skip Iterable-only arguments Oct 20, 2025
@timtebeek timtebeek merged commit 67e8d8c into main Oct 20, 2025
2 checks passed
@timtebeek timtebeek deleted the sets-newHashSet-Iterables-filter branch October 20, 2025 20:34
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Oct 20, 2025
mergify bot added a commit to robfrank/linklift that referenced this pull request Oct 30, 2025
…19.0 to 3.20.0 [skip ci]

[//]: # (dependabot-start)
⚠️ \*\*Dependabot is rebasing this PR\*\* ⚠️
Rebasing might not happen immediately, so don't worry if this takes some time.
Note: if you make any changes to this PR yourself, they will take precedence over the rebase.
---
[//]: # (dependabot-end)
Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.19.0 to 3.20.0.
Release notes

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

> 3.20.0
> ------
>
> What's Changed
> --------------
>
> * Fix Lombok recipes incorrectly replacing getters/setters accessing other objects' fields by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#873](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/873)
> * Update suppressions by [`@​Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-migrate-java#874](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/874)
> * Fix duplicate [`@​Getter`](https://github.com/Getter) annotations on multiple variable declarations by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#877](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/877)
> * Enable the switch recipes as part of the Java 21 migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#878](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/878)
> * Fix `NoGuavaSetsNewHashSet` to skip `Iterable`-only arguments by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#882](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/882)
> * Convert Guava `Predicates.and` and `.or` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#885](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/885)
> * Convert Guava `Predicates.equalTo` and `.not` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#887](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/887)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#888](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/888)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.19.0...v3.20.0>


Commits

* [`ecb6886`](openrewrite/rewrite-migrate-java@ecb6886) OpenRewrite recipe best practices ([#888](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/888))
* [`8014157`](openrewrite/rewrite-migrate-java@8014157) Convert Guava `Predicates.equalTo` and `.not` ([#887](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/887))
* [`cef9c1f`](openrewrite/rewrite-migrate-java@cef9c1f) Convert Guava `Predicates.and` and `.or` ([#885](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/885))
* [`67e8d8c`](openrewrite/rewrite-migrate-java@67e8d8c) Fix `NoGuavaSetsNewHashSet` to skip `Iterable`-only arguments ([#882](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/882))
* [`92f9a94`](openrewrite/rewrite-migrate-java@92f9a94) Enable the switch recipes as part of the Java 21 migration ([#878](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/878))
* [`209211e`](openrewrite/rewrite-migrate-java@209211e) [Auto] SDKMAN! Java candidates as of 2025-10-20T1016
* [`ce56846`](openrewrite/rewrite-migrate-java@ce56846) Fix duplicate [`@​Getter`](https://github.com/Getter) annotations on multiple variable declarations ([#877](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/877))
* [`e18534b`](openrewrite/rewrite-migrate-java@e18534b) Add missed recipes to Java 6 and No Guava recipes
* [`207d898`](openrewrite/rewrite-migrate-java@207d898) Update suppressions ([#874](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/874))
* [`7ebbf72`](openrewrite/rewrite-migrate-java@7ebbf72) Fix Lombok recipes incorrectly replacing getters/setters accessing other obje...
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.19.0...v3.20.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.19.0&new-version=3.20.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.

Sets.newHashSet(Iterables.filter... is not correctly handed by NoGuavaSetsNewHashSet recipe

2 participants