mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] Java methods should be replaced with Kotlin analog: parenthesize the receiver as needed
^KTIJ-23953 Fixed closes https://github.com/JetBrains/intellij-community/pull/2275 GitOrigin-RevId: d6234badaeecc2cd526297231a7931fa8f1f85c8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b497b68505
commit
2e6d1d554e
+9
-9
@@ -2,15 +2,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections.jdk2k
|
||||
|
||||
import org.jetbrains.kotlin.idea.base.psi.replaced
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||
import org.jetbrains.kotlin.idea.base.psi.replaced
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.KtOperationExpression
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.calls.util.getType
|
||||
@@ -39,17 +37,19 @@ object ToExtensionFunctionWithNonNullableReceiver : Transformation {
|
||||
val psiFactory = KtPsiFactory(callExpression.project)
|
||||
val valueArguments = callExpression.valueArguments
|
||||
val typeArguments = callExpression.typeArgumentList?.text ?: ""
|
||||
val receiverText = valueArguments.first().getArgumentExpression()
|
||||
?.run { if (this is KtOperationExpression) "($text)" else text }
|
||||
?: valueArguments.first().text
|
||||
val receiverText = valueArguments.first().let { it.getArgumentExpression()?.text ?: it.text }
|
||||
val argumentsText = valueArguments.drop(1).joinToString(separator = ", ") { it.text }
|
||||
|
||||
val oldExpression = callExpression.getQualifiedExpressionForSelectorOrThis()
|
||||
val commentSaver = CommentSaver(oldExpression)
|
||||
|
||||
val replaced = oldExpression.replaced(
|
||||
psiFactory.createExpression("$receiverText.${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)")
|
||||
)
|
||||
psiFactory.createExpression("($receiverText).${replacement.kotlinFunctionShortName}$typeArguments($argumentsText)")
|
||||
) as KtDotQualifiedExpression
|
||||
val receiver = replaced.receiverExpression as KtParenthesizedExpression
|
||||
if (KtPsiUtil.areParenthesesUseless(receiver)) {
|
||||
receiver.expression?.let { receiver.replace(it) }
|
||||
}
|
||||
commentSaver.restore(replaced)
|
||||
|
||||
file.resolveImportReference(FqName(replacement.kotlinFunctionFqName)).firstOrNull()?.let {
|
||||
|
||||
+10
@@ -12312,6 +12312,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
@TestMetadata("addParentheses.kt")
|
||||
public void testAddParentheses() throws Exception {
|
||||
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/addParentheses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("byte.kt")
|
||||
public void testByte() throws Exception {
|
||||
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/compare/byte.kt");
|
||||
@@ -12629,6 +12634,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
@TestMetadata("addParentheses.kt")
|
||||
public void testAddParentheses() throws Exception {
|
||||
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/addParentheses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("byteToString.kt")
|
||||
public void testByteToString() throws Exception {
|
||||
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/byteToString.kt");
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
fun foo(b: Boolean) {
|
||||
val t = Integer.<caret>compare(if (b) 1 else 10, 6)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
fun foo(b: Boolean) {
|
||||
val t = (if (b) 1 else 10).compareTo(6)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
fun test(b: Boolean) {
|
||||
Integer.toString<caret>(if (b) 1 else 0)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// WITH_STDLIB
|
||||
|
||||
fun test(b: Boolean) {
|
||||
(if (b) 1 else 0).toString()
|
||||
}
|
||||
Reference in New Issue
Block a user