From e7fa2cb776347dc7959bb475add9c76ab5fc1a94 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 17 Oct 2015 11:44:05 +0300 Subject: [PATCH] Replace with explicit "get" inspection enabled by default but is reported only for functions explicitly marked with "operator" --- idea/src/META-INF/plugin.xml | 4 ++-- .../conventionNameCalls/ReplaceGetIntention.kt | 13 ++++++++++++- .../conventionNameCalls/replaceGet/JavaMethods.java | 12 ++++++++++++ .../replaceGet/inspectionData/expected.xml | 9 +++++++++ .../conventionNameCalls/replaceGet/javaMethods.kt | 8 ++++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 6 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/conventionNameCalls/replaceGet/JavaMethods.java create mode 100644 idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 93fb540049f..6da8678761b 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1099,8 +1099,8 @@ (ReplaceGetIntention()) +public class ExplicitGetInspection : IntentionBasedInspection( + ReplaceGetIntention(), + additionalChecker = { expression -> (expression.toResolvedCall()!!.resultingDescriptor as FunctionDescriptor).isExplicitOperator() } +) + +private fun FunctionDescriptor.isExplicitOperator(): Boolean { + return if (overriddenDescriptors.isEmpty()) + containingDeclaration !is JavaClassDescriptor && isOperator + else + overriddenDescriptors.any { it.isExplicitOperator() } +} public class ReplaceGetIntention : JetSelfTargetingRangeIntention(javaClass(), "Replace 'get' call with index operator"), HighPriorityAction { override fun applicabilityRange(element: JetDotQualifiedExpression): TextRange? { diff --git a/idea/testData/intentions/conventionNameCalls/replaceGet/JavaMethods.java b/idea/testData/intentions/conventionNameCalls/replaceGet/JavaMethods.java new file mode 100644 index 00000000000..3d3851c18b5 --- /dev/null +++ b/idea/testData/intentions/conventionNameCalls/replaceGet/JavaMethods.java @@ -0,0 +1,12 @@ +import java.util.List; + +public abstract class JavaMethods implements List { + @Override + public String get(int i) { + return null; + } + + public int get(String s, int p) { + return 1; + } +} \ No newline at end of file diff --git a/idea/testData/intentions/conventionNameCalls/replaceGet/inspectionData/expected.xml b/idea/testData/intentions/conventionNameCalls/replaceGet/inspectionData/expected.xml index 59d765eeadc..82129038e30 100644 --- a/idea/testData/intentions/conventionNameCalls/replaceGet/inspectionData/expected.xml +++ b/idea/testData/intentions/conventionNameCalls/replaceGet/inspectionData/expected.xml @@ -61,4 +61,13 @@ Explicit 'get' Replace 'get' call with index operator + + + javaMethods.kt + 6 + light_idea_test_case + + Explicit 'get' + Replace 'get' call with index operator + diff --git a/idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt b/idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt new file mode 100644 index 00000000000..9ebd7965324 --- /dev/null +++ b/idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt @@ -0,0 +1,8 @@ +// DISABLE-ERRORS +// IS_APPLICABLE: false +import JavaMethods + +fun foo(javaClass: JavaMethods) { + val v1 = javaClass.get(1) + val v2 = javaClass.get("a", 1) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index d3dddac23a4..ee23778fad9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2765,6 +2765,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("javaMethods.kt") + public void testJavaMethods() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGet/javaMethods.kt"); + doTest(fileName); + } + @TestMetadata("missingDefaultArgument.kt") public void testMissingDefaultArgument() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceGet/missingDefaultArgument.kt");