Skip to content

Commit

Permalink
#315: support classes that have fields but don't override equals
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jun 11, 2020
1 parent 503babb commit 7c85ef3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %%.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -59,4 +68,7 @@ public String toString() {
return getClass().getName() + " " + s;
}
}

@SuppressWarnings("serial")
static final class SimpleException extends Exception {}
}

0 comments on commit 7c85ef3

Please sign in to comment.