openrewrite / openrewrite/rewrite-static-analysis

Recipe to convert collection literals which are never mutated to constants

Open
#617 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.