.kiro/steering/java-patterns.md
This file extends the common patterns with Java specific content.
record for value types (Java 16+)final by default — use mutable state only when requiredList.copyOf(), Map.copyOf()public record OrderSummary(Long id, String customerName, BigDecimal total) {}
instanceof (Java 16+)public sealed interface PaymentResult permits PaymentSuccess, PaymentFailure {}
record PaymentSuccess(String transactionId, BigDecimal amount) implements PaymentResult {}
record PaymentFailure(String errorCode, String message) implements PaymentResult {}
Always use constructor injection — never field injection:
// GOOD
public class NotificationService {
private final EmailSender emailSender;
public NotificationService(EmailSender emailSender) {
this.emailSender = emailSender;
}
}
// BAD — field injection
@Inject private EmailSender emailSender;
public interface OrderRepository {
Optional<Order> findById(Long id);
List<Order> findAll();
Order save(Order order);
void deleteById(Long id);
}
Optional<T> from finder methods that may have no resultmap(), flatMap(), orElseThrow() — never call get() without isPresent()Optional as a field type or method parameterRuntimeExceptionpublic class OrderNotFoundException extends RuntimeException {
public OrderNotFoundException(Long id) {
super("Order not found: id=" + id);
}
}
System.getenv("API_KEY")PreparedStatement, JPA, JDBC template)@NotNull, @NotBlank, @Size) on DTOs@Test
@DisplayName("findById returns order when exists")
void findById_existingOrder_returnsOrder() {
var order = new Order(1L, "Alice", BigDecimal.TEN);
when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
var result = orderService.findById(1L);
assertThat(result.customerName()).isEqualTo("Alice");
}
See agents: java-reviewer, java-build-resolver for Java-specific review and build error resolution.
See skills: springboot-patterns, jpa-patterns for framework-specific guidance.