Visitar URL original
Replace `Maps.newConcurrentMap`, `Sets.newTreeSet` and `Strings.nullToEmpty` in NoGuava by knutwannheden · Pull Request #1421 · openrewrite/rewrite-migrate-java · GitHub
Skip to content

Replace Maps.newConcurrentMap, Sets.newTreeSet and Strings.nullToEmpty in NoGuava - #1421

Merged
knutwannheden merged 3 commits into
mainfrom
guava-more-factories
Nov 5, 2026
Merged

knutwannheden merged 3 commits into
mainfrom
guava-more-factories

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

NoGuava leaves four Guava calls in place although each has an exact JDK equivalent. Across about 22,500 public Java repositories, 159 call Maps.newConcurrentMap(), 157 call Strings.nullToEmpty(..) and 81 call Sets.newTreeSet(..), so a project that uses any of them keeps its Guava dependency after the migration.

This adds Refaster templates to NoGuavaRefaster, which NoGuava already runs as NoGuavaRefasterRecipes:

Guava Java standard library
Maps.newConcurrentMap() new ConcurrentHashMap<>()
Sets.newTreeSet() new TreeSet<>()
Sets.newTreeSet(comparator) new TreeSet<>(comparator)
Strings.nullToEmpty(s) Objects.toString(s, "")

Maps.newConcurrentMap() returns a ConcurrentHashMap typed as ConcurrentMap, so the replacement fits wherever the original did. Objects.toString(s, "") returns s.toString() for a non-null String, which is s itself.

Objects.toString(s, "") is preferred to Objects.requireNonNullElse(s, "") because it exists since Java 7 and needs no version gate. It is not statically imported, since a class's own toString() would shadow it. In a file that imports Guava's Objects, the template writes java.util.Objects.toString(..) in full.

Sets.newTreeSet(Iterable) has no template. Given an argument typed as a SortedSet, new TreeSet<>(..) keeps that set's comparator, where Guava sorts by natural order.

NoGuavaRefasterTest has a test for each replacement, and each failed before the templates existed.

…y in NoGuava

Each has an exact Java standard library equivalent, but NoGuava left them in place.
@knutwannheden knutwannheden self-assigned this Nov 3, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 3, 2026
A file that keeps using Guava's `Objects` gets `java.util.Objects.toString(s, "")` in full, as the PR describes, and a test now covers it. The comment on the missing `newTreeSet(Iterable)` template states the invariant.
@knutwannheden
knutwannheden merged commit 29df877 into main Nov 5, 2026
1 check passed
@knutwannheden
knutwannheden deleted the guava-more-factories branch November 5, 2026 18:56
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant