Introduce JavaTypeEnhancement component

It's necessary to allow using language version settings in type enancement
This commit is contained in:
Denis Zharkov
2020-06-29 16:00:29 +03:00
parent 240311d9c7
commit f1c68a9080
2 changed files with 99 additions and 91 deletions
@@ -18,7 +18,10 @@ package org.jetbrains.kotlin.load.java.typeEnhancement
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.* import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import org.jetbrains.kotlin.load.java.* import org.jetbrains.kotlin.load.java.*
import org.jetbrains.kotlin.load.java.descriptors.* import org.jetbrains.kotlin.load.java.descriptors.*
import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext
@@ -31,7 +34,6 @@ import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.deprecation.DEPRECATED_FUNCTION_KEY import org.jetbrains.kotlin.resolve.deprecation.DEPRECATED_FUNCTION_KEY
import org.jetbrains.kotlin.resolve.descriptorUtil.firstArgument import org.jetbrains.kotlin.resolve.descriptorUtil.firstArgument
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.descriptorUtil.isSourceAnnotation
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
@@ -46,7 +48,8 @@ data class NullabilityQualifierWithMigrationStatus(
class SignatureEnhancement( class SignatureEnhancement(
private val annotationTypeQualifierResolver: AnnotationTypeQualifierResolver, private val annotationTypeQualifierResolver: AnnotationTypeQualifierResolver,
private val jsr305State: Jsr305State private val jsr305State: Jsr305State,
private val typeEnhancement: JavaTypeEnhancement
) { ) {
private fun AnnotationDescriptor.extractNullabilityTypeFromArgument(): NullabilityQualifierWithMigrationStatus? { private fun AnnotationDescriptor.extractNullabilityTypeFromArgument(): NullabilityQualifierWithMigrationStatus? {
@@ -241,10 +244,12 @@ class SignatureEnhancement(
classifier.fqNameOrNull() == JavaToKotlinClassMap.FUNCTION_N_FQ_NAME classifier.fqNameOrNull() == JavaToKotlinClassMap.FUNCTION_N_FQ_NAME
} }
return fromOverride.enhance(qualifiersWithPredefined ?: qualifiers)?.let { enhanced -> return with(typeEnhancement) {
fromOverride.enhance(qualifiersWithPredefined ?: qualifiers)?.let { enhanced ->
PartEnhancementResult(enhanced, wereChanges = true, containsFunctionN = containsFunctionN) PartEnhancementResult(enhanced, wereChanges = true, containsFunctionN = containsFunctionN)
} ?: PartEnhancementResult(fromOverride, wereChanges = false, containsFunctionN = containsFunctionN) } ?: PartEnhancementResult(fromOverride, wereChanges = false, containsFunctionN = containsFunctionN)
} }
}
private fun KotlinType.extractQualifiers(): JavaTypeQualifiers { private fun KotlinType.extractQualifiers(): JavaTypeQualifiers {
val (lower, upper) = val (lower, upper) =
@@ -39,12 +39,6 @@ import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.types.typeUtil.createProjection import org.jetbrains.kotlin.types.typeUtil.createProjection
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
// The index in the lambda is the position of the type component:
// Example: for `A<B, C<D, E>>`, indices go as follows: `0 - A<...>, 1 - B, 2 - C<D, E>, 3 - D, 4 - E`,
// which corresponds to the left-to-right breadth-first walk of the tree representation of the type.
// For flexible types, both bounds are indexed in the same way: `(A<B>..C<D>)` gives `0 - (A<B>..C<D>), 1 - B and D`.
fun KotlinType.enhance(qualifiers: (Int) -> JavaTypeQualifiers) = unwrap().enhancePossiblyFlexible(qualifiers, 0).typeIfChanged
fun KotlinType.hasEnhancedNullability(): Boolean = fun KotlinType.hasEnhancedNullability(): Boolean =
SimpleClassicTypeSystemContext.hasEnhancedNullability(this) SimpleClassicTypeSystemContext.hasEnhancedNullability(this)
@@ -57,12 +51,20 @@ enum class TypeComponentPosition {
INFLEXIBLE INFLEXIBLE
} }
class JavaTypeEnhancement {
private open class Result(open val type: KotlinType, val subtreeSize: Int, val wereChanges: Boolean) { private open class Result(open val type: KotlinType, val subtreeSize: Int, val wereChanges: Boolean) {
val typeIfChanged: KotlinType? get() = type.takeIf { wereChanges } val typeIfChanged: KotlinType? get() = type.takeIf { wereChanges }
} }
private class SimpleResult(override val type: SimpleType, subtreeSize: Int, wereChanges: Boolean) : Result(type, subtreeSize, wereChanges) private class SimpleResult(override val type: SimpleType, subtreeSize: Int, wereChanges: Boolean) : Result(type, subtreeSize, wereChanges)
// The index in the lambda is the position of the type component:
// Example: for `A<B, C<D, E>>`, indices go as follows: `0 - A<...>, 1 - B, 2 - C<D, E>, 3 - D, 4 - E`,
// which corresponds to the left-to-right breadth-first walk of the tree representation of the type.
// For flexible types, both bounds are indexed in the same way: `(A<B>..C<D>)` gives `0 - (A<B>..C<D>), 1 - B and D`.
fun KotlinType.enhance(qualifiers: (Int) -> JavaTypeQualifiers) = unwrap().enhancePossiblyFlexible(qualifiers, 0).typeIfChanged
private fun UnwrappedType.enhancePossiblyFlexible(qualifiers: (Int) -> JavaTypeQualifiers, index: Int): Result { private fun UnwrappedType.enhancePossiblyFlexible(qualifiers: (Int) -> JavaTypeQualifiers, index: Int): Result {
if (isError) return Result(this, 1, false) if (isError) return Result(this, 1, false)
return when (this) { return when (this) {
@@ -148,6 +150,7 @@ private fun SimpleType.enhanceInflexible(
return SimpleResult(result as SimpleType, subtreeSize, wereChanges = true) return SimpleResult(result as SimpleType, subtreeSize, wereChanges = true)
} }
}
private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size) { private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size) {
0 -> error("At least one Annotations object expected") 0 -> error("At least one Annotations object expected")