Skip to content

Commit

Permalink
Adds Kotlin filter in FieldIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jan 8, 2025
1 parent 5b926f0 commit 19e74c9
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package nl.jqno.equalsverifier.internal.reflection;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
* Iterable to iterate over all declared fields in a class and, if needed, over all declared fields of its superclasses.
Expand Down Expand Up @@ -76,6 +75,10 @@ public Iterator<FieldProbe> iterator() {
}

private List<FieldProbe> createFieldList() {
return createJavaFieldList();
}

private List<FieldProbe> createJavaFieldList() {
List<FieldProbe> result = new ArrayList<>();

result.addAll(addFieldsFor(type));
Expand All @@ -89,6 +92,24 @@ private List<FieldProbe> createFieldList() {
return result;
}

private List<FieldProbe> createKotlinFieldList() {
List<FieldProbe> result = new ArrayList<>();

result.addAll(addFieldsFor(type));
Set<String> names = result.stream().map(FieldProbe::getName).collect(Collectors.toSet());

if (includeSuperclasses) {
for (Class<?> c : SuperclassIterable.of(type)) {
List<FieldProbe> superFields =
addFieldsFor(c).stream().filter(p -> !names.contains(p.getName())).collect(Collectors.toList());
result.addAll(superFields);
superFields.stream().map(FieldProbe::getName).forEach(names::add);
}
}

return result;
}

private List<FieldProbe> addFieldsFor(Class<?> c) {
List<FieldProbe> fields = new ArrayList<>();
List<FieldProbe> statics = new ArrayList<>();
Expand Down

0 comments on commit 19e74c9

Please sign in to comment.