← All recipes
Open source

Replace annotation

Recipe IDorg.openrewrite.java.ReplaceAnnotationArtifactorg.openrewrite:rewrite-java

Replace an Annotation with another one if the annotation pattern matches. Only fixed parameters can be set in the replacement.

Single recipeApache 2.0

Add to your build

Get a download tokenSign in with GitHub or Google, and your token replaces YOUR_DOWNLOAD_TOKEN below. Free for open source recipes like this one.

~/.gradle/gradle.properties

codeGenomeUsername=you@example.com
codeGenomeToken=YOUR_DOWNLOAD_TOKEN

build.gradle.kts

plugins {
    id("org.openrewrite.rewrite") version("latest.release")
}

rewrite {
    activeRecipe("org.openrewrite.java.ReplaceAnnotation")
}

repositories {
    mavenCentral()
    maven {
        url = uri("https://artifacts.codegenomeproject.org/maven")
        credentials {
            username = providers.gradleProperty("codeGenomeUsername").get()
            password = providers.gradleProperty("codeGenomeToken").get()
        }
    }
}

dependencies {
    rewrite("org.openrewrite:rewrite-java:8.92.8")
}

Then run

./gradlew rewriteRun

This recipe has required options, which a build sets in a rewrite.yml recipe that you activate instead.

Usage

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

mod run . --recipe org.openrewrite.java.ReplaceAnnotation --recipe-option "annotationPatternToReplace=@org.jetbrains.annotations.NotNull("Test")" --recipe-option "annotationTemplateToInsert=@org.jetbrains.annotations.NotNull("Null not permitted")"

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

mod config recipes jar install org.openrewrite:rewrite-java:RELEASE

Options

NameTypeDescription
annotationPatternToReplacerequiredStringAn annotation matcher, expressed as a method pattern to replace.
e.g. @org.jetbrains.annotations.NotNull("Test")
annotationTemplateToInsertrequiredStringAn annotation template to add instead of original one, will be parsed with JavaTemplate.
e.g. @org.jetbrains.annotations.NotNull("Null not permitted")
classpathResourceNameStringIf the annotation's type is defined by a jar within the META-INF/rewrite/classpath directory provide its name here so that it can be loaded. When this parameter is not passed the runtime classpath of the recipe is provided to the parser producing the new annotation. This is necessary when the annotation is not on the runtime classpath of the recipe and isn't in the Java standard library.
e.g. annotations

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