From e4275cc027da013cceaa6d18c8247cf4e7598aee Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 11 Apr 2018 14:11:06 +0300 Subject: [PATCH] Refactor sealed subclass to object: add tests, fix type parameter case --- .../CanSealedSubClassBeObjectInspection.kt | 5 ++++ .../convertSealedSubClassToObject/generic.kt | 5 ++++ .../nonEmptyConstructor.kt | 5 ++++ .../openSubclass.kt | 5 ++++ .../withCompanion.kt | 9 ++++++ .../withInner.kt | 11 +++++++ .../LocalInspectionTestGenerated.java | 30 +++++++++++++++++++ 7 files changed, 70 insertions(+) create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt create mode 100644 idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/CanSealedSubClassBeObjectInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/CanSealedSubClassBeObjectInspection.kt index 4cac7bf5755..2ea70008a35 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/CanSealedSubClassBeObjectInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/CanSealedSubClassBeObjectInspection.kt @@ -38,6 +38,7 @@ class CanSealedSubClassBeObjectInspection : AbstractKotlinInspection() { klass.getSubclasses() .withEmptyConstructors() .thatAreFinal() + .thatHasNoTypeParameters() .thatHasNoInnerClasses() .thatHasNoCompanionObjects() .forEach { reportPossibleObject(it) } @@ -74,6 +75,10 @@ class CanSealedSubClassBeObjectInspection : AbstractKotlinInspection() { return filter { klass -> klass.getModalityFromDescriptor() == KtTokens.FINAL_KEYWORD } } + private fun List.thatHasNoTypeParameters(): List { + return filter { klass -> klass.typeParameters.isEmpty() } + } + private fun List.thatHasNoInnerClasses(): List { return filter { klass -> klass.hasNoInnerClass() } } diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt new file mode 100644 index 00000000000..e8c734df6d3 --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt @@ -0,0 +1,5 @@ +// PROBLEM: none + +sealed class Sealed + +class SubSealed : Sealed() \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt new file mode 100644 index 00000000000..356954f14f3 --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt @@ -0,0 +1,5 @@ +// PROBLEM: none + +sealed class Sealed(val y: Int) + +class SubSealed(x: Int) : Sealed(x) \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt new file mode 100644 index 00000000000..6926028c559 --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt @@ -0,0 +1,5 @@ +// PROBLEM: none + +sealed class Sealed + +open class SubSealed : Sealed() \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt new file mode 100644 index 00000000000..065416a61e8 --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt @@ -0,0 +1,9 @@ +// PROBLEM: none + +sealed class Sealed + +class SubSealed : Sealed() { + companion object { + + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt new file mode 100644 index 00000000000..3a3f96cb50f --- /dev/null +++ b/idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt @@ -0,0 +1,11 @@ +// PROBLEM: none + +sealed class Sealed + +class SubSealed : Sealed() { + val x: Int = 42 + + inner class Inner { + fun foo() = x + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index aba22cfc6b5..9352c950b9f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1259,6 +1259,36 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/convertSubClassWithoutParentheses.kt"); doTest(fileName); } + + @TestMetadata("generic.kt") + public void testGeneric() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/generic.kt"); + doTest(fileName); + } + + @TestMetadata("nonEmptyConstructor.kt") + public void testNonEmptyConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/nonEmptyConstructor.kt"); + doTest(fileName); + } + + @TestMetadata("openSubclass.kt") + public void testOpenSubclass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/openSubclass.kt"); + doTest(fileName); + } + + @TestMetadata("withCompanion.kt") + public void testWithCompanion() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/withCompanion.kt"); + doTest(fileName); + } + + @TestMetadata("withInner.kt") + public void testWithInner() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertSealedSubClassToObject/withInner.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/inspectionsLocal/copyWithoutNamedArguments")