Extend Use{List,Set,Map}Of to recognise prose-statement chains#1145
Merged
Conversation
UseListOf, UseSetOf, and UseMapOf previously matched only the
anonymous-class idiom:
List<String> l = new ArrayList<>() {{ add("a"); add("b"); }};
Extends each recipe to also recognise the more common prose-statement
shape, where a no-arg constructor declaration is followed by a chain
of add(..) or put(..) calls on the declared variable:
List<String> l = new ArrayList<>();
l.add("a");
l.add("b");
These are collapsed into the constructor with the immutable factory:
List<String> l = new ArrayList<>(List.of("a", "b"));
Output is always wrapped in the mutable ArrayList/HashSet/HashMap so
downstream mutations (.sort(), .add(), etc.) remain valid.
Recognised shapes per recipe:
- UseListOf: target.add(arg) chains; output uses List.of(..).
- UseSetOf: target.add(arg) chains; output uses Set.of(..).
- UseMapOf: target.put(k, v) chains; output uses Map.of(k, v, ..)
up to 10 pairs, Map.ofEntries(Map.entry(k, v), ..) beyond.
Bail conditions (all three): raw LHS, non-no-arg constructor (e.g.
capacity hint), intervening non-recognised statement, an argument
that references the target variable (depends on step-by-step
mutation), null literal argument. Threshold: at least 2 add/put
statements; a single call is not worth the rewrite churn.
Implementation: a visitBlock pre-pass scans for prose patterns and
records (initializer UUID -> [args]) on a cursor message; the
existing visitNewClass override consults the map and applies the
JavaTemplate at the correct cursor depth. After super.visitBlock,
the post-pass filters absorbed add/put statements from the block.
Regenerate recipes.csv to reflect the new descriptions.
timtebeek
approved these changes
Jun 25, 2026
timtebeek
left a comment
Member
There was a problem hiding this comment.
Nice extension indeed! Much more common case. Still iterating locally on some readability improvements for map pairs, as all on a single line can hurt readability I think.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UseListOf,UseSetOf, andUseMapOfpreviously matched only the anonymous-class idiom (new ArrayList<>() {{ add("a"); add("b"); }}). Each recipe now also recognises the much more common prose-statement shape:The same pattern applies to
Set<T>/HashSet(withSet.of(...)) andMap<K,V>/HashMap(withMap.of(k, v, ...)up to 10 pairs, thenMap.ofEntries(Map.entry(k, v), ...)).The output is always wrapped in the mutable
ArrayList/HashSet/HashMapso any downstream.sort(),.add(), etc. remain valid. This is the safe choice in all cases; a future enhancement could detect "never mutated after the chain" and shed the wrap.Recognised shapes (per recipe)
UseListOfnew ArrayList<>()target.add(arg)new ArrayList<>(List.of(...))UseSetOfnew HashSet<>()target.add(arg)new HashSet<>(Set.of(...))UseMapOfnew HashMap<>()target.put(k, v)new HashMap<>(Map.of(...))/new HashMap<>(Map.ofEntries(...))past 10 pairsBail conditions
Each recipe leaves code unchanged when any of the following applies:
List names = new ArrayList()) — we'd be guessing at a type argument. Other recipes can be composed to parameterise first.new ArrayList<>(10)with a capacity hint) — silently dropping the hint would be misleading.add/putisn't itself a recognised call on the same target (e.g.System.out.println(...),addAll(..)).target.add(target.size())) — the collapsedList.of(...)would evaluate against the pre-add state and change semantics.nullliteral —List.of/Set.of/Map.of/Map.entryall reject null at runtime.add/putstatements follow the declaration — the rewrite would be noise.Implementation
A
visitBlockpre-pass scans for prose patterns and records(initializer UUID → [args])on a cursor message.super.visitBlockthen visits children; the existingvisitNewClassoverride consults the message viagetCursor().getNearestMessage(...)and, when matched, applies aJavaTemplateat the correct cursor depth (whereupdateCursor(n)is well-defined). Aftersuper.visitBlockreturns, a post-pass filters the now-absorbedadd/putstatements out of the block.The original anonymous-class logic is preserved verbatim — both shapes are handled by the same recipe class.
Test plan
UseListOfTest— 7 existing + 7 new: positive prose-chain, single-add below threshold, intervening statement, arg-referencing-target, null arg, raw LHS, capacity-hint constructor.UseSetOfTest— 8 existing + 6 new: parallel coverage toUseListOfTest.UseMapOfTest— 12 existing + 6 new: positiveMap.of,Map.ofEntriesboundary at 11 pairs, single-put below threshold, intervening statement, value-referencing-target, null value, raw LHS../gradlew testpasses (full suite, no cross-package regressions)../gradlew recipeCsvValidatepasses;recipes.csvregenerated to reflect the new descriptions.