Skip to content

Commit

Permalink
Adds logic to detect Kotlin classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jan 8, 2025
1 parent 19e74c9 commit e06a7b0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
public class FieldInspector<T> {

private final Class<T> type;
private final boolean isKotlin;

public FieldInspector(Class<T> type) {
public FieldInspector(Class<T> type, boolean isKotlin) {
this.type = type;
this.isKotlin = isKotlin;
}

public void check(FieldCheck<T> check) {
for (FieldProbe fieldProbe : FieldIterable.of(type)) {
FieldIterable it = isKotlin ? FieldIterable.ofKotlin(type) : FieldIterable.of(type);
for (FieldProbe fieldProbe : it) {
check.execute(fieldProbe);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public FieldsChecker(Context<T> context) {

@Override
public void check() {
FieldInspector<T> inspector = new FieldInspector<>(context.getType());
FieldInspector<T> inspector = new FieldInspector<>(context.getType(), config.isKotlin());

if (!context.getClassProbe().isEqualsInheritedFromObject()) {
inspector.check(arrayFieldCheck);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void check() {
return;
}

FieldInspector<T> inspector = new FieldInspector<>(context.getType());
FieldInspector<T> inspector = new FieldInspector<>(context.getType(), context.getConfiguration().isKotlin());
inspector.check(new NullPointerExceptionFieldCheck<>(context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public final class FieldIterable implements Iterable<FieldProbe> {
private final Class<?> type;
private final boolean includeSuperclasses;
private final boolean includeStatic;
private final boolean isKotlin;

/** Private constructor. Call {@link #of(Class)} or {@link #ofIgnoringSuper(Class)} instead. */
private FieldIterable(Class<?> type, boolean includeSuperclasses, boolean includeStatic) {
private FieldIterable(Class<?> type, boolean includeSuperclasses, boolean includeStatic, boolean isKotlin) {
this.type = type;
this.includeSuperclasses = includeSuperclasses;
this.includeStatic = includeStatic;
this.isKotlin = isKotlin;
}

/**
Expand All @@ -28,7 +30,18 @@ private FieldIterable(Class<?> type, boolean includeSuperclasses, boolean includ
* @return A FieldIterable.
*/
public static FieldIterable of(Class<?> type) {
return new FieldIterable(type, true, true);
return new FieldIterable(type, true, true, false);
}

/**
* Factory method for a FieldIterable that iterates over all declared fields of {@code type} and over the declared
* fields of all of its superclasses, but that ignores overridden Kotlin backing fields in superclasses.
*
* @param type The class that contains the fields over which to iterate.
* @return A FieldIterable.
*/
public static FieldIterable ofKotlin(Class<?> type) {
return new FieldIterable(type, true, true, true);
}

/**
Expand All @@ -39,7 +52,7 @@ public static FieldIterable of(Class<?> type) {
* @return A FieldIterable.
*/
public static FieldIterable ofIgnoringSuper(Class<?> type) {
return new FieldIterable(type, false, true);
return new FieldIterable(type, false, true, false);
}

/**
Expand All @@ -50,7 +63,7 @@ public static FieldIterable ofIgnoringSuper(Class<?> type) {
* @return A FieldIterable.
*/
public static FieldIterable ofIgnoringStatic(Class<?> type) {
return new FieldIterable(type, true, false);
return new FieldIterable(type, true, false, false);
}

/**
Expand All @@ -61,7 +74,7 @@ public static FieldIterable ofIgnoringStatic(Class<?> type) {
* @return A FieldIterable.
*/
public static FieldIterable ofIgnoringSuperAndStatic(Class<?> type) {
return new FieldIterable(type, false, false);
return new FieldIterable(type, false, false, false);
}

/**
Expand All @@ -75,7 +88,12 @@ public Iterator<FieldProbe> iterator() {
}

private List<FieldProbe> createFieldList() {
return createJavaFieldList();
if (isKotlin) {
return createKotlinFieldList();
}
else {
return createJavaFieldList();
}
}

private List<FieldProbe> createJavaFieldList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ public boolean validate(
Set<String> ignoredAnnotations) {
return "LAZY".equals(properties.getEnumValue("fetch"));
}
};
},

KOTLIN(false, "kotlin.Metadata");

private final boolean inherits;
private final Set<String> partialClassNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Configuration<T> {
private final boolean usingGetClass;
private final EnumSet<Warning> warningsToSuppress;
private final Function<String, String> fieldnameToGetter;
private final boolean isKotlin;

private final TypeTag typeTag;
private final AnnotationCache annotationCache;
Expand All @@ -46,6 +47,7 @@ private Configuration(
boolean usingGetClass,
EnumSet<Warning> warningsToSuppress,
Function<String, String> fieldnameToGetter,
boolean isKotlin,
List<T> equalExamples,
List<T> unequalExamples) {
this.type = type;
Expand All @@ -60,6 +62,7 @@ private Configuration(
this.usingGetClass = usingGetClass;
this.warningsToSuppress = warningsToSuppress;
this.fieldnameToGetter = fieldnameToGetter;
this.isKotlin = isKotlin;
this.equalExamples = equalExamples;
this.unequalExamples = unequalExamples;
}
Expand Down Expand Up @@ -91,6 +94,7 @@ public static <T> Configuration<T> build(
actualFields);
Function<String, String> converter =
fieldnameToGetter != null ? fieldnameToGetter : DEFAULT_FIELDNAME_TO_GETTER_CONVERTER;
boolean isKotlin = annotationCache.hasClassAnnotation(type, SupportedAnnotations.KOTLIN);

return new Configuration<>(type,
typeTag,
Expand All @@ -104,6 +108,7 @@ public static <T> Configuration<T> build(
usingGetClass,
warningsToSuppress,
converter,
isKotlin,
equalExamples,
unequalExamples);
}
Expand Down Expand Up @@ -200,6 +205,10 @@ public Function<String, String> getFieldnameToGetter() {
return fieldnameToGetter;
}

public boolean isKotlin() {
return isKotlin;
}

public List<T> getEqualExamples() {
return Collections.unmodifiableList(equalExamples);
}
Expand Down

0 comments on commit e06a7b0

Please sign in to comment.