From 8476a790bd2046b3d6ba31d56f4eb79769f8192e Mon Sep 17 00:00:00 2001 From: Zack Grannan Date: Mon, 14 Apr 2014 15:25:39 -0700 Subject: [PATCH] Added DoubleBangToIfThenIntention --- .../jet/generators/tests/GenerateTests.kt | 1 + .../after.kt.template | 4 + .../before.kt.template | 1 + .../description.html | 6 ++ idea/src/META-INF/plugin.xml | 5 + .../jetbrains/jet/plugin/JetBundle.properties | 4 + .../intentions/DoubleBangToIfThenIntention.kt | 101 ++++++++++++++++++ .../doubleBangToIfThen/callExpression.kt | 7 ++ .../callExpression.kt.after | 7 ++ .../callExpressionParens.kt | 7 ++ .../callExpressionParens.kt.after | 7 ++ .../exceptionTextEscapesQuotes.kt | 5 + .../exceptionTextEscapesQuotes.kt.after | 5 + .../exceptionTextHandlesMultiline.kt | 7 ++ .../exceptionTextHandlesMultiline.kt.after | 8 ++ .../branched/doubleBangToIfThen/localVal.kt | 4 + .../doubleBangToIfThen/localVal.kt.after | 4 + .../branched/doubleBangToIfThen/localVar.kt | 6 ++ .../doubleBangToIfThen/localVar.kt.after | 7 ++ .../simpleNameExpressionInParens.kt | 4 + .../simpleNameExpressionInParens.kt.after | 4 + .../doubleBangToIfThen/topLevelVal.kt | 4 + .../doubleBangToIfThen/topLevelVal.kt.after | 4 + .../topLevelValCustomGetter.kt | 6 ++ .../topLevelValCustomGetter.kt.after | 7 ++ .../doubleBangToIfThen/topLevelVar.kt | 4 + .../doubleBangToIfThen/topLevelVar.kt.after | 5 + .../topLevelVarCustomGetter.kt | 9 ++ .../topLevelVarCustomGetter.kt.after | 10 ++ .../doubleBangToIfThen/usedInAssignment.kt | 4 + .../usedInAssignment.kt.after | 4 + .../AbstractCodeTransformationTest.java | 4 + 32 files changed, 265 insertions(+) create mode 100644 idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/description.html create mode 100644 idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt.after create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt create mode 100644 idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt.after diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index c0bbe1f6fef..18a6e267388 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -342,6 +342,7 @@ fun main(args: Array) { } testClass(javaClass()) { + model("intentions/branched/doubleBangToIfThen", testMethod = "doTestDoubleBangToIfThen") model("intentions/branched/elvisToIfThen", testMethod = "doTestElvisToIfThen") model("intentions/branched/ifThenToElvis", testMethod = "doTestIfThenToElvis") model("intentions/branched/safeAccessToIfThen", testMethod = "doTestSafeAccessToIfThen") diff --git a/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/after.kt.template b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/after.kt.template new file mode 100644 index 00000000000..14aa0a65f70 --- /dev/null +++ b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/after.kt.template @@ -0,0 +1,4 @@ +foo(if (maybeSomething != null) + maybeSomething + else + throw NullPointerException("expression 'maybeSomething' must not be null") ) diff --git a/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/before.kt.template b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/before.kt.template new file mode 100644 index 00000000000..5d8181f0686 --- /dev/null +++ b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/before.kt.template @@ -0,0 +1 @@ +foo(maybeSomething!!) diff --git a/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/description.html b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/description.html new file mode 100644 index 00000000000..542087eb75f --- /dev/null +++ b/idea/resources/intentionDescriptions/DoubleBangToIfThenIntention/description.html @@ -0,0 +1,6 @@ + + + Converts an expression that uses the '!!' operator to an if-then expression that throws a + NullPointerException if the expression is null. + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4ec68bfe29b..39c9933604c 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -494,6 +494,11 @@ Kotlin + + org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.DoubleBangToIfThenIntention + Kotlin + + org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.ElvisToIfThenIntention Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index cdb88172111..cc4da0ed0ac 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -221,6 +221,10 @@ unfold.return.to.when=Replace return with 'when' expression unfold.return.to.when.family=Replace Return with 'when' Expression unfold.call.to.when=Replace method call with 'when' expression unfold.call.to.when.family=Replace Method Call with 'when' Expression +double.bang.to.if.then.exception.text=Expression ''{0}'' must not be null +double.bang.to.if.then.choose.exception=Choose Exception Class +double.bang.to.if.then=Replace '!!' expression with 'if' expression +double.bang.to.if.then.family=Replace '!!' Expression With 'if' Expression elvis.to.if.then=Replace elvis expression with 'if' expression elvis.to.if.then.family=Replace Elvis Expression With 'if' Expression if.then.to.elvis=Replace 'if' expression with elvis expression diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt new file mode 100644 index 00000000000..f7fe9641eba --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/intentions/branchedTransformations/intentions/DoubleBangToIfThenIntention.kt @@ -0,0 +1,101 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions + +import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention +import com.intellij.openapi.editor.Editor +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.lang.psi.JetPsiUtil +import org.jetbrains.jet.lang.psi.JetPostfixExpression +import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStableVariable +import org.jetbrains.jet.plugin.intentions.JetTypeLookupExpression +import com.intellij.codeInsight.template.TemplateEditingAdapter +import com.intellij.codeInsight.template.impl.TemplateManagerImpl +import com.intellij.codeInsight.template.Template +import org.jetbrains.jet.plugin.JetBundle +import org.jetbrains.jet.plugin.intentions.branchedTransformations.convertToIfNotNullExpression +import org.jetbrains.jet.lang.psi.JetPsiFactory +import org.jetbrains.jet.plugin.intentions.branchedTransformations.introduceValueForCondition +import org.jetbrains.jet.lang.psi.JetThrowExpression +import com.intellij.codeInsight.template.TemplateBuilderImpl +import com.intellij.psi.PsiDocumentManager +import org.apache.commons.lang.StringEscapeUtils.escapeJava +import org.jetbrains.jet.plugin.intentions.branchedTransformations.convertToIfNullExpression +import org.jetbrains.jet.plugin.intentions.branchedTransformations.isStatement +import org.jetbrains.jet.plugin.intentions.branchedTransformations.NULL_PTR_EXCEPTION +import org.jetbrains.jet.plugin.intentions.branchedTransformations.KOTLIN_NULL_PTR_EXCEPTION + +public class DoubleBangToIfThenIntention : JetSelfTargetingIntention("double.bang.to.if.then", javaClass()) { + + override fun isApplicableTo(element: JetPostfixExpression): Boolean = + element.getOperationToken() == JetTokens.EXCLEXCL + + override fun applyTo(element: JetPostfixExpression, editor: Editor) { + val project = element.getProject() + val base = checkNotNull(JetPsiUtil.deparenthesize(element.getBaseExpression()), "Base expression cannot be null") + val expressionText = formatForUseInExceptionArgument(base.getText()!!) + + val defaultException = JetPsiFactory.createExpression(project, "throw $NULL_PTR_EXCEPTION()") + + val isStatement = element.isStatement() + val isStable = base.isStableVariable() + + val ifStatement = if (isStatement) + element.convertToIfNullExpression(base, defaultException) + else + element.convertToIfNotNullExpression(base, base, defaultException) + + val thrownExpression = + ((if (isStatement) ifStatement.getThen() else ifStatement.getElse()) as JetThrowExpression).getThrownExpression()!! + + val nullPtrExceptionText = "$NULL_PTR_EXCEPTION(\"%s\")".format(escapeJava(JetBundle.message("double.bang.to.if.then.exception.text", expressionText))) + val kotlinNullPtrExceptionText = "$KOTLIN_NULL_PTR_EXCEPTION()" + + val exceptionLookupExpression = + object: JetTypeLookupExpression(listOf(nullPtrExceptionText, kotlinNullPtrExceptionText), + nullPtrExceptionText, JetBundle.message("double.bang.to.if.then.choose.exception")) { + + override fun getLookupString(element: String?) = element + override fun getResult(element: String?) = element + } + + val manager = TemplateManagerImpl(project) + val builder = TemplateBuilderImpl(thrownExpression) + builder.replaceElement(thrownExpression, exceptionLookupExpression); + + PsiDocumentManager.getInstance(project).commitAllDocuments(); + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); + editor.getCaretModel().moveToOffset(thrownExpression.getNode()!!.getStartOffset()); + + manager.startTemplate(editor, builder.buildInlineTemplate()!!, object: TemplateEditingAdapter() { + override fun templateFinished(template: Template?, brokenOff: Boolean) { + if (!isStable && !isStatement) { + ifStatement.introduceValueForCondition(ifStatement.getThen()!!, editor) + } + } + }) + } + + fun formatForUseInExceptionArgument(expressionText: String): String { + val lines = expressionText.split('\n') + + return if (lines.size > 1) + lines.first().trim() + " ..." + else + expressionText.trim() + } +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt b/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt new file mode 100644 index 00000000000..e22cfe09249 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt @@ -0,0 +1,7 @@ +fun foo(): String? { + return "foo" +} + +fun main(args: Array) { + foo()!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt.after new file mode 100644 index 00000000000..dc289096d3d --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/callExpression.kt.after @@ -0,0 +1,7 @@ +fun foo(): String? { + return "foo" +} + +fun main(args: Array) { + if (foo() == null) throw NullPointerException("Expression 'foo()' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt b/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt new file mode 100644 index 00000000000..61f3080210f --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt @@ -0,0 +1,7 @@ +fun foo(): String? { + return "foo" +} + +fun main(args: Array) { + (foo())!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt.after new file mode 100644 index 00000000000..dc289096d3d --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/callExpressionParens.kt.after @@ -0,0 +1,7 @@ +fun foo(): String? { + return "foo" +} + +fun main(args: Array) { + if (foo() == null) throw NullPointerException("Expression 'foo()' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt new file mode 100644 index 00000000000..f0b647d89c4 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt @@ -0,0 +1,5 @@ +fun main(args: Array) { + doSomething("one")!! +} + +fun doSomething(a: Any): Any? = null diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt.after new file mode 100644 index 00000000000..111f93aa05c --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextEscapesQuotes.kt.after @@ -0,0 +1,5 @@ +fun main(args: Array) { + if (doSomething("one") == null) throw NullPointerException("Expression 'doSomething(\"one\")' must not be null") +} + +fun doSomething(a: Any): Any? = null diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt new file mode 100644 index 00000000000..65623acb3f4 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt @@ -0,0 +1,7 @@ +fun main(args: Array) { + val t = doSomething("one" + 1, + "two", + 3 * 4)!! +} + +fun doSomething(vararg a: Any): Any? = null diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt.after new file mode 100644 index 00000000000..6097a7ef46d --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/exceptionTextHandlesMultiline.kt.after @@ -0,0 +1,8 @@ +fun main(args: Array) { + val any = doSomething("one" + 1, + "two", + 3 * 4) + val t = if (any != null) any else throw NullPointerException("Expression 'doSomething(\"one\" + 1, ...' must not be null") +} + +fun doSomething(vararg a: Any): Any? = null diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt b/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt new file mode 100644 index 00000000000..a8f413d708a --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt @@ -0,0 +1,4 @@ +fun main(args: Array) { + val a: String? = "A" + a!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt.after new file mode 100644 index 00000000000..1abe253ca22 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/localVal.kt.after @@ -0,0 +1,4 @@ +fun main(args: Array) { + val a: String? = "A" + if (a == null) throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt b/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt new file mode 100644 index 00000000000..c6c42fa7bb1 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt @@ -0,0 +1,6 @@ +fun main(args: Array) { + var a: String? = "A" + doSomething(a!!) +} + +fun doSomething(a: Any){} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt.after new file mode 100644 index 00000000000..52c80123634 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/localVar.kt.after @@ -0,0 +1,7 @@ +fun main(args: Array) { + var a: String? = "A" + val s = a + doSomething(if (s != null) s else throw NullPointerException("Expression 'a' must not be null")) +} + +fun doSomething(a: Any){} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt b/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt new file mode 100644 index 00000000000..a7640fb12fc --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt @@ -0,0 +1,4 @@ +fun main(args: Array) { + val foo: String? = "foo" + val t = (foo)!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt.after new file mode 100644 index 00000000000..c097ce285c2 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/simpleNameExpressionInParens.kt.after @@ -0,0 +1,4 @@ +fun main(args: Array) { + val foo: String? = "foo" + val t = if (foo != null) foo else throw NullPointerException("Expression 'foo' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt new file mode 100644 index 00000000000..4a5237a9bd7 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt @@ -0,0 +1,4 @@ +val a: String? = "A" +fun main(args: Array) { + a!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt.after new file mode 100644 index 00000000000..37132f5ce4b --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVal.kt.after @@ -0,0 +1,4 @@ +val a: String? = "A" +fun main(args: Array) { + if (a == null) throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt new file mode 100644 index 00000000000..bcfad7caaff --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt @@ -0,0 +1,6 @@ +val a: String? + get() = "" + +fun main(args: Array) { + val x = a!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt.after new file mode 100644 index 00000000000..c4b5f1a6732 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelValCustomGetter.kt.after @@ -0,0 +1,7 @@ +val a: String? + get() = "" + +fun main(args: Array) { + val s = a + val x = if (s != null) s else throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt new file mode 100644 index 00000000000..9dc7cea45a5 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt @@ -0,0 +1,4 @@ +var a: String? = "A" +fun main(args: Array) { + val t = a!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt.after new file mode 100644 index 00000000000..e17a2a482f1 --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVar.kt.after @@ -0,0 +1,5 @@ +var a: String? = "A" +fun main(args: Array) { + val s = a + val t = if (s != null) s else throw NullPointerException("Expression 'a' must not be null") +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt new file mode 100644 index 00000000000..154a2a9568f --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt @@ -0,0 +1,9 @@ +var a: String? + get() = "" + set(v) {} + +fun main(args: Array) { + doSomething(a!!) +} + +fun doSomething(a: Any){} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt.after new file mode 100644 index 00000000000..b508c304b0a --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/topLevelVarCustomGetter.kt.after @@ -0,0 +1,10 @@ +var a: String? + get() = "" + set(v) {} + +fun main(args: Array) { + val s = a + doSomething(if (s != null) s else throw NullPointerException("Expression 'a' must not be null")) +} + +fun doSomething(a: Any){} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt b/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt new file mode 100644 index 00000000000..6210bd3ca4c --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt @@ -0,0 +1,4 @@ +fun main(args: Array) { + val foo: String? = "foo" + val x = foo!! +} diff --git a/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt.after b/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt.after new file mode 100644 index 00000000000..ff28bae848e --- /dev/null +++ b/idea/testData/intentions/branched/doubleBangToIfThen/usedInAssignment.kt.after @@ -0,0 +1,4 @@ +fun main(args: Array) { + val foo: String? = "foo" + val x = if (foo != null) foo else throw NullPointerException("Expression 'foo' must not be null") +} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java index f774c28ff45..cc33812b16f 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/AbstractCodeTransformationTest.java @@ -38,6 +38,10 @@ import org.junit.Assert; import java.io.File; public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase { + public void doTestDoubleBangToIfThen(@NotNull String path) throws Exception { + doTestIntention(path, new DoubleBangToIfThenIntention()); + } + public void doTestElvisToIfThen(@NotNull String path) throws Exception { doTestIntention(path, new ElvisToIfThenIntention()); }