← All recipes
Moderne licensed

Replace duplicate String literals

Recipe IDorg.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals
Artifactorg.openrewrite.recipe:rewrite-static-analysis

Replaces String literals with a length of 5 or greater repeated a minimum of 3 times. Qualified String literals include final Strings, method invocations, and new class invocations. Adds a new private static final String or uses an existing equivalent class field. A new variable name will be generated based on the literal value if an existing field does not exist. The generated name will append a numeric value to the variable name if a name already exists in the compilation unit. Centralizing repeated string values into constants makes refactoring safer and reduces the risk of inconsistent updates.

Single recipeRSPEC-S1192RSPEC-S1889MSAL

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals

If the recipe isn’t available locally, install it with:

mod config recipes jar install org.openrewrite.recipe:rewrite-static-analysis:2.40.0

Options

NameTypeDescription
includeTestSourcesBooleanChanges only apply to main by default. includeTestSources will apply the recipe to test source files.

Data tables

Structured output this recipe can produce.

  • Source files that had resultsSource files that were modified by the recipe run.org.openrewrite.table.SourcesFileResults
  • Source files that had search resultsSearch results that were found during the recipe run.org.openrewrite.table.SearchResults
  • Source files that errored on a recipeThe details of all errors produced by a recipe run.org.openrewrite.table.SourcesFileErrors
  • Recipe performanceStatistics used in analyzing the performance of recipes.org.openrewrite.table.RecipeRunStats