--- applyTo: "**/*.java, **/pom.xml, **/.checkstyle*, **/checkstyle*.xml" --- ## Context This instruction file enforces Java code quality standards across all Java modules: Google Java Style Guide formatting, static analysis tooling (SpotBugs, Checkstyle, SonarLint), test coverage mandates (JaCoCo), and security vulnerability scanning (OWASP Dependency-Check). These rules apply to both legacy Spring MVC modules and modern Spring Boot modules. They complement the stack-specific instructions in `spring-boot.instructions.md` and `java-legacy.instructions.md`. --- ## Google Java Style Guide — Enforced Rules All generated Java code must comply with the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). ### Formatting Rules | Rule | Value | |------|-------| | Indentation | 2 spaces (no tabs) | | Continuation indent | +4 spaces | | Column limit | 100 characters | | Brace style | Egyptian (K&R): opening brace on same line | | Braces | Always present — even for single-statement blocks | | Blank lines between members | 1 blank line | | Blank line after class opening brace | None | | Wildcard imports | Forbidden | | Static imports | Grouped first, then all others | | `var` | Allowed for local variables where the type is clear from the right-hand side | ### Naming | Element | Convention | Example | |---------|-----------|---------| | Class | `UpperCamelCase` | `CustomerService` | | Method | `lowerCamelCase` | `findActiveCustomers` | | Variable | `lowerCamelCase` | `orderId` | | Constant | `UPPER_SNAKE_CASE` | `MAX_RETRY_COUNT` | | Type parameter | Single uppercase or `UpperCamelCase` + `T` | `T`, `CustomerT` | | Package | All lowercase, dot-separated | `com.example.order` | | Acronyms | Treated as words | `HttpUrl`, not `HTTPUrl`; `JsonParser`, not `JSONParser` | ### Javadoc Requirements ```java /** * Processes a customer payment request and returns the transaction result. * *
If the customer's balance is insufficient, the transaction is declined
* and a {@link PaymentDeclinedException} is thrown.
*
* @param customerId the UUID of the customer making the payment
* @param amount the payment amount; must be positive
* @return the completed transaction result
* @throws PaymentDeclinedException if the payment cannot be processed
* @throws IllegalArgumentException if {@code amount} is null or non-positive
*/
public TransactionResult processPayment(UUID customerId, BigDecimal amount) { ... }
```
- `@param` for every parameter
- `@return` for every non-void method
- `@throws` for every checked exception and significant runtime exception
- Use `{@code ...}` for inline code references
- Use `{@link ...}` for type references
- Do not write Javadoc that merely restates the method signature
---
## Code Quality — Static Analysis Tools
### Checkstyle (Google Checks)
Checkstyle is configured with `google_checks.xml` (provided by the `checkstyle` library). Run locally:
```bash
mvn checkstyle:check
```
Common violations to eliminate before committing:
- Line length > 100 characters
- Missing Javadoc on public methods
- Wildcard imports
- Tabs instead of spaces
- Magic numbers (use named constants)
- Missing `@Override` annotation
### SpotBugs
SpotBugs performs bytecode-level static analysis. Run locally:
```bash
mvn spotbugs:check
```
SpotBugs bug categories to treat as build failures:
| Category | Examples |
|----------|---------|
| `CORRECTNESS` | Null dereference, infinite loop, integer overflow |
| `SECURITY` | SQL injection, path traversal, hardcoded password |
| `BAD_PRACTICE` | Unclosed streams, ignored return values |
| `PERFORMANCE` | Unnecessary object creation in loops |
SpotBugs suppression — only with justification:
```java
@SuppressFBWarnings(
value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE",
justification = "findById is called after an existence check; null is impossible here"
)
```
### PMD (Optional)
If PMD is configured, enforce:
- `UnusedImports`, `UnusedLocalVariable`
- `EmptyCatchBlock` — must always have a logged message or re-throw
- `SystemPrintln` — use SLF4J
- `AvoidDeeplyNestedIfStmts` — extract methods instead
---
## JaCoCo — Test Coverage
Minimum thresholds enforced at build time:
| Metric | Threshold |
|--------|----------|
| Line coverage (business logic) | 80% |
| Branch coverage (business logic) | 70% |
| Line coverage (overall) | 70% |
Exclude from coverage measurement:
- `**/*MapperImpl.java` (MapStruct generated)
- `**/generated/**`
- `**/*Application.java`
- `**/*Config.java` (pure Spring config classes)
- `**/dto/**`, `**/model/**` (pure data holders with no logic)
```xml