1 | package com.reallifedeveloper.common.infrastructure.persistence; | |
2 | ||
3 | import java.util.Optional; | |
4 | ||
5 | import org.checkerframework.checker.nullness.qual.Nullable; | |
6 | ||
7 | import jakarta.persistence.GeneratedValue; | |
8 | import jakarta.persistence.GenerationType; | |
9 | import jakarta.persistence.Id; | |
10 | import jakarta.persistence.MappedSuperclass; | |
11 | ||
12 | /** | |
13 | * Base class for all JPA entities. | |
14 | * | |
15 | * @author RealLifeDeveloper | |
16 | * | |
17 | * @param <ID> the type of the primary key | |
18 | */ | |
19 | @MappedSuperclass | |
20 | public class BaseJpaEntity<ID> { | |
21 | ||
22 | @Id | |
23 | @GeneratedValue(strategy = GenerationType.AUTO) | |
24 | private final @Nullable ID id; | |
25 | ||
26 | /** | |
27 | * Creates a new {@code AbstractJpaEntity} with null ID. | |
28 | */ | |
29 | protected BaseJpaEntity() { | |
30 | this(null); | |
31 | } | |
32 | ||
33 | /** | |
34 | * Creates a new {@code AbstractJpaEntity} with the given ID. | |
35 | * | |
36 | * @param id the ID of the new entity, may be {@code null} | |
37 | */ | |
38 | protected BaseJpaEntity(@Nullable ID id) { | |
39 | this.id = id; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Gives the ID of this {@code AbstractJpaEntity}. | |
44 | * | |
45 | * @return the ID of this entity | |
46 | */ | |
47 | public Optional<ID> id() { | |
48 |
1
1. id : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/BaseJpaEntity::id → KILLED |
return Optional.ofNullable(id); |
49 | } | |
50 | ||
51 | /** | |
52 | * Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW). | |
53 | * | |
54 | * @see <a href="https://wiki.sei.cmu.edu/confluence/display/java/OBJ11-J.+Be+wary+of+letting+constructors+throw+exceptions"> | |
55 | * Explanation of finalizer attack</a> | |
56 | */ | |
57 | @Override | |
58 | @Deprecated | |
59 | @SuppressWarnings({ "checkstyle:NoFinalizer", "PMD.EmptyFinalizer" }) | |
60 | protected final void finalize() throws Throwable { | |
61 | // Do nothing | |
62 | } | |
63 | } | |
Mutations | ||
48 |
1.1 |