Fix KNPE in different "Generate members" for expect class
#KT-27595 Fixed
This commit is contained in:
+2
-2
@@ -250,7 +250,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
|||||||
append("return true")
|
append("return true")
|
||||||
}
|
}
|
||||||
|
|
||||||
equalsFun.bodyExpression!!.replace(KtPsiFactory(project).createBlock(bodyText))
|
equalsFun.replaceBody { KtPsiFactory(project).createBlock(bodyText) }
|
||||||
|
|
||||||
return equalsFun
|
return equalsFun
|
||||||
}
|
}
|
||||||
@@ -311,7 +311,7 @@ class KotlinGenerateEqualsAndHashcodeAction : KotlinGenerateMemberActionBase<Kot
|
|||||||
}.toString()
|
}.toString()
|
||||||
} else "return $initialValue"
|
} else "return $initialValue"
|
||||||
|
|
||||||
hashCodeFun.bodyExpression!!.replace(KtPsiFactory(project).createBlock(bodyText))
|
hashCodeFun.replaceBody { KtPsiFactory(project).createBlock(bodyText) }
|
||||||
|
|
||||||
return hashCodeFun
|
return hashCodeFun
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -24,14 +24,17 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
|
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
|
||||||
|
|
||||||
abstract class KotlinGenerateMemberActionBase<Info : Any> : KotlinGenerateActionBase() {
|
abstract class KotlinGenerateMemberActionBase<Info : Any> : KotlinGenerateActionBase() {
|
||||||
protected abstract fun prepareMembersInfo(klass: KtClassOrObject, project: Project, editor: Editor?): Info?
|
protected abstract fun prepareMembersInfo(klass: KtClassOrObject, project: Project, editor: Editor?): Info?
|
||||||
|
|
||||||
protected abstract fun generateMembers(project: Project, editor: Editor?, info: Info): List<KtDeclaration>
|
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) {
|
override fun invoke(project: Project, editor: Editor, file: PsiFile) {
|
||||||
if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return
|
if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return
|
||||||
if (!FileDocumentManager.getInstance().requestWriting(editor.document, project)) 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? {
|
private fun generateToString(targetClass: KtClassOrObject, info: Info): KtNamedFunction? {
|
||||||
val superToString = info.classDescriptor.getSuperClassOrAny().findDeclaredToString(true)!!
|
val superToString = info.classDescriptor.getSuperClassOrAny().findDeclaredToString(true)!!
|
||||||
return generateFunctionSkeleton(superToString, targetClass).apply {
|
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
|
||||||
|
}
|
||||||
+10
@@ -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
|
||||||
|
}
|
||||||
+10
@@ -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)
|
||||||
+10
@@ -259,6 +259,16 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
|
|||||||
runTest("idea/testData/multiModuleQuickFix/functionTypeReceiverToParameterByImpl/");
|
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")
|
@TestMetadata("implementAbstractExpectMemberInheritedFromInterface")
|
||||||
public void testImplementAbstractExpectMemberInheritedFromInterface() throws Exception {
|
public void testImplementAbstractExpectMemberInheritedFromInterface() throws Exception {
|
||||||
runTest("idea/testData/multiModuleQuickFix/implementAbstractExpectMemberInheritedFromInterface/");
|
runTest("idea/testData/multiModuleQuickFix/implementAbstractExpectMemberInheritedFromInterface/");
|
||||||
|
|||||||
Reference in New Issue
Block a user