"Convert try / finally to .use { } call intention introduced" #KT-12100 Fixed
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun main(args: Array<String>) {
|
||||||
|
val writer = File("hello-world.txt").bufferedReader()
|
||||||
|
writer.use { writer ->
|
||||||
|
// do stuff with writer
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
fun main(args: Array<String>) {
|
||||||
|
val writer = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
// do stuff with writer
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This intention converts a try-finally block with resource.close() in finally into the resource.use() call
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1446,6 +1446,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.ConvertTryFinallyToUseCallIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||||
displayName="Object literal can be converted to lambda"
|
displayName="Object literal can be converted to lambda"
|
||||||
groupName="Kotlin"
|
groupName="Kotlin"
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 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.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.search.LocalSearchScope
|
||||||
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.contentRange
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.supertypes
|
||||||
|
|
||||||
|
class ConvertTryFinallyToUseCallIntention : SelfTargetingOffsetIndependentIntention<KtTryExpression>(
|
||||||
|
KtTryExpression::class.java, "Convert try-finally to .use()"
|
||||||
|
) {
|
||||||
|
override fun applyTo(element: KtTryExpression, editor: Editor?) {
|
||||||
|
val finallySection = element.finallyBlock!!
|
||||||
|
val finallyDotCall = finallySection.finalExpression.statements.singleOrNull() as KtDotQualifiedExpression
|
||||||
|
val resourceReference = finallyDotCall.receiverExpression as KtNameReferenceExpression
|
||||||
|
|
||||||
|
val factory = KtPsiFactory(element)
|
||||||
|
|
||||||
|
val useCallExpression = factory.buildExpression {
|
||||||
|
appendName(resourceReference.getReferencedNameAsName())
|
||||||
|
appendFixedText(".use {")
|
||||||
|
|
||||||
|
appendName(resourceReference.getReferencedNameAsName())
|
||||||
|
appendFixedText("->")
|
||||||
|
|
||||||
|
appendChildRange(element.tryBlock.contentRange())
|
||||||
|
appendFixedText("}")
|
||||||
|
}
|
||||||
|
|
||||||
|
element.replace(useCallExpression)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isApplicableTo(element: KtTryExpression): Boolean {
|
||||||
|
// Single statement in finally, no catch blocks
|
||||||
|
val finallySection = element.finallyBlock ?: return false
|
||||||
|
val finallyDotCall = finallySection.finalExpression.statements.singleOrNull() as? KtDotQualifiedExpression ?: return false
|
||||||
|
if (element.catchClauses.isNotEmpty()) return false
|
||||||
|
|
||||||
|
// Like resource.close()
|
||||||
|
val resourceReference = finallyDotCall.receiverExpression as? KtNameReferenceExpression ?: return false
|
||||||
|
val resourceCall = finallyDotCall.selectorExpression as? KtCallExpression ?: return false
|
||||||
|
if (resourceCall.calleeExpression?.text != "close") return false
|
||||||
|
|
||||||
|
// resource is Closeable immutable local variable
|
||||||
|
val resourceDescriptor =
|
||||||
|
element.analyze().get(BindingContext.REFERENCE_TARGET, resourceReference) as? VariableDescriptor ?: return false
|
||||||
|
return !resourceDescriptor.isVar && resourceDescriptor.type.supertypes().any {
|
||||||
|
it.constructor.declarationDescriptor?.fqNameSafe?.asString().let {
|
||||||
|
it == "java.io.Closeable" || it == "java.lang.AutoCloseable"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.ConvertTryFinallyToUseCallIntention
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
catch (e: IOException) {
|
||||||
|
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
reader.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
// do stuff with reader
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
reader.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
reader.use { reader -> // do stuff with reader }
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
reader.readLine()
|
||||||
|
reader.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
try {
|
||||||
|
reader.readLine()
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
reader.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val reader = File("hello-world.txt").bufferedReader()
|
||||||
|
reader.use { reader -> reader.readLine() }
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val writer = File("hello-world.txt").bufferedWriter()
|
||||||
|
try {
|
||||||
|
writer.write("123")
|
||||||
|
writer.newLine()
|
||||||
|
writer.write("456")
|
||||||
|
}
|
||||||
|
<caret>finally {
|
||||||
|
writer.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
val writer = File("hello-world.txt").bufferedWriter()
|
||||||
|
writer.use { writer ->
|
||||||
|
writer.write("123")
|
||||||
|
writer.newLine()
|
||||||
|
writer.write("456")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6088,6 +6088,51 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/convertTryFinallyToUseCall")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ConvertTryFinallyToUseCall extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInConvertTryFinallyToUseCall() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertTryFinallyToUseCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("catch.kt")
|
||||||
|
public void testCatch() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/catch.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("example.kt")
|
||||||
|
public void testExample() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/example.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notClose.kt")
|
||||||
|
public void testNotClose() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/notClose.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notOnlyClose.kt")
|
||||||
|
public void testNotOnlyClose() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/notOnlyClose.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("triple.kt")
|
||||||
|
public void testTriple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertTryFinallyToUseCall/triple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
|
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user