Skip to content

Commit 6d44361

Browse files
authored
Merge pull request #347 from nstdio/issue-346
Get only instance fields for records.
2 parents de7f012 + d381f8e commit 6d44361

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/main/java/nl/jqno/equalsverifier/internal/checkers/RecordChecker.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import java.lang.reflect.Field;
77
import java.lang.reflect.InvocationTargetException;
88
import java.lang.reflect.Method;
9+
import java.lang.reflect.Modifier;
910
import java.util.ArrayList;
1011
import java.util.List;
1112
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
1214
import java.util.stream.StreamSupport;
1315
import nl.jqno.equalsverifier.internal.reflection.ClassAccessor;
1416
import nl.jqno.equalsverifier.internal.reflection.FieldAccessor;
@@ -72,12 +74,15 @@ private void verifyRecordPrecondition(
7274
failedFields.stream().collect(Collectors.joining(","))));
7375
}
7476

77+
private static <T> Stream<Field> instanceFieldFor(Class<T> type) {
78+
return StreamSupport.stream(FieldIterable.of(type).spliterator(), false)
79+
.filter(field -> !Modifier.isStatic(field.getModifiers()));
80+
}
81+
7582
private static <T> Constructor<T> getConstructorFor(Class<T> type) {
7683
try {
7784
List<Class<?>> constructorTypes =
78-
StreamSupport.stream(FieldIterable.of(type).spliterator(), false)
79-
.map(Field::getType)
80-
.collect(Collectors.toList());
85+
instanceFieldFor(type).map(Field::getType).collect(Collectors.toList());
8186
Constructor<T> constructor =
8287
type.getConstructor(constructorTypes.toArray(new Class<?>[0]));
8388
constructor.setAccessible(true);
@@ -91,7 +96,7 @@ private static <T> Constructor<T> getConstructorFor(Class<T> type) {
9196
private T callConstructor(
9297
Class<T> type, Constructor<T> constructor, ObjectAccessor<T> accessor) {
9398
List<?> params =
94-
StreamSupport.stream(FieldIterable.of(type).spliterator(), false)
99+
instanceFieldFor(type)
95100
.map(accessor::fieldAccessorFor)
96101
.map(FieldAccessor::get)
97102
.collect(Collectors.toList());

src/test/java/nl/jqno/equalsverifier/integration/extended_contract/RecordsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public void fail_whenRecordAccessorThrowsNpe() {
101101
EqualsVerifier.forClass(type).verify();
102102
}
103103

104+
@Test
105+
public void succeed_whenRecordContainsStaticField() {
106+
Class<?> type = compile(STATIC_FIELD_RECORD_CLASS_NAME, STATIC_FIELD_RECORD_CLASS);
107+
EqualsVerifier.forClass(type).verify();
108+
}
109+
104110
public boolean isRecordsAvailable() {
105111
if (!isTypeAvailable("java.lang.Record")) {
106112
return false;
@@ -214,4 +220,9 @@ public boolean isRecordsAvailable() {
214220
+ "\n return s;"
215221
+ "\n }"
216222
+ "\n}";
223+
private static final String STATIC_FIELD_RECORD_CLASS_NAME = "StaticFieldRecord";
224+
private static final String STATIC_FIELD_RECORD_CLASS =
225+
"record StaticFieldRecord(int i, String s) {\n"
226+
+ " private static final int X = 0;\n"
227+
+ "}";
217228
}

0 commit comments

Comments
 (0)