Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

java-modern

/java-modern

Stosuje funkcje Javy 21+ w Spring Boot: rekordy, sealed classes, dopasowanie wzorców, wątki wirtualne i bloki tekstowe, także przy modernizacji istniejącego kodu.

Licencja
Apache-2.0
Ostatnia zmiana repozytorium
27.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 27.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

Co robi i kiedy

Stosuje funkcje Javy 21+ w Spring Boot: rekordy, sealed classes, dopasowanie wzorców, wątki wirtualne i bloki tekstowe, także przy modernizacji istniejącego kodu.

Oryginalny opis (EN)

Java 21+ features for Spring Boot - records, sealed classes, pattern matching, virtual threads, text blocks. Use when user asks about modern Java, Java 21 features, or when modernizing existing code.

Jak działa

  1. When to Use
  2. Records (Java 16+)
  3. Sealed Classes (Java 17+)
  4. Pattern Matching
  5. Virtual Threads (Java 21 + Spring Boot 4.x)
  6. Text Blocks (Java 15+)
  7. Sequenced Collections (Java 21)
  8. Quick Decision Guide
Sekcje z nagłówków SKILL.md, w oryginale · 316 linii, 977 słów
Podgląd SKILL.md
---
name: java-modern
description: Java 21+ features for Spring Boot - records, sealed classes, pattern matching, virtual threads, text blocks. Use when user asks about modern Java, Java 21 features, or when modernizing existing code.
---

# Java Modern Skill

Java 21+ language features and how to use them in Spring Boot applications.

## When to Use
- Building new Spring Boot 4.x applications (default to modern features)
- Modernizing/refactoring existing Java code
- User asks about records, sealed classes, pattern matching, virtual threads

---

## Records (Java 16+)

### DTOs

```java
// ✅ Records for request/response DTOs
public record ProductRequest(
    @NotBlank String name,
    @DecimalMin("0.0") BigDecimal price
) {}

public record ProductResponse(
    Long id,
    String name,
    BigDecimal price,
    LocalDateTime createdAt
) {
    public static ProductResponse from(Product entity) {
        return new ProductResponse(
            entity.getId(),
            entity.getName(),
            entity.getPrice(),
            entity.getCreatedAt()
        );
    }
}
```

### Configuration Properties

```java
// ✅ Records for config properties (Spring Boot 4.x)
@ConfigurationProperties(prefix = "app.notification")
public record NotificationProperties(
    boolean enabled,
    String fromEmail,
    Duration timeout,
    Retry retry
) {
    public record Retry(int maxAttempts, Duration delay) {}
}
```

### When NOT to use records
- JPA entities (Hibernate needs mutable objects, no-arg constructor)
- Classes that need inheritance
- Objects with mutable state

---

## Sealed Classes (Java 17+)

### Domain Modeling

```java
// ✅ Restrict payment types at compile time
public sealed interface PaymentResult
    permits PaymentSuccess, PaymentFailure, PaymentPending {
}

public record PaymentSuccess(String transactionId, BigDecimal amount)
    implements PaymentResult {}

public record PaymentFailure(String errorCode, String message)
    implements PaymentResult {}

public record PaymentPending(String referenceId)
    implements PaymentResult {}

// Usage in service
public PaymentResult processPayment(PaymentRequest request) {
    try {
        String txId = gateway.charge(request);
        return new PaymentSuccess(txId, request.amount());
    } catch (InsufficientFundsException e) {
        return new PaymentFailure("INSUFFICIENT_FUNDS", e.getMessage());
    }
}
```

### Event Hierarchies

```java
public sealed interface OrderEvent
    permits OrderCreated, OrderShipped, OrderCancelled {

    Long orderId();
    LocalDateTime timestamp();
}

public record OrderCreated(Long orderId, LocalDateTime timestamp, Long customerId)
    implements OrderEvent {}

public record OrderShipped(Long orderId, LocalDateTime timestamp, String trackingNumber)
    implements OrderEvent {}

public record OrderCancelled(Long orderId, LocalDateTime timestamp, String reason)
    implements OrderEvent {}
```

---

## Pattern Matching

### instanceof (Java 16+)

```java
// ❌ Old style
if (obj instanceof String) {
    String s = (String) obj;
    return s.length();
}

// ✅ Pattern matching
if (obj instanceof String s) {
    return s.length();
}
```

### switch (Java 21+)

```java
// ✅ Exhaustive switch with sealed types - compiler enforces all cases
public String handlePayment(PaymentResult result) {
    return switch (result) {
        case PaymentSuccess s -> "Paid: " + s.transactionId();
        case PaymentFailure f -> "Failed: " + f.message();
        case PaymentPending p -> "Pending: " + p.referenceId();
    };
}

// ✅ Guarded patterns
public BigDecimal calculateDiscount(Order order) {
    return switch (order) {
        case Order o when o.getTotal().compareTo(new BigDecimal("100")) > 0 ->
            o.getTotal().multiply(new BigDecimal("0.10"));
        case Order o when o.isLoyaltyMember() ->
            o.getTotal().multiply(new BigDecimal("0.05"));
        default -> BigDecimal.ZERO;
    };
}
```

### In Service Logic

```java
// Replacing if-else chains with pattern matching
public String processNotification(Object event) {
    return switch (event) {
        case OrderCreated o when o.total().compareTo(BigDecimal.valueOf(1000)) > 0 ->
            sendPriorityNotification(o);
        case OrderCreated o -> sendStandardNotification(o);
        case OrderCancelled c -> sendCancellationNotification(c);
        case PaymentReceived p -> sendPaymentConfirmation(p);
        default -> throw new IllegalArgumentException("Unknown event: " + event);
    };
}

// Pattern matching with instanceof — no more casting
public BigDecimal calculateShipping(Address address) {
    if (address instanceof DomesticAddress d) {
        return d.isExpressZone() ? DOMESTIC_EXPRESS : DOMESTIC_STANDARD;
    } else if (address instanceof InternationalAddress i) {
        return BASE_INTERNATIONAL.multiply(i.distanceFactor());
    }
    throw new IllegalArgumentException("Unknown address type");
}
```

### In Exception Handlers

```java
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handle(Exception ex) {
        return switch (ex) {
            case EntityNotFoundException e ->
                ResponseEntity.status(404).body(new ErrorResponse(e.getMessage()));
            case DataIntegrityViolationException e ->
                ResponseEntity.status(409).body(new ErrorResponse("Duplicate entry"));
            case ConstraintViolationException e ->
                ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
            default ->
                ResponseEntity.internalServerError().body(new ErrorResponse("Unexpected error"));
        };
    }
}
```

---

## Virtual Threads (Java 21 + Spring Boot 4.x)

```yaml
# application.yml - enables virtual threads for all request handling
spring:
  threads:
    virtual:
      enabled: true
```

This single property:
- Runs each HTTP request on a virtual thread (Tomcat)
- Makes `@Async` methods use virtual threads
- Allows blocking I/O (JDBC, RestClient) to scale like reactive code

```java
// ✅ Blocking code scales with virtual threads - no code changes needed
@Service
@Transactional(readOnly = true)
public class ReportService {
    private final OrderRepository orderRepository;
    private final RestClient externalApi;

    public ReportService(OrderRepository orderRepository, RestClient externalApi) {
        this.orderRepository = orderRepository;
        this.externalApi = externalApi;
    }

    public Report generate(Long customerId) {
        // Both blocking calls - on virtual threads this scales to thousands of concurrent requests
        List<Order> orders = orderRepository.findByCustomerId(customerId);
        CustomerData data = externalApi.get()
            .uri("/customers/{id}", customerId)
            .retrieve()
            .body(CustomerData.class);
        return new Report(orders, data);
    }
}
```

**Do NOT use with virtual threads:**
- `synchronized` blocks holding across blocking calls (use `ReentrantLock` instead)
- Thread-local heavy frameworks (check compatibility first)

---

## Text Blocks (Java 15+)

```java
// ✅ JPQL queries
@Query("""
    SELECT p FROM Product p
    WHERE p.active = true
      AND p.price BETWEEN :minPrice AND :maxPrice
    ORDER BY p.name
    """)
List<Product> findActiveInPriceRange(
    @Param("minPrice") BigDecimal min,
    @Param("maxPrice") BigDecimal max);

// ✅ JSON in tests
mockMvc.perform(post("/api/v1/products")
    .contentType(MediaType.APPLICATION_JSON)
    .content("""
        {
            "name": "Widget",
            "price": 10.00
        }
        """))
    .andExpect(status().isCreated());
```

---

## Sequenced Collections (Java 21)

`SequencedCollection` adds `getFirst()`, `getLast()`, and `reversed()` to ordered collections.

```java
// ❌ Before Java 21
String first = list.get(0);
String last = list.get(list.size() - 1);

// ✅ Java 21+
String first = list.getFirst();
String last = list.getLast();

// Reversed view (no copy)
for (var item : list.reversed()) {
    process(item);
}

// Works with all ordered collections: List, Deque, SortedSet, LinkedHashSet, LinkedHashMap
SortedSet<User> users = userRepository.findTopByOrderByCreatedAtDesc();
User newest = users.getFirst();
User oldest = users.getLast();
```

---

## Quick Decision Guide

| Feature | Use For | Avoid When |
|---------|---------|------------|
| **Records** | DTOs, config, value objects | JPA entities, mutable state |
| **Sealed classes** | Closed type hierarchies, domain events | Open extension needed |
| **Pattern matching switch** | Handling sealed types, replacing if-else chains | Simple equality checks |
| **Virtual threads** | High-concurrency blocking I/O | CPU-bound work, `synchronized` over I/O |
| **Text blocks** | JPQL queries, JSON test data, SQL | Single-line strings |
| **SequencedCollections** | First/last access on ordered collections | Unordered sets/maps |

Wersje

RepozytoriumWynikLicencja
Ta strona · piomin/claude-ai-spring-boot100Apache-2.0
piomin/claude-ai-quarkus100Apache-2.0

Migawka bazy z 13.11.2026 · wersja paczki f9111413df3a (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100java-expert
    Dostarcza wiedzę o funkcjach Javy 25: virtual threads, pattern matching, records, sealed classes, value types oraz Project Loom i Valhalla, przy współbieżności i optymalizacji wydajności.
  • 100programming-java
    Podpowiada nowoczesne praktyki Javy: rekordy, typy zapieczętowane, dopasowanie wzorców, wątki wirtualne i kontrakty błędów.
  • 90modernize-java
    Aktualizuje kod Java do nowoczesnych funkcji języka: records, switch expressions, pattern matching, virtual threads, sealed classes, text blocks i współczesne idiomy kolekcji.
  • 100java-pro
    Pisze i optymalizuje aplikacje Java 21+ i Spring Boot 3.x, w tym mikroserwisy, wirtualne wątki i wydajność.
  • 90java-conventions
    Pilnuje, by nowy kod Java stosował konwencje repozytorium: Effective Java, rekordy, sealed types, Optional, niezmienność, wątki wirtualne, Spring Boot i testy JUnit 5 z Mockito.
  • 100java
    Pomaga pisać aplikacje w Javie 17+ z nowoczesnymi funkcjami: records, sealed classes, strumienie, pattern matching, wyrażenia switch, Optional, kolekcje i generyki, także dla backendów Spring Boot.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.