Skip to content

Commit

Permalink
Bypasses sealed classes to avoid crash
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jan 25, 2022
1 parent 56fb255 commit 35a93e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import nl.jqno.equalsverifier.internal.checkers.*;
import nl.jqno.equalsverifier.internal.exceptions.MessagingException;
import nl.jqno.equalsverifier.internal.prefabvalues.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.ClassAccessor;
import nl.jqno.equalsverifier.internal.util.*;
import nl.jqno.equalsverifier.internal.util.Formatter;

Expand Down Expand Up @@ -358,7 +359,7 @@ private String buildErrorMessage(String description, boolean showUrl) {
}

private void performVerification() {
if (type.isEnum() || type.isInterface()) {
if (type.isEnum() || type.isInterface() || ClassAccessor.isSealed(type)) {
return;
}
Validations.validateClassCanBeVerified(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public boolean isRecord() {
* @return true if T is a sealed class
*/
public boolean isSealed() {
return ClassAccessor.isSealed(type);
}

public static boolean isSealed(Class<?> type) {
int version = Integer.parseInt(System.getProperty("java.version").replaceAll("[.-].*", ""));
if (version >= 17) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.integration.extended_contract.SealedClasses.SealedChild;
import nl.jqno.equalsverifier.integration.extended_contract.SealedClasses.SealedParent;
import org.junit.jupiter.api.Test;

public class SealedClassTest {

@Test
public void dontCrash() {
public void dontCrashOnParent() {
EqualsVerifier
.forClass(SealedParent.class)
.withRedefinedSubclass(SealedChild.class)
.verify();
}

@Test
public void dontCrashOnChild() {
EqualsVerifier.forClass(SealedChild.class).withRedefinedSuperclass().verify();
}
}

0 comments on commit 35a93e4

Please sign in to comment.