Get the FREE Ultimate OpenClaw Setup Guide β†’

141-java-refactoring-with-modern-features

Scanned
npx machina-cli add skill jabrena/cursor-rules-java/141-java-refactoring-with-modern-features --openclaw
Files (1)
SKILL.md
2.4 KB

Modern Java Development Guidelines (Java 8+)

Identify and apply modern Java (Java 8+) refactoring opportunities to improve readability, maintainability, and performance.

Core areas: 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()), CompletableFuture for composable async programming, text blocks for multi-line strings, Java 25 Flexible Constructor Bodies (JEP 513), and Java 25 Module Import Declarations (JEP 511).

Prerequisites: Run ./mvnw compile or mvn compile before applying any changes. If compilation fails, stop immediately β€” do not proceed until the project compiles successfully.

Multi-step scope: Step 1 validates compilation. Step 2 categorizes refactoring opportunities by impact (CRITICAL, MAINTAINABILITY, PERFORMANCE, READABILITY). Step 3 applies lambda/method-reference conversions. Step 4 replaces imperative loops with Stream API pipelines. Step 5 introduces Optional for null-safe method returns. Step 6 migrates legacy date/time to java.time. Step 7 adopts collection factory methods and async CompletableFuture patterns. Step 8 applies text blocks, var, and Java 25 features where applicable. Step 9 runs ./mvnw clean verify to confirm all tests pass after changes.

Before applying changes: Read the reference for detailed good/bad examples, constraints, and safeguards for each modern Java feature.

Reference

For detailed guidance, examples, and constraints, see references/141-java-refactoring-with-modern-features.md.

Source

git clone https://github.com/jabrena/cursor-rules-java/blob/main/skills/141-java-refactoring-with-modern-features/SKILL.mdView on GitHub

Overview

This skill guides updating Java code to adopt modern features such as lambdas, streams, Optional, and java.time, along with factory methods, CompletableFuture, text blocks, var inference, and Java 25 improvements. It emphasizes safe, incremental refactoring with a strict compilation-check workflow to boost readability, maintainability, and performance.

How This Skill Works

It scans code to identify modernization opportunities aligned with the guidelines and applies changes in logical stages. The workflow starts with lambda/method reference conversions, then replaces loops with Stream API, adds Optional where appropriate, migrates date/time to java.time, adopts factory methods and CompletableFuture, and then applies text blocks, var inference, and eligible Java 25 features, with a mandatory mvn compile check before changes.

When to Use It

  • Your code contains anonymous inner classes that can be replaced with lambdas or method references.
  • You want to replace imperative loops over collections with Stream API pipelines for readability and performance.
  • Null-safety would benefit from Optional-based APIs and return values.
  • Legacy Date/Calendar usage should be migrated to java.time for better API design and correctness.
  • You are ready to adopt collection factory methods, CompletableFuture patterns, text blocks, var, and select Java 25 features where applicable.

Quick Start

  1. Step 1: Run mvn compile to ensure the project builds before making changes.
  2. Step 2: Identify candidate refactors: anonymous classes -> lambdas, loops -> streams, and legacy date/time usage.
  3. Step 3: Implement targeted changes in small commits, then run mvn clean verify to validate tests.

Best Practices

  • Run mvn compile before applying changes and stop if compilation fails.
  • Apply changes incrementally in small, testable commits to keep the build green.
  • Favor lambda expressions and method references over anonymous classes for clarity.
  • Use unmodifiable factory methods (List.of, Set.of, Map.of) when immutability fits the design.
  • Add or update tests and run mvn clean verify to ensure behavior remains correct after refactors.

Example Use Cases

  • Convert an anonymous Comparator instance to a lambda expression to simplify sorting logic.
  • Replace a for-each loop collecting integers with a Stream.map(...).collect(...) pipeline.
  • Change a method that returns null to return Optional<T> and adjust callers accordingly.
  • Migrate Date/Calendar usage to java.time.LocalDateTime or ZonedDateTime with proper ZoneId.
  • Use List.of()/Map.of() for immutable collections and refactor async tasks to CompletableFuture chains.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers β†—