Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ class MetamodelProcessor(
K_FLOAT,
K_DOUBLE,
)

/**
* Kotlin array types. `==` on these compares references; the compiler warns and the
* semantically-correct comparison is `contentEquals`, whose stdlib extension on the
* nullable receiver handles the both-null case transparently.
*/
private val KOTLIN_ARRAY_QNS: Set<String> = setOf(
"kotlin.Array",
"kotlin.BooleanArray",
"kotlin.ByteArray",
"kotlin.CharArray",
"kotlin.ShortArray",
"kotlin.IntArray",
"kotlin.LongArray",
"kotlin.FloatArray",
"kotlin.DoubleArray",
"kotlin.UByteArray",
"kotlin.UShortArray",
"kotlin.UIntArray",
"kotlin.ULongArray",
)
}

override fun process(resolver: Resolver): List<KSAnnotated> {
Expand Down Expand Up @@ -579,6 +600,11 @@ class MetamodelProcessor(
}
}

private fun isKotlinArrayType(typeRef: KSTypeReference): Boolean {
val qn = typeRef.resolve().declaration.qualifiedName?.asString() ?: return false
return qn in KOTLIN_ARRAY_QNS
}

private fun ensureNullable(typeName: String): String = if (typeName.endsWith("?")) typeName else "$typeName?"

private fun sameExpr(left: String, right: String, typeRef: KSTypeReference, forceNullableChain: Boolean): String {
Expand Down Expand Up @@ -606,7 +632,7 @@ class MetamodelProcessor(
PrimitiveKind.LONG,
PrimitiveKind.CHAR,
-> "$left == $right"
null -> "$left == $right"
null -> if (isKotlinArrayType(typeRef)) "($left).contentEquals($right)" else "$left == $right"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,12 @@ private static String sameComparisonExpr(@Nonnull String left, @Nonnull String r
if (isPrimitiveReturn(t)) {
return left + " == " + right;
}
// Java arrays don't override equals — Objects.equals would do reference comparison.
// Arrays.equals handles both null operands and shallow content equality for primitive
// and Object arrays.
if (t.getKind() == TypeKind.ARRAY) {
return "java.util.Arrays.equals(" + left + ", " + right + ")";
}
return "Objects.equals(" + left + ", " + right + ")";
}

Expand Down
Loading