Skip to content

Commit

Permalink
Allows final entities with @GeneratedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Mar 1, 2024
1 parent aad009a commit 346c69b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Final entities with @GeneratedValue could not be verified. ([Issue 929](https://github.com/jqno/equalsverifier/issues/929))

## [3.15.7] - 2024-02-23

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static nl.jqno.equalsverifier.internal.util.Assert.assertTrue;

import java.lang.reflect.Modifier;
import java.util.function.Function;
import nl.jqno.equalsverifier.Warning;
import nl.jqno.equalsverifier.internal.exceptions.EqualsVerifierInternalBugException;
Expand Down Expand Up @@ -49,7 +50,8 @@ public void execute(

if (
!fieldIsUsed(referenceAccessor, copyAccessor, fieldAccessor, true) ||
!fieldIsLazy(fieldAccessor)
!fieldIsLazy(fieldAccessor) ||
Modifier.isFinal(type.getModifiers())
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public void gettersAreUsedAndProtected() {
EqualsVerifier.forClass(ProtectedJakartaLazyFieldContainer.class).verify();
}

@Test
public void finalEntitiesArentLazy() {
EqualsVerifier.forClass(FinalEntity.class).verify();
}

private void getterNotUsed(Class<?> type, String method, Warning... additionalWarnings) {
ExpectedException
.when(() -> EqualsVerifier.forClass(type).suppress(additionalWarnings).verify())
Expand Down Expand Up @@ -718,4 +723,29 @@ public int hashCode() {
return Objects.hash(getBasic());
}
}

@Entity
static final class FinalEntity {

@GeneratedValue // which is considered lazy
private int id;

public int getId() {
return id;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof FinalEntity)) {
return false;
}
FinalEntity other = (FinalEntity) obj;
return Objects.equals(getId(), other.getId());
}

@Override
public int hashCode() {
return Objects.hash(getId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public void gettersAreUsedAndProtected() {
EqualsVerifier.forClass(ProtectedJpaLazyFieldContainer.class).verify();
}

@Test
public void finalEntitiesArentLazy() {
EqualsVerifier.forClass(FinalEntity.class).verify();
}

private void getterNotUsed(Class<?> type, String method, Warning... additionalWarnings) {
ExpectedException
.when(() -> EqualsVerifier.forClass(type).suppress(additionalWarnings).verify())
Expand Down Expand Up @@ -705,4 +710,29 @@ public int hashCode() {
return Objects.hash(getBasic());
}
}

@Entity
static final class FinalEntity {

@GeneratedValue // which is considered lazy
private int id;

public int getId() {
return id;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof FinalEntity)) {
return false;
}
FinalEntity other = (FinalEntity) obj;
return Objects.equals(getId(), other.getId());
}

@Override
public int hashCode() {
return Objects.hash(getId());
}
}
}

0 comments on commit 346c69b

Please sign in to comment.