Use when you need to review, improve, or refactor Java code for generics quality — including avoiding raw types, applying the PECS (Producer Extends Consumer Super) principle for wildcards, using bounded type parameters, designing effective generic methods, leveraging the diamond operator, understanding type erasure implications, handling generic inheritance correctly, preventing heap pollution with @SafeVarargs, and integrating generics with modern Java features like Records, sealed types, and pattern matching. This should trigger for requests such as Improve the code with Generics; Apply Generics; Refactor the code with Generics. Part of cursor-rules-java project
85
81%
Does it follow best practices?
Impact
—
No eval scenarios have been run
Passed
No known issues
Review and improve Java code using comprehensive generics best practices that enforce compile-time type safety and enable flexible, reusable APIs.
What is covered in this Skill?
? extends for producers, ? super for consumers@SafeVarargs for heap pollution preventionComparator<? super T>, Function<? super T, ? extends R>TypeReference/TypeTokenT, E, K/V, ?), typesafe heterogeneous containersScope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
Before applying any generics changes, ensure the project compiles. If compilation fails, stop immediately — do not proceed until resolved. After applying improvements, run full verification.
./mvnw compile or mvn compile before applying any change./mvnw clean verify or mvn clean verify after applying improvementsRun ./mvnw compile or mvn compile and stop immediately if compilation fails.
Read references/128-java-generics.md and identify raw types, unsafe casts, wildcard misuse, and API variance opportunities.
Implement selected generic type and API improvements while preserving behavior.
Run ./mvnw clean verify or mvn clean verify after applying improvements.
For detailed guidance, examples, and constraints, see references/128-java-generics.md.
762cb86
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.