Extract Function: Do not use flexible type as default: use one of its bounds instead
#KT-9099 Fixed
This commit is contained in:
+11
-8
@@ -541,23 +541,26 @@ private class MutableParameter(
|
||||
|
||||
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)
|
||||
val typeSet = if (defaultType.isFlexible()) {
|
||||
val bounds = defaultType.getCapability(javaClass<Flexibility>())!!
|
||||
LinkedHashSet<JetType>().apply {
|
||||
if (typePredicate(bounds.upperBound)) add(bounds.upperBound)
|
||||
if (typePredicate(bounds.lowerBound)) add(bounds.lowerBound)
|
||||
}
|
||||
}
|
||||
else arrayListOf(defaultType)
|
||||
else linkedSetOf(defaultType)
|
||||
|
||||
val addNullableTypes = typeList.size() > 1
|
||||
val addNullableTypes = defaultType.isNullabilityFlexible() && typeSet.size() > 1
|
||||
val superTypes = TypeUtils.getAllSupertypes(defaultType).filter(typePredicate)
|
||||
|
||||
for (superType in superTypes) {
|
||||
if (addNullableTypes) {
|
||||
typeList.add(superType.makeNullable())
|
||||
typeSet.add(superType.makeNullable())
|
||||
}
|
||||
typeList.add(superType)
|
||||
typeSet.add(superType)
|
||||
}
|
||||
|
||||
typeList
|
||||
typeSet.toList()
|
||||
}
|
||||
|
||||
override fun getParameterTypeCandidates(allowSpecialClassNames: Boolean): List<JetType> {
|
||||
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import java.util.*;
|
||||
|
||||
class J {
|
||||
public List<String> getData() {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// PARAM_DESCRIPTOR: val data: (kotlin.MutableList<(kotlin.String..kotlin.String?)>..kotlin.List<(kotlin.String..kotlin.String?)>) defined in test
|
||||
// PARAM_TYPES: kotlin.List<(kotlin.String..kotlin.String?)>, kotlin.MutableList<(kotlin.String..kotlin.String?)>, kotlin.MutableCollection<(kotlin.String..kotlin.String?)>, kotlin.Collection<(kotlin.String..kotlin.String?)>
|
||||
fun test(): Boolean {
|
||||
val j: J? = null
|
||||
val data = j?.getData() ?: return false
|
||||
return <selection>data.contains("foo")</selection>
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
// PARAM_DESCRIPTOR: val data: (kotlin.MutableList<(kotlin.String..kotlin.String?)>..kotlin.List<(kotlin.String..kotlin.String?)>) defined in test
|
||||
// PARAM_TYPES: kotlin.List<(kotlin.String..kotlin.String?)>, kotlin.MutableList<(kotlin.String..kotlin.String?)>, kotlin.MutableCollection<(kotlin.String..kotlin.String?)>, kotlin.Collection<(kotlin.String..kotlin.String?)>
|
||||
fun test(): Boolean {
|
||||
val j: J? = null
|
||||
val data = j?.getData() ?: return false
|
||||
return b(data)
|
||||
}
|
||||
|
||||
private fun b(data: List<String>) = data.contains("foo")
|
||||
+6
@@ -1802,6 +1802,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
doExtractFunctionTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("mutablyFlexibleType.kt")
|
||||
public void testMutablyFlexibleType() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/mutablyFlexibleType.kt");
|
||||
doExtractFunctionTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nonNullableTypes.kt")
|
||||
public void testNonNullableTypes() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/nonNullableTypes.kt");
|
||||
|
||||
Reference in New Issue
Block a user