openrewrite / openrewrite/rewrite-static-analysis
Recipe to convert collection literals which are never mutated to constants
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 62
- Forks
- 112
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 40
Description
What problem are you trying to solve?
Performance and readability. Avoid constructing the collection objects too many times.
What precondition(s) should be checked before applying this recipe?
There's a collection literal (or alike) which:
- is not returned beyond the current scope
- has no mutations of the content
Describe the situation before applying the recipe
public boolean isWeekend(String day) {
return Set.of("Saturday", "Sunday").contains(day);
}
Describe the situation after applying the recipe
private static final Set<String> WEEKEND_DAYS = Set.of("Saturday", "Sunday");
public boolean isWeekend(String day) {
return WEEKEND_DAYS.contains(day);
}
Any additional context
Probably the hardest here is the naming. But even if the name is automatically generated using org.openrewrite.java.VariableNameUtils#generateVariableName() it would still make sense.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the repository's existing recipe implementations and the preconditions described here. Define how non-mutated collection literals are identified and how generated constant names should work. Done means the recipe safely handles the stated scope and mutation constraints, with coverage for the example transformation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100