Skip to content

Convert Guava Sets.filter#898

Merged
timtebeek merged 8 commits intoopenrewrite:mainfrom
lredor:lredor/eng/897-Sets_filter
Oct 26, 2025
Merged

Convert Guava Sets.filter#898
timtebeek merged 8 commits intoopenrewrite:mainfrom
lredor:lredor/eng/897-Sets_filter

Conversation

@lredor
Copy link
Contributor

@lredor lredor commented Oct 24, 2025

What's changed?

Anything in particular you'd like reviewers to focus on?

First contributions, so I’m not sure I’ve mastered all aspects of OpenRewrite yet.

Anyone you would like to review specifically?

@timtebeek

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 24, 2025
@timtebeek timtebeek self-requested a review October 24, 2025 16:16
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Near perfect already, great to see, thanks! One question before we merge as we don't want to potentially change the behavior for folks relying on special handling for SortedSet.

maybeAddImport("java.util.function.Predicate");
maybeAddImport("java.util.stream.Collectors");

return JavaTemplate.builder("#{any(java.util.Collection)}.stream().filter(#{any(java.util.function.Predicate)}).collect(Collectors.toSet())")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a slight concern here where Sets has special handling for SortedSet, whereas the the replacement always uses toSet, which uses HashSet. Should we conditionally use .toCollection(TreeSet::new) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I added a case for that.

@timtebeek timtebeek added recipe Recipe requested guava labels Oct 24, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Oct 24, 2025
Comment on lines +119 to +121
public static Set<Object> test(SortedSet<Object> set, Predicate<Object> isNotNull) {
Set<Object> indirectSet = set;
return Sets.filter(indirectSet, isNotNull);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is tricky; we can't really deduce from the type system whether we'd end up in this conditional of special handling:
https://github.com/google/guava/blob/21cdcf6d9a49c1a498acb422471d8d71f9219865/guava/src/com/google/common/collect/Sets.java#L1190-L1194

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd hope that to be rare enough to perhaps still merge the current work, but there is a risk here which could only be mitigated by data flow analysis to figure out if it might be a SortedSet going into this filter(Set, Predicate) method. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way thanks for the quick rework; would love to hear your thoughts on this edge case.

@timtebeek timtebeek moved this from Ready to Review to In Progress in OpenRewrite Oct 25, 2025
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again! I think the risk is low here for folks relying on that instanceof SortedSet check, so we'll merge this as is with the test documented to fail.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Oct 26, 2025
@timtebeek timtebeek merged commit fd556ee into openrewrite:main Oct 26, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Oct 26, 2025
@lredor lredor deleted the lredor/eng/897-Sets_filter branch October 29, 2025 09:46
mergify bot added a commit to robfrank/linklift that referenced this pull request Nov 10, 2025
…20.0 to 3.21.1 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.20.0 to 3.21.1.
Release notes

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

> v3.21.1
> -------
>
> What's Changed
> --------------
>
> * Instance main should not be used for certain test cases by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#891](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/891)
> * Update expected indentation for `LombokValueToRecordTest` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#892](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/892)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#894](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/894)
> * Convert Guava `Iterables.any` and `.filter` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#896](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/896)
> * Support varargs for Predicates.and() and Predicates.or() by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#901](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/901)
> * Convert Guava `Functions.compose` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#906](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/906)
> * Convert Guava `Sets.filter` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#898](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/898)
> * Only convert Guava Predicate when not used as method argument by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#902](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/902)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#907](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/907)
> * Support Lambda for Predicates.and() and Predicates.or() by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#910](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/910)
> * Convert Guava `Predicates.equalTo` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#911](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/911)
> * Only change Guava `Predicate.apply` to `test` when not using `filter` or similar methods by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#909](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/909)
> * Cover more cases for Predicates.and/or methods by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#912](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/912)
> * Prefer `Optional.stream().collect(toSet())` over `Optional.asSet()` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#915](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/915)
> * Fix indentation calculation by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#916](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/916)
> * Convert Guava `Iterables.transform` and `Collections2.transform` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#917](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/917)
> * Ignore `java.util.Iterator` in NoGuavaSetsNewHashSet recipe by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#921](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/921)
> * Gradle 9.1.0 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#922](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/922)
> * Dependencies for `javax.jws..*` in Java 11 Migration by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-migrate-java#925](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/925)
> * Set build tools to use Java 8 when running Java 8 upgrade by [`@​MBoegers`](https://github.com/MBoegers) in [openrewrite/rewrite-migrate-java#924](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/924)
> * Update SDKMan Java version by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#926](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/926)
> * Do not migrate to Jakarta annotations as part of Java 17 upgrade by [`@​Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-migrate-java#923](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/923)
> * Gradle 9.2.0 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#930](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/930)
> * Change the Jetty dependency coordinates going from EE9 to EE10 by [`@​ChenyuWang98`](https://github.com/ChenyuWang98) in [openrewrite/rewrite-migrate-java#914](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/914)
> * Move the `@InlineMe` recipes to rewrite-third-party by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#931](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/931)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.20.0...v3.21.1>
>
> 3.21.0
> ------
>
> What's Changed
> --------------
>
> * Instance main should not be used for certain test cases by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#891](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/891)
> * Update expected indentation for `LombokValueToRecordTest` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#892](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/892)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#894](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/894)
> * Convert Guava `Iterables.any` and `.filter` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#896](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/896)
> * Support varargs for Predicates.and() and Predicates.or() by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#901](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/901)
> * Convert Guava `Functions.compose` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#906](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/906)
> * Convert Guava `Sets.filter` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#898](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/898)
> * Only convert Guava Predicate when not used as method argument by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#902](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/902)
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#907](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/907)
> * Support Lambda for Predicates.and() and Predicates.or() by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#910](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/910)
> * Convert Guava `Predicates.equalTo` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#911](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/911)
> * Only change Guava `Predicate.apply` to `test` when not using `filter` or similar methods by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#909](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/909)
> * Cover more cases for Predicates.and/or methods by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#912](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/912)
> * Prefer `Optional.stream().collect(toSet())` over `Optional.asSet()` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#915](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/915)
> * Fix indentation calculation by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#916](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/916)
> * Convert Guava `Iterables.transform` and `Collections2.transform` by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#917](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/917)
> * Ignore `java.util.Iterator` in NoGuavaSetsNewHashSet recipe by [`@​lredor`](https://github.com/lredor) in [openrewrite/rewrite-migrate-java#921](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/921)

... (truncated)


Commits

* [`29e071d`](openrewrite/rewrite-migrate-java@29e071d) Handle new Java 17 fx zulu images going forward
* [`abce128`](openrewrite/rewrite-migrate-java@abce128) OpenRewrite recipe best practices
* [`5fe85a4`](openrewrite/rewrite-migrate-java@5fe85a4) [Auto] SDKMAN! Java candidates as of 2025-11-03T1018
* [`fc5ae22`](openrewrite/rewrite-migrate-java@fc5ae22) Move the `@InlineMe` recipes to rewrite-third-party ([#931](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/931))
* [`a5af8f1`](openrewrite/rewrite-migrate-java@a5af8f1) Add `NoGuavaMapsNewLinkedHashMap` to `NoGuava`
* [`19ba363`](openrewrite/rewrite-migrate-java@19ba363) Change the Jetty dependency coordinates going from EE9 to EE10 ([#914](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/914))
* [`d802221`](openrewrite/rewrite-migrate-java@d802221) Gradle 9.2.0 ([#930](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/930))
* [`bb406cf`](openrewrite/rewrite-migrate-java@bb406cf) Do not migrate to Jakarta annotations as part of Java 17 upgrade ([#923](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/923))
* [`926d84d`](openrewrite/rewrite-migrate-java@926d84d) Update SDKMan Java version ([#926](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/926))
* [`bd5eecf`](openrewrite/rewrite-migrate-java@bd5eecf) Set build tools to use Java 8 when running Java 8 upgrade ([#924](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/924))
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.20.0...v3.21.1)
  
[![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.20.0&new-version=3.21.1)](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

guava recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants