Fix KNPE in different "Generate members" for expect class

#KT-27595 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-11-08 12:32:53 +03:00
parent 38c3f13b12
commit cb28387da4
12 changed files with 62 additions and 5 deletions
@@ -250,7 +250,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
append("return true")
}
equalsFun.bodyExpression!!.replace(KtPsiFactory(project).createBlock(bodyText))
equalsFun.replaceBody { KtPsiFactory(project).createBlock(bodyText) }
return equalsFun
}
@@ -311,7 +311,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
}.toString()
} else "return $initialValue"
hashCodeFun.bodyExpression!!.replace(KtPsiFactory(project).createBlock(bodyText))
hashCodeFun.replaceBody { KtPsiFactory(project).createBlock(bodyText) }
return hashCodeFun
}
@@ -24,14 +24,17 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.*
abstract class KotlinGenerateMemberActionBase<Info : Any> : KotlinGenerateActionBase() {
protected abstract fun prepareMembersInfo(klass: KtClassOrObject, project: Project, editor: Editor?): Info?
protected abstract fun generateMembers(project: Project, editor: Editor?, info: Info): List<KtDeclaration>
protected fun KtNamedFunction.replaceBody(generateBody: () -> KtExpression) {
bodyExpression?.replace(generateBody())
}
override fun invoke(project: Project, editor: Editor, file: PsiFile) {
if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return
if (!FileDocumentManager.getInstance().requestWriting(editor.document, project)) return
@@ -181,7 +181,9 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
private fun generateToString(targetClass: KtClassOrObject, info: Info): KtNamedFunction? {
val superToString = info.classDescriptor.getSuperClassOrAny().findDeclaredToString(true)!!
return generateFunctionSkeleton(superToString, targetClass).apply {
bodyExpression!!.replace(KtPsiFactory(targetClass).createExpression("{\n${info.generator.generate(info)}\n}"))
replaceBody {
KtPsiFactory(targetClass).createExpression("{\n${info.generator.generate(info)}\n}")
}
}
}
@@ -0,0 +1,9 @@
// "Generate 'equals()'" "true"
// TOOL: org.jetbrains.kotlin.idea.inspections.EqualsOrHashCodeInspection
expect class With<caret>Constructor(x: Int, s: String) {
val x: Int
val s: String
override fun hashCode(): Int
}
@@ -0,0 +1,10 @@
// "Generate 'equals()'" "true"
// TOOL: org.jetbrains.kotlin.idea.inspections.EqualsOrHashCodeInspection
expect class With<caret>Constructor(x: Int, s: String) {
val x: Int
val s: String
override fun hashCode(): Int
override fun equals(other: Any?): Boolean
}
@@ -0,0 +1 @@
actual class WithConstructor(actual val x: Int, actual val s: String)
@@ -0,0 +1 @@
actual class WithConstructor(actual val x: Int, actual val s: String)
@@ -0,0 +1,9 @@
// "Generate 'hashCode()'" "true"
// TOOL: org.jetbrains.kotlin.idea.inspections.EqualsOrHashCodeInspection
expect class With<caret>Constructor(x: Int, s: String) {
val x: Int
val s: String
override fun equals(other: Any?): Boolean
}
@@ -0,0 +1,10 @@
// "Generate 'hashCode()'" "true"
// TOOL: org.jetbrains.kotlin.idea.inspections.EqualsOrHashCodeInspection
expect class With<caret>Constructor(x: Int, s: String) {
val x: Int
val s: String
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
}
@@ -0,0 +1 @@
actual class WithConstructor(actual val x: Int, actual val s: String)
@@ -0,0 +1 @@
actual class WithConstructor(actual val x: Int, actual val s: String)
@@ -259,6 +259,16 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
runTest("idea/testData/multiModuleQuickFix/functionTypeReceiverToParameterByImpl/");
}
@TestMetadata("generateEqualsInExpect")
public void testGenerateEqualsInExpect() throws Exception {
runTest("idea/testData/multiModuleQuickFix/generateEqualsInExpect/");
}
@TestMetadata("generateHashCodeInExpect")
public void testGenerateHashCodeInExpect() throws Exception {
runTest("idea/testData/multiModuleQuickFix/generateHashCodeInExpect/");
}
@TestMetadata("implementAbstractExpectMemberInheritedFromInterface")
public void testImplementAbstractExpectMemberInheritedFromInterface() throws Exception {
runTest("idea/testData/multiModuleQuickFix/implementAbstractExpectMemberInheritedFromInterface/");