KT-9128 ReplaceWith incorrectly converts javaClass<X>() to X::class.java for type with arguments
#KT-9128 Fixed
This commit is contained in:
+16
-2
@@ -207,19 +207,33 @@ private fun ConstructedExpressionWrapper.processTypeParameterUsages(resolvedCall
|
||||
}
|
||||
|
||||
val factory = JetPsiFactory(callElement)
|
||||
val type = resolvedCall.typeArguments[typeParameter]!!
|
||||
val typeElement = if (explicitTypeArgs != null) { // we use explicit type arguments if available to avoid shortening
|
||||
val _typeElement = explicitTypeArgs[index].typeReference?.typeElement ?: continue
|
||||
_typeElement.marked(USER_CODE_KEY)
|
||||
}
|
||||
else {
|
||||
val type = resolvedCall.typeArguments[typeParameter]!!
|
||||
factory.createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type)).typeElement!!
|
||||
}
|
||||
|
||||
// for class literal ("X::class") we need type arguments only for kotlin.Array
|
||||
val typeClassifier = type.constructor.declarationDescriptor
|
||||
val classLiteralTypeElement = if (typeElement is JetUserType && typeElement.typeArgumentList == null || typeClassifier == null || KotlinBuiltIns.isArray(type)) {
|
||||
typeElement
|
||||
}
|
||||
else {
|
||||
factory.createType(IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(typeClassifier)).typeElement!!
|
||||
}
|
||||
|
||||
for (usage in usages) {
|
||||
val parent = usage.parent
|
||||
if (parent is JetUserType) {
|
||||
parent.replace(typeElement)
|
||||
if (parent.parent is JetTypeReference && parent.parent.parent is JetClassLiteralExpression) {
|
||||
parent.replace(classLiteralTypeElement)
|
||||
}
|
||||
else {
|
||||
parent.replace(typeElement)
|
||||
}
|
||||
}
|
||||
else {
|
||||
//TODO: tests for this?
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace usages of 'myJavaClass(): Class<T>' in whole project" "true"
|
||||
|
||||
@Deprecated("", ReplaceWith("T::class.java"))
|
||||
inline fun <reified T: Any> myJavaClass(): Class<T> = T::class.java
|
||||
|
||||
fun foo() {
|
||||
val v1 = <caret>myJavaClass<List<*>>()
|
||||
val v2 = myJavaClass<List<String>>()
|
||||
val v3 = myJavaClass<Array<String>>()
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace usages of 'myJavaClass(): Class<T>' in whole project" "true"
|
||||
|
||||
@Deprecated("", ReplaceWith("T::class.java"))
|
||||
inline fun <reified T: Any> myJavaClass(): Class<T> = T::class.java
|
||||
|
||||
fun foo() {
|
||||
val v1 = List::class.java
|
||||
val v2 = List::class.java
|
||||
val v3 = Array<String>::class.java
|
||||
}
|
||||
@@ -2998,6 +2998,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("classLiteralAndTypeArgsRuntime.kt")
|
||||
public void testClassLiteralAndTypeArgsRuntime() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/classLiteralAndTypeArgsRuntime.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("doNotShortenUserReferences.kt")
|
||||
public void testDoNotShortenUserReferences() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/deprecatedSymbolUsage/doNotShortenUserReferences.kt");
|
||||
|
||||
Reference in New Issue
Block a user