← All recipes
Moderne licensed

Replace a method invocation with a reference to an annotated field

Recipe IDio.moderne.java.dropwizard.boot.test.ReplaceMethodInvocationWithAnnotatedField
Artifactio.moderne.recipe:rewrite-dropwizard

For each class containing an invocation matching the configured method pattern, introduces an annotated field of the requested type and rewrites every matching invocation in that class to reference the new field. If a field with the same annotation and type already exists, its name is reused.

Single recipeProprietary

Usage

You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe io.moderne.java.dropwizard.boot.test.ReplaceMethodInvocationWithAnnotatedField --recipe-option "methodPattern=io.dropwizard.testing.junit5.DropwizardAppExtension getLocalPort()" --recipe-option "fieldType=int" --recipe-option "annotationFqn=org.springframework.boot.test.web.server.LocalServerPort" --recipe-option "preferredFieldName=localPort" --recipe-option "classpathResources=spring-boot-test-3.*"

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

mod config recipes jar install io.moderne.recipe:rewrite-dropwizard:0.5.6

Options

NameTypeDescription
methodPatternrequiredStringA method pattern matched against the invocation to replace. A method pattern is used to find matching method invocations. For example, to find all method invocations in the Guava library, use the pattern: com.google.common..*#*(..).

The pattern format is <PACKAGE>#<METHOD_NAME>(<ARGS>).

..* includes all subpackages of com.google.common.
*(..) matches any method name with any number of arguments.

For more specific queries, like Guava's ImmutableMap, use com.google.common.collect.ImmutableMap#*(..) to narrow down the results.
e.g. io.dropwizard.testing.junit5.DropwizardAppExtension getLocalPort()
fieldTyperequiredStringEither a primitive keyword (e.g. int) or a fully qualified class name for the field's type.
e.g. int
annotationFqnrequiredStringFully qualified annotation type to place on the new field.
e.g. org.springframework.boot.test.web.server.LocalServerPort
preferredFieldNamerequiredStringName to give the new field. If a same-named field already exists in the class, an incrementing suffix is appended to avoid collision.
e.g. localPort
classpathResourcesrequiredListResource patterns passed to the JavaParser used by the field-injection template.
e.g. spring-boot-test-3.*

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