java-patterns

Codify Java 17 and Spring best practices for dependency injection, Stream API, Optional, and exception handling.

9|2|Updated Dec 5, 2025
One-click install
npx skills add https://github.com/Zate/cc-plugins --skill java-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: java-patterns
Source: https://github.com/Zate/cc-plugins/tree/main/plugins/devloop/skills/java-patterns
Command: npx skills add https://github.com/Zate/cc-plugins --skill java-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Building robust Java applications requires adherence to modern patterns and best practices, especially with the Spring framework. Without clear guidance, code can become hard to maintain, test, and scale.

Core Features & Use Cases

  • Spring Dependency Injection: Master constructor injection and configuration classes for clean, testable code.
  • Stream API: Efficiently filter, transform, and aggregate collections with best practices for readability.
  • Optional & Exception Handling: Properly manage nullable values and design custom, global exception handlers for robust error management.
  • Records & Builder Pattern: Leverage modern Java features for immutable data classes and complex object creation.
  • Testing with JUnit: Write effective unit and integration tests using Mockito and Spring Boot's testing utilities.
  • Use Case: Refactor a legacy Java method using Streams, implement a global exception handler in Spring, or write comprehensive unit tests for a service layer.

Quick Start

Refactor the given Java code to use the Stream API for filtering and transforming a list of objects, and handle potential null values with Optional.

Frequently Asked Questions about java-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor Java code to use the Stream API for filtering and transforming collections?

The Stream API provides functional operations to filter, map, and aggregate collections efficiently. Use `filter()` to select elements, `map()` to transform them, and terminal operations like `collect()` to gather results—this approach is more readable and maintainable than nested loops, especially for complex data transformations in Java 17+.

What's the best way to handle null values in Java with Optional?

Optional wraps potentially null values and replaces null checks with explicit methods like `isPresent()`, `orElse()`, and `ifPresent()`. This pattern prevents NullPointerExceptions, makes intent clear, and is the idiomatic approach for Java 17+ codebases to manage nullable data safely.

How do I implement dependency injection in Spring with constructor injection?

Constructor injection passes dependencies as parameters to a class constructor, making them immutable and testable. Spring automatically wires beans into constructors, eliminating the need for field injection and reducing coupling—this pattern is the modern best practice for Spring projects.

Can I use custom global exception handlers in Spring applications?

Yes. Spring's `@ControllerAdvice` and `@ExceptionHandler` annotations create global exception handlers that centralize error management across all controllers. This approach ensures consistent error responses, simplifies testing, and decouples exception logic from business code.

What testing strategies work best for Java service layers with Spring Boot?

Use JUnit for unit tests with Mockito to mock dependencies, and Spring Boot's `@SpringBootTest` for integration tests. This combination isolates business logic, verifies service behavior, and validates Spring configuration—essential for comprehensive test coverage in modern Java applications.

When should I use Records and the Builder pattern in Java?

Records create immutable data classes with minimal boilerplate for simple value holders. The Builder pattern suits complex objects with many optional fields, enabling readable construction and validation. Together, they modernize data modeling in Java 17+ while improving maintainability.