Extract Function: Fix type candidates for parameters with flexibly nullable types

#KT-6546 Fixed
This commit is contained in:
Alexey Sedunov
2014-12-26 14:54:28 +03:00
parent c8cb5f8c29
commit 64ddd52e60
6 changed files with 70 additions and 2 deletions
@@ -69,6 +69,8 @@ import org.jetbrains.jet.plugin.caches.resolve.findModuleDescriptor
import org.jetbrains.jet.plugin.caches.resolve.analyze
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.jet.plugin.refactoring.comparePossiblyOverridingDescriptors
import org.jetbrains.kotlin.plugin.util.makeNullable
import org.jetbrains.kotlin.plugin.util.makeNotNullable
private val DEFAULT_FUNCTION_NAME = "myFun"
private val DEFAULT_RETURN_TYPE = KotlinBuiltIns.getInstance().getUnitType()
@@ -473,8 +475,26 @@ private class MutableParameter(
override val parameterTypeCandidates: List<JetType> by Delegates.lazy {
writable = false
val superTypes = TypeUtils.getAllSupertypes(defaultType).filter(and(typePredicates))
(Collections.singletonList(defaultType) + superTypes).filter { it.isExtractable() }
val typePredicate = and(typePredicates)
val typeList = if (defaultType.isNullabilityFlexible()) {
val bounds = defaultType.getCapability(javaClass<Flexibility>())
if (typePredicate(bounds.upperBound)) arrayListOf(bounds.upperBound, bounds.lowerBound) else arrayListOf(bounds.lowerBound)
}
else arrayListOf(defaultType)
val addNullableTypes = typeList.size() > 1
val superTypes = TypeUtils.getAllSupertypes(defaultType).filter(typePredicate)
for (superType in superTypes) {
if (addNullableTypes) {
typeList.add(superType.makeNullable())
}
typeList.add(superType)
}
typeList.filter { it.isExtractable() }
}
override val parameterType: JetType by Delegates.lazy { parameterTypeCandidates.firstOrNull() ?: defaultType }
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// PARAM_TYPES: String?, String, kotlin.CharSequence?, CharSequence
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
fun test() {
val property = System.getProperty("some")
val n = <selection>property?.length()</selection>
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// PARAM_TYPES: String?, String, kotlin.CharSequence?, CharSequence
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
fun test() {
val property = System.getProperty("some")
val n = i(property)
}
private fun i(property: String?): Int? {
return property?.length()
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// PARAM_TYPES: String, CharSequence
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
fun test() {
val property = System.getProperty("some")
val n = <selection>property.length()</selection>
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// PARAM_TYPES: String, CharSequence
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
fun test() {
val property = System.getProperty("some")
val n = i(property)
}
private fun i(property: String): Int {
return property.length()
}
@@ -1421,6 +1421,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest(fileName);
}
@TestMetadata("flexibleTypesWithNull.kt")
public void testFlexibleTypesWithNull() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/flexibleTypesWithNull.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("flexibleTypesWithoutNull.kt")
public void testFlexibleTypesWithoutNull() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/flexibleTypesWithoutNull.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("liftAnonymousToSupertype1.kt")
public void testLiftAnonymousToSupertype1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/liftAnonymousToSupertype1.kt");