-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supports @NonNullFields and @NullMarked
- Loading branch information
Showing
12 changed files
with
307 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...t/java/nl/jqno/equalsverifier/integration/extra_features/AnnotationNonNullFieldsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nl.jqno.equalsverifier.integration.extra_features; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.integration.extra_features.nonnull.springframework.NonNullFieldsOnPackage; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AnnotationNonNullFieldsTest { | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNonNullFieldsAnnotationOnPackage() { | ||
EqualsVerifier.forClass(NonNullFieldsOnPackage.class).verify(); | ||
} | ||
} |
186 changes: 186 additions & 0 deletions
186
...test/java/nl/jqno/equalsverifier/integration/extra_features/AnnotationNullMarkedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package nl.jqno.equalsverifier.integration.extra_features; | ||
|
||
import java.util.Objects; | ||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import nl.jqno.equalsverifier.Warning; | ||
import nl.jqno.equalsverifier.integration.extra_features.nonnull.jspecify.NullMarkedOnPackage; | ||
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; | ||
import nl.jqno.equalsverifier.testhelpers.annotations.org.jspecify.annotations.NullMarked; | ||
import nl.jqno.equalsverifier.testhelpers.annotations.org.jspecify.annotations.Nullable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AnnotationNullMarkedTest { | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNullMarkedAnnotationOnClass() { | ||
EqualsVerifier.forClass(NullMarkedOnClass.class).verify(); | ||
} | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNullMarkedAnnotationOnPackage() { | ||
EqualsVerifier.forClass(NullMarkedOnPackage.class).verify(); | ||
} | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNullMarkedAnnotationOnOuterClass() { | ||
EqualsVerifier.forClass(NullMarkedOuter.FInner.class).verify(); | ||
} | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNullMarkedAnnotationOnNestedOuterClass() { | ||
EqualsVerifier.forClass(NullMarkedOuter.FMiddle.FInnerInner.class).verify(); | ||
} | ||
|
||
@Test | ||
public void fail_whenEqualsDoesntCheckForNull_givenNullMarkedAndNullableAnnotationOnClass() { | ||
ExpectedException | ||
.when(() -> EqualsVerifier.forClass(NullMarkedWithNullableOnClass.class).verify()) | ||
.assertFailure() | ||
.assertMessageContains( | ||
"Non-nullity", | ||
"equals throws NullPointerException", | ||
"'this' object's field o" | ||
); | ||
} | ||
|
||
@Test | ||
public void succeed_whenEqualsDoesntCheckForNull_givenNullMarkedAndNullableAnnotationOnClassAndWarningSuppressed() { | ||
EqualsVerifier | ||
.forClass(NullMarkedWithNullableOnClass.class) | ||
.suppress(Warning.NULL_FIELDS) | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void succeed_whenEqualsChecksForNull_givenNullMarkedAndNullableAnnotationOnClass() { | ||
EqualsVerifier.forClass(NullMarkedWithNullableOnClassAndNullCheckInEquals.class).verify(); | ||
} | ||
|
||
@NullMarked | ||
static final class NullMarkedOnClass { | ||
|
||
private final Object o; | ||
|
||
public NullMarkedOnClass(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof NullMarkedOnClass)) { | ||
return false; | ||
} | ||
NullMarkedOnClass other = (NullMarkedOnClass) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} | ||
|
||
@NullMarked | ||
static final class NullMarkedOuter { | ||
|
||
static final class FInner { | ||
|
||
private final Object o; | ||
|
||
public FInner(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof FInner)) { | ||
return false; | ||
} | ||
FInner other = (FInner) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} | ||
|
||
static final class FMiddle { | ||
|
||
static final class FInnerInner { | ||
|
||
private final Object o; | ||
|
||
public FInnerInner(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof FInnerInner)) { | ||
return false; | ||
} | ||
FInnerInner other = (FInnerInner) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@NullMarked | ||
static final class NullMarkedWithNullableOnClass { | ||
|
||
@Nullable | ||
private final Object o; | ||
|
||
public NullMarkedWithNullableOnClass(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
if (!(obj instanceof NullMarkedWithNullableOnClass)) { | ||
return false; | ||
} | ||
NullMarkedWithNullableOnClass other = (NullMarkedWithNullableOnClass) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} | ||
|
||
@NullMarked | ||
static final class NullMarkedWithNullableOnClassAndNullCheckInEquals { | ||
|
||
@Nullable | ||
private final Object o; | ||
|
||
public NullMarkedWithNullableOnClassAndNullCheckInEquals(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
if (!(obj instanceof NullMarkedWithNullableOnClassAndNullCheckInEquals)) { | ||
return false; | ||
} | ||
NullMarkedWithNullableOnClassAndNullCheckInEquals other = | ||
(NullMarkedWithNullableOnClassAndNullCheckInEquals) obj; | ||
return o == null ? other.o == null : o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../jqno/equalsverifier/integration/extra_features/nonnull/jspecify/NullMarkedOnPackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nl.jqno.equalsverifier.integration.extra_features.nonnull.jspecify; | ||
|
||
import java.util.Objects; | ||
|
||
public final class NullMarkedOnPackage { | ||
|
||
private final Object o; | ||
|
||
public NullMarkedOnPackage(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof NullMarkedOnPackage)) { | ||
return false; | ||
} | ||
NullMarkedOnPackage other = (NullMarkedOnPackage) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...java/nl/jqno/equalsverifier/integration/extra_features/nonnull/jspecify/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** Applies JSpecify's NullMarked annotation to the package. */ | ||
@NullMarked | ||
package nl.jqno.equalsverifier.integration.extra_features.nonnull.jspecify; | ||
|
||
import nl.jqno.equalsverifier.testhelpers.annotations.org.jspecify.annotations.NullMarked; |
26 changes: 26 additions & 0 deletions
26
...lsverifier/integration/extra_features/nonnull/springframework/NonNullFieldsOnPackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nl.jqno.equalsverifier.integration.extra_features.nonnull.springframework; | ||
|
||
import java.util.Objects; | ||
|
||
public final class NonNullFieldsOnPackage { | ||
|
||
private final Object o; | ||
|
||
public NonNullFieldsOnPackage(Object o) { | ||
this.o = o; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof NonNullFieldsOnPackage)) { | ||
return false; | ||
} | ||
NonNullFieldsOnPackage other = (NonNullFieldsOnPackage) obj; | ||
return o.equals(other.o); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(o); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
.../jqno/equalsverifier/integration/extra_features/nonnull/springframework/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** Applies Spring's NonNullFields annotation to the package. */ | ||
@NonNullFields | ||
package nl.jqno.equalsverifier.integration.extra_features.nonnull.springframework; | ||
|
||
import nl.jqno.equalsverifier.testhelpers.annotations.org.springframework.lang.NonNullFields; |
13 changes: 13 additions & 0 deletions
13
...a/nl/jqno/equalsverifier/testhelpers/annotations/org/jspecify/annotations/NullMarked.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package nl.jqno.equalsverifier.testhelpers.annotations.org.jspecify.annotations; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* This annotation serves as a placeholder for the real {@link org.jspecify.annotations.NullMarked} | ||
* annotation. However, since that annotation is compiled for Java 9, and this code base must | ||
* support Java 8, we use this copy instead. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.PACKAGE, ElementType.TYPE }) | ||
public @interface NullMarked { | ||
} |
8 changes: 8 additions & 0 deletions
8
...ava/nl/jqno/equalsverifier/testhelpers/annotations/org/jspecify/annotations/Nullable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nl.jqno.equalsverifier.testhelpers.annotations.org.jspecify.annotations; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE_USE) | ||
public @interface Nullable { | ||
} |
8 changes: 8 additions & 0 deletions
8
...l/jqno/equalsverifier/testhelpers/annotations/org/springframework/lang/NonNullFields.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package nl.jqno.equalsverifier.testhelpers.annotations.org.springframework.lang; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PACKAGE) | ||
public @interface NonNullFields { | ||
} |