Introduce Parameter: Fix NPE on invalid parameter name or type

EA-95189 Fixed
This commit is contained in:
Alexey Sedunov
2017-06-27 15:57:45 +03:00
parent 2e8b374ce8
commit 20969f161c
2 changed files with 7 additions and 8 deletions
@@ -69,14 +69,13 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
return (createExpression("a?.b") as KtSafeQualifiedExpression).operationTokenNode
}
private fun doCreateExpression(text: String): KtExpression {
//TODO: '\n' below if important - some strange code indenting problems appear without it
val expression = createProperty("val x =\n$text").initializer ?: error("Failed to create expression from text: '$text'")
return expression
private fun doCreateExpression(text: String): KtExpression? {
//NOTE: '\n' below is important - some strange code indenting problems appear without it
return createProperty("val x =\n$text").initializer
}
fun createExpression(text: String): KtExpression {
val expression = doCreateExpression(text)
val expression = doCreateExpression(text) ?: error("Failed to create expression from text: '$text'")
assert(expression.text == text) {
"Failed to create expression from text: '$text', resulting expression's text was: '${expression.text}'"
}
@@ -84,7 +83,7 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
}
fun createExpressionIfPossible(text: String): KtExpression? {
val expression = doCreateExpression(text)
val expression = doCreateExpression(text) ?: return null
return if (expression.text == text) expression else null
}
@@ -231,8 +231,8 @@ class KotlinIntroduceParameterDialog private constructor(
override fun canRun() {
val psiFactory = KtPsiFactory(myProject)
psiFactory.createSimpleName(nameField.enteredName.quoteIfNeeded()).validateElement("Invalid parameter name")
psiFactory.createType(typeField.enteredName).validateElement("Invalid parameter type")
psiFactory.createExpressionIfPossible(nameField.enteredName.quoteIfNeeded()).validateElement("Invalid parameter name")
psiFactory.createTypeIfPossible(typeField.enteredName).validateElement("Invalid parameter type")
}
override fun doAction() {