Add quick fix for 'JAVA_CLASS_ON_COMPANION'
#KT-29264 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
d61158a176
commit
15a615d63b
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
|
||||
object JavaClassOnCompanionFixes : KotlinIntentionActionsFactory() {
|
||||
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
|
||||
val expression = diagnostic.psiElement.parent as? KtDotQualifiedExpression ?: return emptyList()
|
||||
val companionName = expression.receiverExpression.mainReference?.resolve()?.safeAs<KtObjectDeclaration>()?.name ?: "Companion"
|
||||
return listOf(
|
||||
ReplaceWithCompanionClassJavaFix(expression, companionName),
|
||||
ReplaceWithClassJavaFix(expression)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class ReplaceWithCompanionClassJavaFix(
|
||||
expression: KtDotQualifiedExpression,
|
||||
private val companionName: String
|
||||
) : KotlinQuickFixAction<KtDotQualifiedExpression>(expression) {
|
||||
override fun getText(): String = KotlinBundle.message("replace.with.0", "$companionName::class.java")
|
||||
|
||||
override fun getFamilyName(): String = text
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
element?.replace("$0.$companionName::class.java")
|
||||
}
|
||||
}
|
||||
|
||||
class ReplaceWithClassJavaFix(
|
||||
expression: KtDotQualifiedExpression
|
||||
) : KotlinQuickFixAction<KtDotQualifiedExpression>(expression), LowPriorityAction {
|
||||
override fun getText(): String = KotlinBundle.message("replace.with.0", "::class.java")
|
||||
|
||||
override fun getFamilyName(): String = text
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
element?.replace("$0::class.java")
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtDotQualifiedExpression.replace(pattern: String) {
|
||||
replace(KtPsiFactory(this).createExpressionByPattern(pattern, receiverExpression))
|
||||
}
|
||||
@@ -668,5 +668,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
INCOMPATIBLE_THROWS_OVERRIDE.registerFactory(RemoveAnnotationFix)
|
||||
|
||||
INLINE_CLASS_CONSTRUCTOR_NOT_FINAL_READ_ONLY_PARAMETER.registerFactory(InlineClassConstructorNotValParameterFactory)
|
||||
|
||||
JAVA_CLASS_ON_COMPANION.registerFactory(JavaClassOnCompanionFixes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
// DISABLE-ERRORS
|
||||
fun main() {
|
||||
val c: Class<Int.Companion> = Int.javaClass<caret>
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
// DISABLE-ERRORS
|
||||
fun main() {
|
||||
val c: Class<Int.Companion> = Int::class.java
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c = Int.javaClass<caret>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c = Int::class.java
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val name = Int.javaClass<caret>.name
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with '::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val name = Int::class.java.name
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c: Class<Int.Companion> = Int.javaClass<caret>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c: Class<Int.Companion> = Int.Companion::class.java
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'Bar::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
class Foo {
|
||||
companion object Bar
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Foo.javaClass<caret>
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'Bar::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
class Foo {
|
||||
companion object Bar
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Foo.Bar::class.java
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c = Int.javaClass<caret>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val c = Int.Companion::class.java
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val name = Int.javaClass<caret>.name
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// "Replace with 'Companion::class.java'" "true"
|
||||
// WITH_RUNTIME
|
||||
fun main() {
|
||||
val name = Int.Companion::class.java.name
|
||||
}
|
||||
+39
@@ -3087,6 +3087,45 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class JavaClassOnCompanion extends AbstractQuickFixMultiFileTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInJavaClassOnCompanion() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceWithClassJava extends AbstractQuickFixMultiFileTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceWithClassJava() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceWithCompanionClassJava extends AbstractQuickFixMultiFileTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceWithCompanionClassJava() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/kdocMissingDocumentation")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -8657,6 +8657,80 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class JavaClassOnCompanion extends AbstractQuickFixTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInJavaClassOnCompanion() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceWithClassJava extends AbstractQuickFixTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceWithClassJava() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("hasType.kt")
|
||||
public void testHasType() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava/hasType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava/simple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("withName.kt")
|
||||
public void testWithName() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithClassJava/withName.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceWithCompanionClassJava extends AbstractQuickFixTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceWithCompanionClassJava() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@TestMetadata("hasType.kt")
|
||||
public void testHasType() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava/hasType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("namedCompanion.kt")
|
||||
public void testNamedCompanion() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava/namedCompanion.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava/simple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("withName.kt")
|
||||
public void testWithName() throws Exception {
|
||||
runTest("idea/testData/quickfix/javaClassOnCompanion/replaceWithCompanionClassJava/withName.kt");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/kdocMissingDocumentation")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user