Skip to content

openrewrite/rewrite-templating

Repository files navigation

Rewrite Templating

Support before/after templating as seen in Google Refaster.

ci Apache 2.0 Maven Central Revved up by Develocity Contributing Guide

Input

Allows you to defined one or more @BeforeTemplate annotated methods and a single @AfterTemplate method.

package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;

public class UseStringIsEmpty {
    @BeforeTemplate
    boolean before(String s) {
        return s.length() > 0;
    }

    @AfterTemplate
    boolean after(String s) {
        return !s.isEmpty();
    }
}

Output

This results in a recipe that can be used to transform code that matches the @BeforeTemplate to the @AfterTemplate.

Options

Annotation processors can take options to customize their behavior. Options are passed to the annotation processor via the -A flag.

Change the @Generated annotation

By default, the annotation processor will add a @javax.annotation.Generated annotation to the generated classes, compatible with Java 8. On newer Java version you'd perhaps want to pass in the following option:

-Arewrite.generatedAnnotation=jakarta.annotation.Generated

Contributing

We appreciate all types of contributions. See the contributing guide for detailed instructions on how to get started.