openrewrite

Applies reusable refactoring recipes to large codebases to perform framework upgrades and code transformations, ensuring the change process is auditable and reviewable.

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "openrewrite" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-3a1040373f52139295402e7b13d20d70ca023761f91caafea8a8d508ee7a889b. Verify the upstream source and version against https://skillhub.cn/skills/user_3c6cb52e/run-recipe-driven-codebase-migrations-and-framework-upgrades-with-reviewable-diffs-through-openrewrite before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

skillhub.cn

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Applies reusable refactoring recipes to large codebases to perform framework upgrades and code transformations, ensuring the change process is auditable and reviewable.

0 Downloads

Introduction source: third_party · user_3c6cb52e · 1.0.0 Reference

SkillPi interpretation

Tasks
OpenRewrite recipe test maintenance. Use when fixing test failures, import ordering issues, type validation problems, IDE warnings, or writing comprehensive recipe tests.
Audience
Not specified in the source documentation.
Inputs and outputs
Not specified in the source documentation.
Software, accounts and paid services
Not specified in the source documentation.
Limits and differences
Not specified in the source documentation.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: openrewrite
description: OpenRewrite recipe test maintenance. Use when fixing test failures, import ordering issues, type validation problems, IDE warnings, or writing comprehensive recipe tests.

OpenRewrite Recipe Development

This skill provides guidelines for developing OpenRewrite recipes and maintaining their tests, with a focus on import ordering issues.

Fixing Import Ordering Test Failures

🔧 OpenRewrite recipe tests often fail due to import order differences, not actual transformation issues.

Problem

OpenRewrite recipe tests fail with diffs showing only import order differences:

-import java.util.List;
-import org.assertj.core.api.Assertions;
+import org.assertj.core.api.Assertions;
+import java.util.List;

Root Cause

OpenRewrite manages imports automatically based on:

  • Existing imports in the file
  • JavaTemplate configuration
  • Import optimization rules
  • The order may differ from test expectations

Solution Approach

1. Fix the Recipe (if imports are missing)

Ensure your JavaTemplate is properly configured:

JavaTemplate template = JavaTemplate
    .builder("Your.template.code()")
    .imports(
        "org.assertj.core.api.Assertions",
        "org.eclipse.collections.impl.utility.Iterate"
    )
    .contextSensitive()  // Important for proper context handling
    .javaParser(JavaParser.fromJavaVersion()
        .classpath("assertj-core", "eclipse-collections", "eclipse-collections-api")
    )
    .build();

Don't forget to call:

maybeAddImport("org.assertj.core.api.Assertions");
maybeAddImport("org.eclipse.collections.impl.utility.Iterate");
maybeRemoveImport("old.package.OldClass");

2. Fix the Test Expectations

Accept the actual import order that OpenRewrite produces:

❌ Instead of forcing a specific order:

// DON'T expect a specific order you want
"import java.util.List;\n" +
"import org.assertj.core.api.Assertions;\n"

✅ Use the actual order OpenRewrite produces:

// DO accept the order OpenRewrite generates
"import org.assertj.core.api.Assertions;\n" +
"import org.eclipse.collections.impl.utility.Iterate;\n" +
"\n" +
"import java.util.List;\n"

3. Common Import Ordering Patterns

OpenRewrite typically orders imports as:

  1. Third-party packages (org.assertj, org.eclipse.collections, etc.)
  2. Blank line
  3. Java standard library (java.*, javax.*)
  4. Blank line (if static imports exist)
  5. Static imports

Quick Fix Steps

  1. Run the failing test and copy the actual output from the error message
  2. Replace the expected output in your test with the actual output
  3. Verify the transformation logic is correct (ignore import order)
  4. Re-run the test to confirm it passes

Note on ~~> Syntax

The ~~> prefix in test expectations is not standard in all codebases. It's used in some OpenRewrite projects to indicate "ignore everything before this line" but isn't recognized in all contexts. If you see it failing, remove it and use exact matching instead.

Example Fix

@Test
void replacesVerifyWithAssertJ() {
    rewriteRun(
        java(
            // Input
            """
            import org.eclipse.collections.impl.test.Verify;
            import java.util.List;

            class Test {
                void test() {
                    List<String> list = List.of("a", "b", "c");
                    Verify.assertCount(2, list, each -> each.length() > 0);
                }
            }
            """,
            // Expected output - use actual order from test failure
            """
            import org.assertj.core.api.Assertions;
            import org.eclipse.collections.impl.utility.Iterate;

            import java.util.List;

            class Test {
                void test() {
                    List<String> list = List.of("a", "b", "c");
                    Assertions.assertThat(Iterate.count(list, each -> each.length() > 0)).isEqualTo(2);
                }
            }
            """
        )
    );
}

Testing Best Practices

Type Validation in Tests

For tests involving custom types with incomplete type information, disable type validation as a last resort. Prefer specifying types that exist:

@Test
void withCustomType() {
    rewriteRun(
      spec -> spec.typeValidationOptions(TypeValidation.none()),
      java(
        // test code
      )
    );
}

Suppressing IDE Warnings in Tests

When testing code with intentional issues (that the recipe will fix), suppress IDE warnings:

// Single test method
@SuppressWarnings("RedundantCast")
@Test
void testRedundantCast() { ... }

// Multiple tests with same warning - move to class level
@SuppressWarnings({"ConstantConditions", "RedundantCast"})
class MyRecipeTest implements RewriteTest { ... }

Common suppressions: "RedundantCast", "ConstantConditions", "unused", "unchecked"

IDE Support with Language Comments

Add //language=java before string templates to enable IDE syntax highlighting.

When using java("before", "after") with no customization, place the comment before java:

//language=java
java(
    """
      public class Before { }
      """,
    """
      public class After { }
      """
)

When there's customization or multiple java() calls, place comments on individual strings:

spec -> spec.typeValidationOptions(TypeValidation.none()),
//language=java
java(
    """
      public class Test { }
      """
)

Do NOT add //language=java to JavaTemplate strings containing parameters like #{any()} or #{} — these aren't valid Java and will cause IDE errors.

Test Coverage

Ensure comprehensive coverage including:

  • Basic cases
  • Edge cases (custom types, fully qualified types)
  • Cases where the recipe should NOT make changes
  • Import handling scenarios
  • Formatting preservation

Maven POM Dependency Ordering

Maven POM files should follow a consistent dependency ordering structure. See the java:pom-ordering skill for detailed guidelines.

Current listing version

1.0.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value65/100

Highlights

  • 针对 OpenRewrite recipe 开发中的典型痛点(如 import ordering test failures)提供详细解决方案
  • 涵盖多种实际场景(type validation、IDE warnings、test coverage)的最佳实践
Trigger Precision85/100

Highlights

  • description 清晰列出适用场景(test failures, import ordering, type validation, IDE warnings, recipe tests)
  • 触发词精准,边界明确,不易与其他 Skill 混淆
Structure and Standards90/100

Highlights

  • Phase/步骤划分清晰(Problem -> Root Cause -> Solution -> Quick Fix Steps)
  • 包含多个端到端代码示例,便于理解与实施
  • frontmatter 完整,文档结构规范
Execution Reliability88/100

Highlights

  • 工具来源(OpenRewrite)及 API 使用方式明确
  • 覆盖主要异常处理(type validation, IDE warnings suppression)
  • 对不可逆操作(修改测试期望)有确认与验证建议
Evidence Verifiability (mean of the first four)82/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Not specified in the source documentation.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.