Introduction to OpenRewrite
OpenRewrite enables large-scale distributed source code refactoring for framework migrations, vulnerability patches, and API migrations with an early focus on the Java language.
The OpenRewrite project is a semantic code search and transformation ecosystem for Java and other source code. It consists of a platform of prepackaged refactoring recipes for common framework migration and stylistic consistency tasks. It also allows you to define custom recipes to achieve a wide range of source code transformations.
OpenRewrite works by making changes to Lossless Semantic Trees (LST) representing your source code and printing the modified trees back into source code. You can then review the changes in your code and commit. Modifications to the LST are performed in Visitors and visitors are aggregated into Recipes. OpenRewrite recipes make minimally invasive changes to your source code that honor the original formatting.
For example, if you want to consistently use static imports across all of your test files, rather than doing this manually, you could use the UseStaticImport visitor provided by OpenRewrite. Applied to the file below, you can see the changes this generates.
// Before OpenRewrite
// After OpenRewrite
import static org.junit.Assert.assertTrue;
- If you want to learn how to use OpenRewrite to perform code transformations in a Maven or Gradle project, check out the Setting Up Your Project and Running Recipes Guide.
- If you want to learn how to create your own recipes, start with the Recipe Development Environment guide and then work through the Writing a Java Refactoring Recipe guide.