From 7c85ef33079af314323241abb309914ae09d8c1b Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Thu, 11 Jun 2020 13:07:58 +0200 Subject: [PATCH] #315: support classes that have fields but don't override equals --- CHANGELOG.md | 3 +++ .../fieldchecks/MutableStateFieldCheck.java | 6 +++--- .../extended_contract/DontOverrideEqualsTest.java | 14 +++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 791f79e87..d4f339b80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial support for Java 14's records and their [new equality invariant](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Record.html). - Support for JSR305's `ParametersAreNonnullByDefault` annotation. ([Issue 308](https://github.com/jqno/equalsverifier/issues/308)) + ### Changed - Renames the project's main branch to `main`. - Replaces all references to the word 'black' to 'blue', including those in internal (but accessible) API's. + ### Fixed +- Testing a class with that has fields but doesn't override `equals` causes "Mutability: equals depends on mutable field" error. ([Issue 315](https://github.com/jqno/equalsverifier/issues/315)) ## [3.3] - 2020-05-14 ### Added diff --git a/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java b/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java index f717bdb0f..654417117 100644 --- a/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java +++ b/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java @@ -31,11 +31,11 @@ public void execute(FieldAccessor referenceAccessor, FieldAccessor changedAccess Object reference = referenceAccessor.getObject(); Object changed = changedAccessor.getObject(); + boolean equalBefore = reference.equals(changed); changedAccessor.changeField(prefabValues, typeTag); + boolean equalAfter = reference.equals(changed); - boolean equalsChanged = !reference.equals(changed); - - if (equalsChanged && !referenceAccessor.fieldIsFinal()) { + if (equalBefore && !equalAfter && !referenceAccessor.fieldIsFinal()) { fail( Formatter.of( "Mutability: equals depends on mutable field %%.", diff --git a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/DontOverrideEqualsTest.java b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/DontOverrideEqualsTest.java index 00fae4a8f..941b7fc2b 100644 --- a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/DontOverrideEqualsTest.java +++ b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/DontOverrideEqualsTest.java @@ -35,6 +35,15 @@ public void succeed_whenEqualsIsInheritedButNotFromObject() { .verify(); } + @Test + public void + succeed_whenClassIsAnExceptionAndEqualsIsInheritedDirectlyFromObject_givenDetailMessageIsIgnored() { + EqualsVerifier.forClass(SimpleException.class) + .suppress(Warning.INHERITED_DIRECTLY_FROM_OBJECT) + .suppress(Warning.ALL_FIELDS_SHOULD_BE_USED) + .verify(); + } + static final class NoEqualsNoHashCodeMethod {} static final class InheritedEqualsAndHashCodeMethod extends Point { @@ -43,7 +52,7 @@ static final class InheritedEqualsAndHashCodeMethod extends Point { } } - public final class Pojo { + static final class Pojo { private String s; public void setS(String value) { @@ -59,4 +68,7 @@ public String toString() { return getClass().getName() + " " + s; } } + + @SuppressWarnings("serial") + static final class SimpleException extends Exception {} }