128-java-generics

Featured

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; Improve generic type safety in Java APIs; Fix raw types and unchecked casts in Java code. Part of Plinth Toolkit

AI & Automation 423 stars 89 forks Updated 3 days ago Apache-2.0

Install

View on GitHub

Quality Score: 93/100

Stars 20%
87
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Java Generics Best Practices 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?** - Type safety: avoiding raw types, eliminating unsafe casts - Code reusability: generic methods and types for multiple type contexts - PECS wildcards: `? extends` for producers, `? super` for consumers - Diamond operator for type inference - Type erasure awareness: type tokens, factory patterns, array creation - Generic inheritance and variance: invariance, covariance, contravariance - `@SafeVarargs` for heap pollution prevention - Wildcard capture helpers, self-bounded generics (CRTP) for fluent builders - Proper wildcard API design: `Comparator<? super T>`, `Function<? super T, ? extends R>` - Arrays-vs-generics covariance pitfalls, serialization with `TypeReference`/`TypeToken` - Generic naming conventions (`T`, `E`, `K/V`, `?`), typesafe heterogeneous containers - Integration with Records, sealed types, and pattern matching **Scope:** The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples. ## Constraints 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. - **MANDATORY**: Run `./mvnw compile` or `mvn compile` before applying any change - **S...

Details

Author
jabrena
Repository
jabrena/plinth
Created
1 years ago
Last Updated
3 days ago
Language
Java
License
Apache-2.0

Integrates with

Bundled in these plugins

Similar Skills

Semantically similar based on skill content — not just same category

AI & Automation Featured

122-java-type-design

Use when you need to review, improve, or refactor Java code for type design quality — including establishing clear type hierarchies, applying consistent naming conventions, eliminating primitive obsession with domain-specific value objects, leveraging generic type parameters, creating type-safe wrappers, designing fluent interfaces, ensuring precision-appropriate numeric types (BigDecimal for financial calculations), and improving type contrast through interfaces and method signature alignment. This should trigger for requests such as Review Java code for type design; Improve type design in Java code; Fix primitive obsession in Java code; Create value objects in Java code. Part of Plinth Toolkit

423 Updated 3 days ago
jabrena
Code & Development Featured

141-java-refactoring-with-modern-features

Use when you need to refactor Java code to adopt modern Java features (Java 8+) — including migrating anonymous classes to lambdas, replacing Iterator loops with Stream API, adopting Optional for null safety, switching from legacy Date/Calendar to java.time, using collection factory methods, applying text blocks, var inference, or leveraging Java 25 features like flexible constructor bodies and module import declarations. This should trigger for requests such as 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. Part of Plinth Toolkit

423 Updated 3 days ago
jabrena
Code & Development Featured

124-java-secure-coding

Use when you need to apply Java secure coding best practices — including validating untrusted inputs, defending against injection attacks with parameterized queries, minimizing attack surface via least privilege, applying strong cryptographic algorithms, handling exceptions securely without exposing sensitive data, managing secrets at runtime, avoiding unsafe deserialization, and encoding output to prevent XSS. This should trigger for requests such as Review Java code for secure coding; Find input validation risks in Java code; Review Java code for injection vulnerabilities; Improve secure error handling in Java services; Harden Java code against common security flaws. Part of Plinth Toolkit

423 Updated 3 days ago
jabrena