Generate equals/hashCode(): Enable for classes without properties
Also: - forbid for enum classes - use javaClass.hashCode() instead of 0 if class has no properties #KT-18418 Fixed
This commit is contained in:
+3
-3
@@ -74,12 +74,12 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
|||||||
override fun isValidForClass(targetClass: KtClassOrObject): Boolean {
|
override fun isValidForClass(targetClass: KtClassOrObject): Boolean {
|
||||||
return targetClass is KtClass
|
return targetClass is KtClass
|
||||||
&& targetClass !is KtEnumEntry
|
&& targetClass !is KtEnumEntry
|
||||||
|
&& !targetClass.isEnum()
|
||||||
&& !targetClass.isAnnotation()
|
&& !targetClass.isAnnotation()
|
||||||
&& !targetClass.isInterface()
|
&& !targetClass.isInterface()
|
||||||
&& (!targetClass.isData() || isValidForDataClass(targetClass))
|
&& (!targetClass.isData() || isValidForDataClass(targetClass))
|
||||||
&& getPropertiesToUseInGeneratedMember(targetClass).isNotEmpty()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isValidForDataClass(targetClass: KtClass): Boolean {
|
private fun isValidForDataClass(targetClass: KtClass): Boolean {
|
||||||
val constructor = targetClass.primaryConstructor ?: return false
|
val constructor = targetClass.primaryConstructor ?: return false
|
||||||
val context = constructor.analyze(BodyResolveMode.PARTIAL)
|
val context = constructor.analyze(BodyResolveMode.PARTIAL)
|
||||||
@@ -226,7 +226,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
|||||||
val initialValue = when {
|
val initialValue = when {
|
||||||
!builtins.isMemberOfAny(superHashCode) -> "super.hashCode()"
|
!builtins.isMemberOfAny(superHashCode) -> "super.hashCode()"
|
||||||
propertyIterator.hasNext() -> propertyIterator.next().genVariableHashCode(false)
|
propertyIterator.hasNext() -> propertyIterator.next().genVariableHashCode(false)
|
||||||
else -> "0"
|
else -> "javaClass.hashCode()"
|
||||||
}
|
}
|
||||||
|
|
||||||
val bodyText = if (propertyIterator.hasNext()) {
|
val bodyText = if (propertyIterator.hasNext()) {
|
||||||
|
|||||||
+1
-3
@@ -5,6 +5,4 @@ class Test {
|
|||||||
var name: String = ""
|
var name: String = ""
|
||||||
get
|
get
|
||||||
<caret>
|
<caret>
|
||||||
}
|
}
|
||||||
|
|
||||||
// FORCED
|
|
||||||
+2
-4
@@ -12,9 +12,7 @@ class Test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
return 0
|
return javaClass.hashCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FORCED
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// NOT_APPLICABLE
|
|
||||||
class A {<caret>
|
class A {<caret>
|
||||||
fun foo() {
|
fun foo() {
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -1,4 +1,3 @@
|
|||||||
// FORCED
|
|
||||||
class A {
|
class A {
|
||||||
fun foo() {
|
fun foo() {
|
||||||
|
|
||||||
@@ -11,6 +10,6 @@ class A {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
return 0
|
return javaClass.hashCode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
// FORCED
|
|
||||||
class A {<caret>
|
|
||||||
fun foo() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-1
@@ -1,4 +1,3 @@
|
|||||||
// FORCED
|
|
||||||
open class X {
|
open class X {
|
||||||
override fun equals(other: Any?) = super.equals(other)
|
override fun equals(other: Any?) = super.equals(other)
|
||||||
override fun hashCode() = super.hashCode()
|
override fun hashCode() = super.hashCode()
|
||||||
-1
@@ -1,4 +1,3 @@
|
|||||||
// FORCED
|
|
||||||
open class X {
|
open class X {
|
||||||
override fun equals(other: Any?) = super.equals(other)
|
override fun equals(other: Any?) = super.equals(other)
|
||||||
override fun hashCode() = super.hashCode()
|
override fun hashCode() = super.hashCode()
|
||||||
+3
-9
@@ -132,15 +132,9 @@ public class GenerateHashCodeAndEqualsActionTestGenerated extends AbstractGenera
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("noVarsForced.kt")
|
@TestMetadata("noVarsWithSuperClass.kt")
|
||||||
public void testNoVarsForced() throws Exception {
|
public void testNoVarsWithSuperClass() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/generate/equalsWithHashCode/noVarsForced.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/generate/equalsWithHashCode/noVarsWithSuperClass.kt");
|
||||||
doTest(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("noVarsForcedWithSuperClass.kt")
|
|
||||||
public void testNoVarsForcedWithSuperClass() throws Exception {
|
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/generate/equalsWithHashCode/noVarsForcedWithSuperClass.kt");
|
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user