$npx -y skills add softspark/ai-toolkit --skill java-rulesJava coding rules: style, patterns, security, testing. Triggers: .java, pom.xml, build.gradle, Spring, Spring Boot, JPA, Hibernate, JUnit, Maven, Gradle.
| 1 | # Java Rules |
| 2 | |
| 3 | These rules come from `app/rules/java/` in ai-toolkit. They cover |
| 4 | the project's standards for coding style, frameworks, patterns, |
| 5 | security, and testing in Java. Apply them when writing or |
| 6 | reviewing Java code. |
| 7 | |
| 8 | # Java Coding Style |
| 9 | |
| 10 | ## Naming |
| 11 | - PascalCase: classes, interfaces, enums, records, annotations. |
| 12 | - camelCase: methods, variables, parameters. |
| 13 | - UPPER_SNAKE: constants (`static final`). |
| 14 | - Package names: lowercase, dot-separated, reverse domain (`com.company.project`). |
| 15 | - No Hungarian notation. No `I` prefix on interfaces. |
| 16 | |
| 17 | ## Modern Java (17+) |
| 18 | - Use `record` for immutable data carriers. No need for Lombok in most cases. |
| 19 | - Use `sealed` classes/interfaces for restricted hierarchies. |
| 20 | - Use pattern matching: `if (obj instanceof String s)` instead of cast. |
| 21 | - Use `switch` expressions with arrow syntax and exhaustiveness. |
| 22 | - Use text blocks (`"""`) for multiline strings (SQL, JSON, HTML). |
| 23 | |
| 24 | ## Types |
| 25 | - Use `var` for local variables when the type is obvious from the right-hand side. |
| 26 | - Use `Optional<T>` for return types that may be absent. Never for fields or params. |
| 27 | - Prefer `List.of()`, `Map.of()`, `Set.of()` for immutable collections. |
| 28 | - Use `Stream` for collection transformations. Avoid streams for simple iterations. |
| 29 | |
| 30 | ## Classes |
| 31 | - Prefer composition over inheritance. Use interfaces for abstraction. |
| 32 | - Keep classes focused: single responsibility. |
| 33 | - Use `final` on classes not designed for extension. |
| 34 | - Use `private` constructors + static factory methods for controlled instantiation. |
| 35 | - Records over POJOs for value types. Lombok only if records are insufficient. |
| 36 | |
| 37 | ## Methods |
| 38 | - Max 20-30 lines per method. Extract when longer. |
| 39 | - Use `@Override` on every overridden method. |
| 40 | - Return empty collections over `null`. Use `Collections.emptyList()` or `List.of()`. |
| 41 | - Avoid checked exceptions for programming errors. Use runtime exceptions. |
| 42 | |
| 43 | ## Formatting |
| 44 | - Use project formatter (Google Java Format or IDE-configured). |
| 45 | - Use `@SuppressWarnings` sparingly and with specific warning names. |
| 46 | - Use `final` for parameters and local variables where practical. |
| 47 | |
| 48 | ## Nullability |
| 49 | - Annotate with `@Nullable` / `@NonNull` from JSpecify or JetBrains. |
| 50 | - Use `Objects.requireNonNull()` at public API boundaries. |
| 51 | - Never return `null` from collections or arrays. Return empty. |
| 52 | - Use `Optional` for genuinely optional return values. |
| 53 | |
| 54 | ## Documentation |
| 55 | - Javadoc on all public classes and methods. |
| 56 | - Use `@param`, `@return`, `@throws` tags for public API methods. |
| 57 | - Skip Javadoc for obvious getters, `toString()`, and `equals()`. |
| 58 | |
| 59 | # Java Frameworks |
| 60 | |
| 61 | ## Spring Boot |
| 62 | - Use Spring Boot 3+ with Java 17+ minimum. |
| 63 | - Use `@RestController` for REST APIs. Return `ResponseEntity` for status control. |
| 64 | - Use `@Valid` + Jakarta Bean Validation for request validation. |
| 65 | - Use profiles (`@Profile`) for environment-specific configuration. |
| 66 | - Use `application.yml` over `application.properties` for readability. |
| 67 | - Externalize config: env vars > config files > hardcoded defaults. |
| 68 | |
| 69 | ## Spring Data JPA |
| 70 | - Use repository interfaces extending `JpaRepository`. |
| 71 | - Use `@Query` with JPQL for custom queries. Use native queries only when needed. |
| 72 | - Use `@EntityGraph` to prevent N+1 queries in associations. |
| 73 | - Use `Specification` for dynamic query building. |
| 74 | - Always use `@Transactional` at the service layer, not repository. |
| 75 | |
| 76 | ## Spring Security |
| 77 | - Use `SecurityFilterChain` bean configuration (not `WebSecurityConfigurerAdapter`). |
| 78 | - Use `@PreAuthorize` / `@Secured` for method-level authorization. |
| 79 | - Use BCrypt for password encoding: `new BCryptPasswordEncoder()`. |
| 80 | - Configure CORS, CSRF, and session management explicitly. |
| 81 | - Use OAuth2 Resource Server for JWT validation in APIs. |
| 82 | |
| 83 | ## Hibernate / JPA |
| 84 | - Use `FetchType.LAZY` by default on all associations. |
| 85 | - Use `@BatchSize` or `@Fetch(FetchMode.SUBSELECT)` to avoid N+1. |
| 86 | - Use `@Version` for optimistic locking on entities. |
| 87 | - Use DTOs (records) for read queries. Do not expose entities in APIs. |
| 88 | - Use Flyway or Liquibase for schema migrations. |
| 89 | |
| 90 | ## Quarkus / Micronaut |
| 91 | - Use for microservices and serverless where startup time matters. |
| 92 | - Use compile-time DI (Micronaut) or build-time optimization (Quarkus). |
| 93 | - Use reactive patterns with Mutiny (Quarkus) or Reactor (Micronaut). |
| 94 | - Use native image builds with GraalVM for production deployments. |
| 95 | |
| 96 | ## Build Tools |
| 97 | - Use Gradle (Kotlin DSL) for new projects. Maven for enterprise legacy. |
| 98 | - Use dependency management to unify versions across modules. |
| 99 | - Use Bill of Materials (BOM) imports for consistent Spring versions. |
| 100 | - Use Spotless or Checkstyle for enforced code formatting. |
| 101 | |
| 102 | ## Logging |
| 103 | - Use SLF4J facade with Logback or Log4j2 backend. |
| 104 | - Use structured logging with MDC for correlation IDs. |
| 105 | - Use parameterized logging: `log.info("User {} created", userId)`. |
| 106 | - Never log |