Replace a method invocation with a reference to an annotated field

Proprietary

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. Calls in static or initialization contexts, ambiguous receivers and custom extension construction are retained.

io.moderne.java.dropwizard.boot.test.ReplaceMethodInvocationWithAnnotatedField

Required

A 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.

Either a primitive keyword (e.g. int) or a fully qualified class name for the field's type.

Fully qualified annotation type to place on the new field.

Name to give the new field. If a same-named field already exists in the class, an incrementing suffix is appended to avoid collision.

Resource patterns passed to the JavaParser used by the field-injection template. Separate values with commas.

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

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.