https://github.com/openrewrite/rewrite-templating
Automated templating using code snippets.
https://github.com/openrewrite/rewrite-templating
Last synced: 4 months ago
JSON representation
Automated templating using code snippets.
- Host: GitHub
- URL: https://github.com/openrewrite/rewrite-templating
- Owner: openrewrite
- License: apache-2.0
- Created: 2022-11-28T18:26:43.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2026-01-30T01:20:56.000Z (5 months ago)
- Last Synced: 2026-01-30T02:39:28.275Z (5 months ago)
- Language: Java
- Homepage:
- Size: 1.25 MB
- Stars: 16
- Watchers: 7
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
rewrite-templating
[](https://github.com/openrewrite/rewrite-templating/actions/workflows/ci.yml)
[](https://www.apache.org/licenses/LICENSE-2.0)
[](https://mvnrepository.com/artifact/org.openrewrite/rewrite-templating)
[](https://ge.openrewrite.org/scans)
[](https://github.com/openrewrite/.github/blob/main/CONTRIBUTING.md)
### What is this?
Support before/after templating as seen in Google Refaster.
### Input
Allows you to defined one or more `@BeforeTemplate` annotated methods and a single `@AfterTemplate` method.
```java
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
```
### Use JavaParser.Builder `.classpathFromResources(ctx, "guava-31")`
By default, the annotation processor will use `JavaParser.runtimeClasspath()` to resolve the classpath for newly generated Java code snippets.
If you want to use `TypeTables` from `src/main/resources/META-INF/rewrite/classpath.tsv.gz` instead, pass in the following option:
```
-Arewrite.javaParserClasspathFrom=resources
```
## Contributing
We appreciate all types of contributions. See the [contributing guide](https://github.com/openrewrite/.github/blob/main/CONTRIBUTING.md) for detailed instructions on how to get started.