"Change JVM name" (@JvmName) quickfix: improve name suggester for generic functions

#KT-38559 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-04-25 11:14:39 +09:00
committed by Yan Zhulanow
parent 5efbbdea57
commit 6868f53f46
8 changed files with 77 additions and 2 deletions
@@ -55,9 +55,24 @@ class AddJvmNameAnnotationFix(element: KtElement, private val jvmName: String) :
function,
NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES
)
val jvmName = KotlinNameSuggester.suggestNameByName(functionName, nameValidator)
val receiverTypeElements = function.receiverTypeReference?.typeElements()?.joinToString("") { it.text } ?: ""
val jvmName = KotlinNameSuggester.suggestNameByName(functionName + receiverTypeElements, nameValidator)
return AddJvmNameAnnotationFix(function.findAnnotation(JVM_NAME_FQ_NAME) ?: function, jvmName)
}
private fun KtTypeReference.typeElements(): List<KtTypeElement> {
val typeElements = mutableListOf<KtTypeElement>()
fun collect(typeReference: KtTypeReference) {
val typeElement = typeReference.typeElement ?: return
val typeArguments = typeElement.typeArgumentsAsTypes
if (typeArguments.isEmpty()) {
typeElements.add(typeElement)
} else {
typeArguments.forEach { collect(it) }
}
}
typeElement?.typeArgumentsAsTypes?.forEach { collect(it) }
return typeElements
}
}
}
@@ -0,0 +1,7 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Foo<T>
fun Foo<Int>.foo() = this
fun <caret>Foo<String>.foo() = this
@@ -0,0 +1,8 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Foo<T>
fun Foo<Int>.foo() = this
@JvmName("fooString")
fun Foo<String>.foo() = this
@@ -0,0 +1,7 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Bar<T, U>
fun <caret>Bar<Int, Double>.bar() = this
fun Bar<Int, Bar<Long, Bar<Double, String>>>.bar() = this
@@ -0,0 +1,8 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Bar<T, U>
@JvmName("barIntDouble")
fun Bar<Int, Double>.bar() = this
fun Bar<Int, Bar<Long, Bar<Double, String>>>.bar() = this
@@ -0,0 +1,7 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Bar<T, U>
fun Bar<Int, Double>.bar() = this
fun <caret>Bar<Int, Bar<Long, Bar<Double, String>>>.bar() = this
@@ -0,0 +1,8 @@
// "Add '@JvmName' annotation" "true"
// WITH_RUNTIME
interface Bar<T, U>
fun Bar<Int, Double>.bar() = this
@JvmName("barIntLongDoubleString")
fun Bar<Int, Bar<Long, Bar<Double, String>>>.bar() = this
@@ -879,6 +879,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/addJvmNameAnnotation/basic.kt");
}
@TestMetadata("extension.kt")
public void testExtension() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/extension.kt");
}
@TestMetadata("extension2.kt")
public void testExtension2() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/extension2.kt");
}
@TestMetadata("extension3.kt")
public void testExtension3() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/extension3.kt");
}
@TestMetadata("hasAnnotation.kt")
public void testHasAnnotation() throws Exception {
runTest("idea/testData/quickfix/addJvmNameAnnotation/hasAnnotation.kt");