Refactoring. Minor refactoring and cleanup in FlexibleTypes.

This commit is contained in:
Stanislav Erokhin
2016-04-22 18:58:52 +03:00
parent a1d052b8fa
commit feec3566a4
3 changed files with 22 additions and 38 deletions
@@ -294,18 +294,19 @@ class LazyJavaTypeResolver(
} }
private class Impl(lowerBound: KotlinType, upperBound: KotlinType) : private class Impl(lowerBound: KotlinType, upperBound: KotlinType) :
DelegatingFlexibleType(lowerBound, upperBound, FlexibleJavaClassifierTypeFactory), CustomTypeVariable, Specificity { DelegatingFlexibleType(lowerBound, upperBound, FlexibleJavaClassifierTypeFactory), CustomTypeVariable {
override val delegateType: KotlinType get() = lowerBound
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return when (capabilityClass) { if (capabilityClass == CustomTypeVariable::class.java) return this as T
CustomTypeVariable::class.java, Specificity::class.java -> this as T
else -> super.getCapability(capabilityClass) return super.getCapability(capabilityClass)
}
} }
override val isTypeVariable: Boolean = lowerBound.getConstructor() == upperBound.getConstructor() override val isTypeVariable: Boolean = lowerBound.constructor == upperBound.constructor
&& lowerBound.getConstructor().getDeclarationDescriptor() is TypeParameterDescriptor && lowerBound.constructor.declarationDescriptor is TypeParameterDescriptor
override fun substitutionResult(replacement: KotlinType): KotlinType { override fun substitutionResult(replacement: KotlinType): KotlinType {
return if (replacement.isFlexible()) replacement return if (replacement.isFlexible()) replacement
@@ -323,7 +324,7 @@ class LazyJavaTypeResolver(
// Int! >< Int? // Int! >< Int?
if (otherType.isFlexible()) return Specificity.Relation.DONT_KNOW if (otherType.isFlexible()) return Specificity.Relation.DONT_KNOW
// Int? >< Int! // Int? >< Int!
if (otherType.isMarkedNullable()) return Specificity.Relation.DONT_KNOW if (otherType.isMarkedNullable) return Specificity.Relation.DONT_KNOW
// Int! lessSpecific Int // Int! lessSpecific Int
return Specificity.Relation.LESS_SPECIFIC return Specificity.Relation.LESS_SPECIFIC
} }
@@ -43,19 +43,12 @@ object DynamicTypeFactory : FlexibleTypeFactory {
return Impl(lowerBound, upperBound) return Impl(lowerBound, upperBound)
} }
private class Impl(lowerBound: KotlinType, upperBound: KotlinType) : DelegatingFlexibleType(lowerBound, upperBound, DynamicTypeFactory), Dynamicity, Specificity, NullAwareness, FlexibleTypeDelegation { private class Impl(lowerBound: KotlinType, upperBound: KotlinType) :
companion object { DelegatingFlexibleType(lowerBound, upperBound, DynamicTypeFactory), Dynamicity {
internal val capabilityClasses = hashSetOf(
Dynamicity::class.java,
Specificity::class.java,
NullAwareness::class.java,
FlexibleTypeDelegation::class.java
)
}
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
if (capabilityClass in capabilityClasses) return this as T if (capabilityClass == Dynamicity::class.java) return this as T
return super.getCapability(capabilityClass) return super.getCapability(capabilityClass)
} }
@@ -71,6 +64,6 @@ object DynamicTypeFactory : FlexibleTypeFactory {
return createDynamicType(delegateType.builtIns) return createDynamicType(delegateType.builtIns)
} }
override fun computeIsNullable() = false override fun isMarkedNullable() = false
} }
} }
@@ -87,24 +87,19 @@ fun KotlinType.upperIfFlexible(): KotlinType = if (this.isFlexible()) this.flexi
interface NullAwareness : TypeCapability { interface NullAwareness : TypeCapability {
fun makeNullableAsSpecified(nullable: Boolean): KotlinType fun makeNullableAsSpecified(nullable: Boolean): KotlinType
fun computeIsNullable(): Boolean
} }
interface FlexibleTypeDelegation : TypeCapability { abstract class DelegatingFlexibleType protected constructor(
val delegateType: KotlinType
}
open class DelegatingFlexibleType protected constructor(
override val lowerBound: KotlinType, override val lowerBound: KotlinType,
override val upperBound: KotlinType, override val upperBound: KotlinType,
override val factory: FlexibleTypeFactory override val factory: FlexibleTypeFactory
) : DelegatingType(), NullAwareness, Flexibility, FlexibleTypeDelegation { ) : DelegatingType(), NullAwareness, Flexibility, Specificity {
companion object { companion object {
internal val capabilityClasses = hashSetOf( internal val capabilityClasses = hashSetOf(
NullAwareness::class.java, NullAwareness::class.java,
Flexibility::class.java, Flexibility::class.java,
SubtypingRepresentatives::class.java, SubtypingRepresentatives::class.java,
FlexibleTypeDelegation::class.java Specificity::class.java
) )
@JvmField @JvmField
@@ -131,6 +126,8 @@ open class DelegatingFlexibleType protected constructor(
} }
} }
protected abstract val delegateType: KotlinType
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
if (capabilityClass in capabilityClasses) return this as T if (capabilityClass in capabilityClasses) return this as T
@@ -143,17 +140,10 @@ open class DelegatingFlexibleType protected constructor(
TypeUtils.makeNullableAsSpecified(upperBound, nullable)) TypeUtils.makeNullableAsSpecified(upperBound, nullable))
} }
override fun computeIsNullable() = delegateType.isMarkedNullable final override fun getDelegate(): KotlinType {
runAssertions()
override fun isMarkedNullable(): Boolean = getCapability(NullAwareness::class.java)!!.computeIsNullable() return delegateType
}
override val delegateType: KotlinType
get() {
runAssertions()
return lowerBound
}
override fun getDelegate() = getCapability(FlexibleTypeDelegation::class.java)!!.delegateType
override fun toString() = "('$lowerBound'..'$upperBound')" override fun toString() = "('$lowerBound'..'$upperBound')"
} }