Generate hashCode()/equals(): Support deep array equality
#KT-19102 Fixed
This commit is contained in:
+10
-4
@@ -199,8 +199,11 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
||||
variablesForEquals.forEach {
|
||||
val propName = (DescriptorToSourceUtilsIde.getAnyDeclaration(project, it) as PsiNameIdentifierOwner).nameIdentifier!!.text
|
||||
val notEquals = when {
|
||||
KotlinBuiltIns.isArray(it.type) || KotlinBuiltIns.isPrimitiveArray(it.type) ->
|
||||
"!java.util.Arrays.equals($propName, $paramName.$propName)"
|
||||
KotlinBuiltIns.isArrayOrPrimitiveArray(it.type) -> {
|
||||
val isNestedArray = KotlinBuiltIns.isArrayOrPrimitiveArray(classDescriptor.builtIns.getArrayElementType(it.type))
|
||||
val methodName = if (isNestedArray) "deepEquals" else "equals"
|
||||
"!java.util.Arrays.$methodName($propName, $paramName.$propName)"
|
||||
}
|
||||
else ->
|
||||
"$propName != $paramName.$propName"
|
||||
}
|
||||
@@ -230,8 +233,11 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
||||
var text = when {
|
||||
typeClass == builtIns.byte || typeClass == builtIns.short || typeClass == builtIns.int ->
|
||||
ref
|
||||
KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) ->
|
||||
if (isNullable) "$ref?.let { java.util.Arrays.hashCode(it) }" else "java.util.Arrays.hashCode($ref)"
|
||||
KotlinBuiltIns.isArrayOrPrimitiveArray(type) -> {
|
||||
val isNestedArray = KotlinBuiltIns.isArrayOrPrimitiveArray(builtIns.getArrayElementType(type))
|
||||
val methodName = if (isNestedArray) "deepHashCode" else "hashCode"
|
||||
if (isNullable) "$ref?.let { java.util.Arrays.$methodName(it) }" else "java.util.Arrays.$methodName($ref)"
|
||||
}
|
||||
else ->
|
||||
if (isNullable) "$ref?.hashCode()" else "$ref.hashCode()"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
class EqKotlin(val a: Array<Array<String>>) {<caret>
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import java.util.Arrays
|
||||
|
||||
class EqKotlin(val a: Array<Array<String>>) {
|
||||
<caret>override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as EqKotlin
|
||||
|
||||
if (!Arrays.deepEquals(a, other.a)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Arrays.deepHashCode(a)
|
||||
}
|
||||
}
|
||||
+5
@@ -114,6 +114,11 @@ public class GenerateHashCodeAndEqualsActionTestGenerated extends AbstractGenera
|
||||
runTest("idea/testData/codeInsight/generate/equalsWithHashCode/nameClash.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nestedArray.kt")
|
||||
public void testNestedArray() throws Exception {
|
||||
runTest("idea/testData/codeInsight/generate/equalsWithHashCode/nestedArray.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noVars.kt")
|
||||
public void testNoVars() throws Exception {
|
||||
runTest("idea/testData/codeInsight/generate/equalsWithHashCode/noVars.kt");
|
||||
|
||||
Reference in New Issue
Block a user