Scripts2026年7月24日·1 分钟阅读

OpenRewrite — Automated Mass Refactoring for Source Code

Automated source code refactoring engine for Java and other JVM languages, applying safe, tested transformation recipes at scale.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
OpenRewrite Refactoring
直接安装命令
npx -y tokrepo@latest install c9f88758-86f9-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

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.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产