128-java-generics

Refactor Java generics to improve type safety and maintainability.

423|90|Updated Feb 8, 2025
One-click install
npx skills add https://github.com/jabrena/cursor-rules-java --skill 128-java-generics
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: 128-java-generics
Source: https://github.com/jabrena/cursor-rules-java/tree/main/skills/128-java-generics
Command: npx skills add https://github.com/jabrena/cursor-rules-java --skill 128-java-generics

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Java generics are powerful but error-prone; this skill helps teams review and refactor code to enforce type safety, remove raw types, and improve API design with PECS, bounded types, and modern features like Records and sealed types.

Core Features & Use Cases

  • Assess and fix generics usage across codebases to eliminate unchecked warnings and ClassCastExceptions.
  • Rework APIs to use appropriate wildcards (PECS) and bounded type parameters for flexible yet safe interfaces.
  • Align code with modern Java features (Records, sealed types, pattern matching) to create expressive, maintainable generics APIs.

Quick Start

Run the multi-step generics refinement process on your codebase to produce a safer, more maintainable generics surface.

Frequently Asked Questions about 128-java-generics

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

FAQPage Schema
How do I fix unchecked warnings and ClassCastException in Java generics?

Fix unchecked warnings and ClassCastException in Java generics by removing raw types and applying bounded type parameters. This process assesses codebases to enforce type safety, ensuring all changes compile and tests pass for a documented, reusable generics pattern library.

How do I apply PECS wildcards to rework Java APIs for type safety?

Apply PECS wildcards to rework Java APIs by using Producer extends and Consumer super for flexible yet safe interfaces. This refactoring eliminates unchecked warnings, aligns code with bounded type parameters, and yields maintainable generics APIs across common codebases.

What is type erasure and how does it affect Java generics compilation?

Type erasure removes generic type information at runtime, affecting Java generics compilation by enforcing type safety only at compile time. This skill addresses type erasure implications by running Maven compilation before applying changes to ensure all code passes.

Can I integrate Java generics with Records and sealed types?

Yes, you can integrate Java generics with Records and sealed types to create expressive, maintainable APIs. This alignment with modern Java features like pattern matching ensures type safety while producing a documented, reusable generics pattern library for production systems.

What's the best way to eliminate raw types in a production Java codebase?

The best way to eliminate raw types in a production Java codebase is running a multi-step generics refinement process that assesses and fixes generics usage. It requires Maven compilation before applying changes and yields documented, reusable generics patterns with all tests passing.

When should I use bounded type parameters vs wildcards in Java generics?

Use bounded type parameters when a type must be referenced within a method, and wildcards when only consuming or producing values. This skill resolves generics-related issues by applying PECS guidelines and bounded types to improve type safety and maintainability across codebases.