Title: Replace Guava ImmutableList/ImmutableMap/ImmutableSet with java.util and ban them with forbidden-apis · Issue #202 · openjavaformat/open-java-format · GitHub
Open Graph Title: Replace Guava ImmutableList/ImmutableMap/ImmutableSet with java.util and ban them with forbidden-apis · Issue #202 · openjavaformat/open-java-format
X Title: Replace Guava ImmutableList/ImmutableMap/ImmutableSet with java.util and ban them with forbidden-apis · Issue #202 · openjavaformat/open-java-format
Description: Goal Stop using Guava's ImmutableList, ImmutableMap and ImmutableSet in this repository (all modules, the SPI included) in favour of plain java.util collections, and make the build fail if they come back. Tooling Error Prone has no built...
Open Graph Description: Goal Stop using Guava's ImmutableList, ImmutableMap and ImmutableSet in this repository (all modules, the SPI included) in favour of plain java.util collections, and make the build fail if they com...
X Description: Goal Stop using Guava's ImmutableList, ImmutableMap and ImmutableSet in this repository (all modules, the SPI included) in favour of plain java.util collections, and make the build fail if they...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Replace Guava ImmutableList/ImmutableMap/ImmutableSet with java.util and ban them with forbidden-apis","articleBody":"## Goal\n\nStop using Guava's `ImmutableList`, `ImmutableMap` and `ImmutableSet` in this repository (all modules, the SPI included) in favour of plain `java.util` collections, and make the build fail if they come back.\n\n## Tooling\n\nError Prone has no built-in \"ban this class\" check, and Picnic's error-prone-support (0.30.0) pushes the other way (it has many rules around Guava's `Immutable*`). What fits instead:\n\n- **[forbidden-apis](https://github.com/policeman-tools/forbidden-apis) 3.11** (`de.thetaphi.forbiddenapis`): bytecode check, one signature per class in `config/forbidden-apis/guava-immutables.txt`.\n - Configuration cache: works (stored and reused with no problems on `:open-java-format-spi:forbiddenApisMain`).\n - Build cache: not supported, `CheckForbiddenApis` is not `@CacheableTask`. It is still `UP-TO-DATE` locally, and the scan takes ~0.03 s.\n - It needs `ignoreSignaturesOfMissingClasses = true`, otherwise modules without Guava on the classpath (benchmarks) fail while parsing the signatures.\n - It also flags indirect uses that javac accepts: subclasses (`ImmutableSortedSet`), and Guava methods whose declared return type is an `Immutable*` (`ImmutableRangeMap.asMapOfRanges()`, `ClassPath.getResources()`).\n- Alternatives considered: Checkstyle `IllegalImport` (cacheable, source level, but misses `var`), OpenRewrite `NoGuavaImmutableList*` recipes for the migration itself.\n\n## What was done (local, not pushed)\n\nAbout 46 files across all modules; `./gradlew check` was green, including all tests.\n\n- Mechanical: `ImmutableList.of/copyOf` -> `List.of/copyOf`, `toImmutableList()` -> `Stream.toList()`, builders -> `ArrayList` + `List.copyOf`.\n- `ignoreFailures` removed from the forbidden-apis convention, so the ban actually fails the build.\n- `FileBasedTests` lists test resources with `Files.walk` instead of Guava's `ClassPath`.\n- `Input.getPositionTokenMap()` now returns `RangeMap` (was `ImmutableRangeMap`).\n\n## Pitfalls found on the way\n\n- `Set.of` / `Map.of` do not keep insertion order. `Newlines.BREAKS` iterates in order (`\"\\r\\n\"` before `\"\\n\"`), so it became a `List`; `StringWrapper` keeps a `LinkedHashSet`.\n- `Set.of(..).contains(null)` and `List.of(..).contains(null)` throw. `JavaInput.buildToks` calls `contains` with a null token kind for comment tokens; this broke dozens of tests until the check used the (null-tolerant) `HashSet`.\n- Error Prone's `@Immutable` cannot prove `java.util.List`/`Map` immutable, so it had to be removed from `Input.Token`, `JavaInput.Token`, `OpsBuilder.BlankLineWanted`, `InputMetadata` and `doc.Token`.\n- `formatDiff` only formats changed lines and does not re-sort inserted imports.\n\n## Open question: SPI compatibility with Spotless (#200)\n\n#200 keeps `Style.PALANTIR` so Spotless's built-in `palantirJavaFormat()` step keeps working after swapping coordinates. Spotless's glue (`PalantirJavaFormatFormatterFunc`) is linked against `JavaFormatterOptions` (`Style`, `Builder.style`, `formatJavadoc`), `Formatter.createFormatter` / `formatSource`, `ImportOrderer.reorderImports(String, Style)` and `RemoveUnusedImports.removeUnusedImports`. None of these mention `Immutable*`.\n\nThe migration changes one public SPI signature, `FormatterService.getFormatReplacements(...)`: `ImmutableList\u003cReplacement>` -> `List\u003cReplacement>` (also in its implementations). `FormatterException` only changes its private field (`diagnostics()` already returned `List`).\n\nDecide whether the SPI keeps the Guava types (and is excluded from the ban) or changes.\n\n## Next steps\n\n1. Verify binary compatibility against Spotless: compile its `PalantirJavaFormatFormatterFunc` against the old (HEAD) classes and run it against the new ones.\n2. Decide what to do with the SPI signatures above.\n3. Rebase onto `origin/abashev/Fix-error-prone-issues` (two newer commits touch `SnippetFormatter`, `PalantirJavaFormatFormattingService` and `StringWrapper`), reapply the work, and open a PR.\n4. Update the CLAUDE.md line that still allows Guava immutables \"where an existing API already hands one over\".\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-11-08T16:29:50Z","url":"https://github.com/openjavaformat/open-java-format/issues/202"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:965b80de-8270-11f4-bcfc-3a288ac82c4b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82B4:2A59EB:5E4B8F:7D4C01:6AFBC739 |
| html-safe-nonce | df46c65e1454b339289eef65064269653da4648433854ea04b6f743da6c066f8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkI0OjJBNTlFQjo1RTRCOEY6N0Q0QzAxOjZBRkJDNzM5IiwidmlzaXRvcl9pZCI6IjUzMDE4NDE5NTcwNzk0Njc4MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7e7efb919369636897019ee0fa57d762596c2658232e86a65d35e1bd6399055e |
| hovercard-subject-tag | issue:6273051193 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openjavaformat/open-java-format/202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0eb73fe68500537585a44c1d638d2b1a810629facacd048ce88052b47615c814/openjavaformat/open-java-format/issues/202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0eb73fe68500537585a44c1d638d2b1a810629facacd048ce88052b47615c814/openjavaformat/open-java-format/issues/202 |
| og:image:alt | Goal Stop using Guava's ImmutableList, ImmutableMap and ImmutableSet in this repository (all modules, the SPI included) in favour of plain java.util collections, and make the build fail if they com... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width