Skip to main content

Generate InlineMethodCalls recipes for deprecated delegating methods

org.openrewrite.java.recipes.GenerateDeprecatedMethodRecipes

Finds @Deprecated method declarations whose body is a single delegation call to another method in the same class, and generates a declarative YAML recipe file containing InlineMethodCalls entries for each.

Recipe source

GitHub: GenerateDeprecatedMethod.java, Issue Tracker, Maven Central

This recipe is available under the Moderne Source Available License.

Used by

This recipe is used as part of the following composite recipes:

Example

Unchanged
package com.example;

public class Foo {
private final String a;
private final String b;

public Foo(String a, String b) {
this.a = a;
this.b = b;
}

@Deprecated
public Foo(String a) {
this(a, null);
}
}
New file
src/main/resources/META-INF/rewrite/inline-deprecated-methods.yml
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.recipes.InlineDeprecatedMethods
displayName: Inline deprecated delegating methods
description: Automatically generated recipes to inline deprecated method calls that delegate to other methods in the same class.
recipeList:
- org.openrewrite.java.InlineMethodCalls:
methodPattern: 'com.example.Foo <constructor>(java.lang.String)'
replacement: 'this(a, null)'

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-rewrite` in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.java.recipes.GenerateDeprecatedMethodRecipes")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-rewrite:0.24.2")
    }
  2. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Deprecated method delegations

org.openrewrite.java.recipes.DeprecatedMethodDelegations

Deprecated methods that delegate to another method in the same class, suitable for inlining via InlineMethodCalls.

Column NameDescription
Method patternThe method pattern of the deprecated method.
ReplacementThe replacement expression to inline.
Recipe YAMLA YAML snippet that can be copied into a recipe list.