141-java-refactoring-with-modern-features

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Modern Java Development Guidelines (Java 8+)

现代Java开发指南(Java 8+)

Identify and apply modern Java (Java 8+) refactoring opportunities to improve readability, maintainability, and performance.
What is covered in this Skill?
  • Lambda expressions and method references (over anonymous classes)
  • Stream API for declarative collection processing
  • Optional
    for null-safe APIs
  • java.time
    API (replacing
    Date
    /
    Calendar
    )
  • Default interface methods,
    var
    type inference
  • Unmodifiable collection factory methods (
    List.of()
    ,
    Set.of()
    ,
    Map.of()
    )
  • Text blocks for multi-line strings
  • Java 25 Flexible Constructor Bodies (JEP 513)
  • Java 25 Module Import Declarations (JEP 511)
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
识别并应用现代Java(Java 8+)重构机会,以提升代码的可读性、可维护性和性能。
本Skill涵盖哪些内容?
  • Lambda表达式与方法引用(替代匿名类)
  • 用于声明式集合处理的Stream API
  • 实现空安全API的
    Optional
  • java.time
    API(替代
    Date
    /
    Calendar
    )
  • 默认接口方法、
    var
    类型推断
  • 不可变集合工厂方法(
    List.of()
    、
    Set.of()
    、
    Map.of()
    )
  • 用于多行字符串的文本块
  • Java 25灵活构造函数体(JEP 513)
  • Java 25模块导入声明(JEP 511)
范围:参考文档按每个核心领域的示例(良好/不良代码模式)组织。根据适用的示例应用建议。

Constraints

约束条件

Before applying any modern Java refactoring, ensure the project compiles. If compilation fails, stop immediately — do not proceed until the project compiles successfully. After applying improvements, run full verification.
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any changes
  • SAFETY: If compilation fails, stop immediately — do not proceed until the project compiles successfully
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each modern Java feature
  • EDGE CASE: If request scope is ambiguous, stop and ask a clarifying question before applying changes
  • EDGE CASE: If required inputs, files, or tooling are missing, report what is missing and ask whether to proceed with setup guidance
在应用任何现代Java重构之前,确保项目能够编译。如果编译失败,请立即停止——在项目编译成功前不要继续。应用改进后,运行完整验证。
  • 强制要求:在应用任何更改前运行
    ./mvnw compile
    或
    mvn compile
  • 安全要求:如果编译失败,立即停止——在项目编译成功前不要继续
  • 验证要求:应用改进后运行
    ./mvnw clean verify
    或
    mvn clean verify
  • 应用前:阅读参考文档,了解每个现代Java特性的详细良好/不良示例、约束条件和防护措施
  • 边缘情况:如果请求范围不明确,请停止并提出澄清问题后再应用更改
  • 边缘情况:如果缺少所需的输入、文件或工具,请报告缺失内容并询问是否需要提供设置指导

When to use this skill

何时使用本Skill

  • Review Java code for modern Java development
  • Apply best practices for modern Java development in Java code
  • Modernize Java code with records pattern matching or switch expressions
  • Replace legacy idioms with modern Java features
  • Adopt Java 8+ language features safely
  • 为现代Java开发评审Java代码
  • 在Java代码中应用现代Java开发最佳实践
  • 使用记录模式匹配或switch表达式现代化Java代码
  • 用现代Java特性替代传统写法
  • 安全采用Java 8+语言特性

Workflow

工作流程

  1. Compile project before modernization
Run
./mvnw compile
or
mvn compile
and stop immediately if compilation fails.
  1. Read modern-Java reference and identify candidates
Read
references/141-java-refactoring-with-modern-features.md
and identify high-value opportunities to adopt modern Java features.
  1. Apply modern Java refactorings
Refactor incrementally using appropriate language/library features while preserving behavior and readability.
  1. Verify with full build
Run
./mvnw clean verify
or
mvn clean verify
after applying improvements.
  1. 现代化前编译项目
运行
./mvnw compile
或
mvn compile
,如果编译失败立即停止。
  1. 阅读现代Java参考文档并识别候选重构点
阅读
references/141-java-refactoring-with-modern-features.md
,识别采用现代Java特性的高价值机会。
  1. 应用现代Java重构
使用合适的语言/库特性逐步重构,同时保留代码行为和可读性。
  1. 通过完整构建验证
应用改进后运行
./mvnw clean verify
或
mvn clean verify
。

Reference

参考文档

For detailed guidance, examples, and constraints, see references/141-java-refactoring-with-modern-features.md.
如需详细指南、示例和约束条件,请查看references/141-java-refactoring-with-modern-features.md。