Skip to content
Back to skills

Java

ASecurity

This skill should be used when the user works with Java files (.java), asks about "records", "sealed classes", "streams", or discusses modern Java patterns.

  • 23 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 3, 2026
developmentjavarefactoring

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned November 9, 2026

npx -y skills add dean0x/devflow --skill java --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Java
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/dean0x-java/badge)](https://www.skillsdirectory.com/skills/dean0x-java)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: java
description: This skill should be used when the user works with Java files (.java), asks about "records", "sealed classes", "streams", or discusses modern Java patterns.
user-invocable: false
allowed-tools: Read, Grep, Glob
activation:
  file-patterns:
    - "**/*.java"
  exclude:
    - "**/build/**"
    - "**/target/**"
---

# Java Patterns

## Iron Law

> **FAVOR COMPOSITION OVER INHERITANCE**
>
> Delegation and interfaces over class hierarchies. Inheritance creates tight coupling,
> breaks encapsulation, and makes refactoring dangerous. Use interfaces for polymorphism,
> records for data, and sealed classes for restricted hierarchies. Extend only when the
> "is-a" relationship is genuinely invariant. — Effective Java, Item 18 [1, Item 18]

## Type System (Modern Java)

- Use records for pure data, not mutable POJOs with `setName()/getName()`. Validate in the compact constructor (`Objects.requireNonNull`).
- Use sealed interfaces for type hierarchies. They close the subtype set, so a switch over them is exhaustive with no `default`:

```java
public sealed interface Result<T> permits Success, Failure {
    record Success<T>(T value) implements Result<T> {}
    record Failure<T>(String error) implements Result<T> {}
}
switch (result) {
    case Success<User> s -> handleSuccess(s.value());
    case Failure<User> f -> handleError(f.error());
}
```

- Never return `null`: return `Optional`, and chain with `map`/`orElse`, not `isPresent()`/`get()`.
- No raw generic types: `List<User>`, not `List`.

## Error Handling

- Use try-with-resources for every `AutoCloseable` resource.
- Throw specific or unchecked custom exceptions that carry structured fields for context, not `throws Exception`.

## Immutability and Composition

- Prefer `List.of`/`Map.of`. Make defensive copies in constructors with `List.copyOf`.
- Do not share mutable state across threads (`HashMap`): use `ConcurrentHashMap` or immutable data.
- Constructor-inject collaborators. Never extend a class only to reuse methods; replace deep hierarchies (4+ levels) with interfaces and composition.
- Use streams for collection transforms.

## Extended References

- `references/sources.md` — Full bibliography
- `references/violations.md` — Common Java violations with citations
- `references/patterns.md` — Extended Java patterns with citations
- `references/detection.md` — Detection patterns for Java issues
- `references/modern-java.md` — Modern Java features (17-21+) with JEP citations

Files in this skill

  • SKILL.md4.8 KB
  • references/detection.md2.7 KB
  • references/modern-java.md10.1 KB
  • references/patterns.md9.7 KB
  • references/sources.md4.3 KB
  • references/violations.md7.1 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.