Introduction
OpenRewrite is an automated refactoring engine that applies deterministic transformations to source code at scale. It parses code into a lossless semantic tree (LST) preserving formatting and whitespace, then applies composable recipes to migrate frameworks, fix vulnerabilities, or upgrade language versions.
What OpenRewrite Does
- Migrates Java applications between major versions (Java 8 to 17, 17 to 21)
- Upgrades framework dependencies such as Spring Boot, JUnit, and Jakarta EE
- Applies security patches by replacing vulnerable API calls and dependencies
- Enforces coding standards and style rules across repositories
- Transforms build files (Maven POM, Gradle) alongside source code
Architecture Overview
OpenRewrite operates on a Lossless Semantic Tree (LST) that retains every character of the original file. Parsers convert source into LSTs, then recipes — composed of visitors — traverse and modify the tree. The printer serializes the LST back to text with minimal diff. Recipes are distributed as Maven artifacts and can be composed into migration plans.
Self-Hosting & Configuration
- Runs as a Maven or Gradle build plugin with no separate server required
- Configure active recipes and styles in pom.xml or build.gradle
- Recipe modules are pulled from Maven Central as regular dependencies
- The rewrite.yml file allows custom recipe composition and parameter overrides
- Moderne CLI provides optional commercial multi-repo orchestration
Key Features
- Lossless Semantic Tree preserves original formatting after transformations
- Pre-built recipes covering Java, Kotlin, Groovy, XML, YAML, and properties files
- Composable recipes — combine visitors into large migration plans
- Dry-run mode previews changes as diffs before applying
- Type-aware refactoring resolves method signatures, imports, and class hierarchies
Comparison with Similar Tools
- Error Prone — Catches bugs at compile time; OpenRewrite applies fixes across entire repos
- Refaster — Template-based refactoring in a single build; OpenRewrite supports cross-repo distribution
- Semgrep — Pattern-based linting; OpenRewrite uses a full semantic tree for type-aware transforms
- jscodeshift — JavaScript AST transforms; OpenRewrite targets JVM languages with format preservation
FAQ
Q: Does OpenRewrite modify code in place?
A: Yes, rewriteRun applies changes directly. Use rewriteDryRun to generate a diff report first.
Q: Can I write custom recipes? A: Yes. Recipes are Java classes extending visitor patterns on the LST. You can also compose existing recipes in YAML.
Q: Does it support languages beyond Java? A: It has parsers for Kotlin, Groovy, XML, YAML, JSON, and properties files.
Q: Will it break my formatting? A: The LST preserves original formatting. Transformations only change targeted code.