From 69e70575f9b3ede56b8bc9aa3bbb6d401dff3cfa Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 28 Jun 2017 20:32:04 +0300 Subject: [PATCH] Drop some of the TypeUsage enum entries The only sensible usage of them was in the `isNullable` method But you can check that UPPER_BOUND/SUPERTYPE_ARGUMENT are always flexible and TYPE_ARGUMENT is not only in case of annotation methods/parameters --- .../load/java/components/TypeUsage.java | 5 +--- .../LazyJavaTypeParameterDescriptor.kt | 2 +- .../load/java/lazy/types/JavaTypeResolver.kt | 27 ++++++------------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/TypeUsage.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/TypeUsage.java index cac8bd962f5..d4d370b6c48 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/TypeUsage.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/TypeUsage.java @@ -21,12 +21,9 @@ package org.jetbrains.kotlin.load.java.components; * This enum encodes the kinds of occurrences */ public enum TypeUsage { - // Type T occurs somewhere as a generic argument, e.g.: List or List - TYPE_ARGUMENT, - UPPER_BOUND, MEMBER_SIGNATURE_COVARIANT, MEMBER_SIGNATURE_CONTRAVARIANT, MEMBER_SIGNATURE_INVARIANT, SUPERTYPE, - SUPERTYPE_ARGUMENT + COMMON } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaTypeParameterDescriptor.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaTypeParameterDescriptor.kt index 48972057ae7..1a003495ead 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaTypeParameterDescriptor.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaTypeParameterDescriptor.kt @@ -53,7 +53,7 @@ class LazyJavaTypeParameterDescriptor( )) } return bounds.map { - c.typeResolver.transformJavaType(it, TypeUsage.UPPER_BOUND.toAttributes(upperBoundForTypeParameter = this)) + c.typeResolver.transformJavaType(it, TypeUsage.COMMON.toAttributes(upperBoundForTypeParameter = this)) } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt index 3b7067bf5a0..dc968269a32 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/types/JavaTypeResolver.kt @@ -73,7 +73,7 @@ class JavaTypeResolver( } val componentType = transformJavaType(javaComponentType, - TYPE_ARGUMENT.toAttributes(attr.allowFlexible, attr.isForAnnotationParameter)) + COMMON.toAttributes(attr.allowFlexible, attr.isForAnnotationParameter)) if (attr.allowFlexible) { return@run KotlinTypeFactory.flexibleType( @@ -113,7 +113,7 @@ class JavaTypeResolver( val annotations = CompositeAnnotations(listOf(LazyJavaAnnotations(c, javaType), attr.typeAnnotations)) val constructor = computeTypeConstructor(javaType, attr) ?: return null val arguments = computeArguments(javaType, attr, constructor) - val isNullable = isNullable(javaType, attr) + val isNullable = attr.isNullable() return KotlinTypeFactory.simpleType(annotations, constructor, arguments, isNullable) } @@ -232,7 +232,6 @@ class JavaTypeResolver( // Most of the time this means there is an error in the Java code return typeParameters.map { p -> TypeProjectionImpl(ErrorUtils.createErrorType(p.name.asString())) }.toList() } - val howTheProjectionIsUsed = if (attr.howThisTypeIsUsed == SUPERTYPE) SUPERTYPE_ARGUMENT else TYPE_ARGUMENT return javaType.typeArguments.withIndex().map { indexedArgument -> val (i, javaTypeArgument) = indexedArgument @@ -242,7 +241,7 @@ class JavaTypeResolver( } val parameter = typeParameters[i] - transformToTypeProjection(javaTypeArgument, howTheProjectionIsUsed.toAttributes(), parameter) + transformToTypeProjection(javaTypeArgument, COMMON.toAttributes(), parameter) }.toList() } @@ -259,7 +258,7 @@ class JavaTypeResolver( makeStarProjection(typeParameter, attr) else { createProjection( - type = transformJavaType(bound, UPPER_BOUND.toAttributes()), + type = transformJavaType(bound, COMMON.toAttributes()), projectionKind = projectionKind, typeParameterDescriptor = typeParameter ) @@ -274,21 +273,11 @@ class JavaTypeResolver( return this != typeParameter.variance } - private fun isNullable(javaType: JavaClassifierType, attr: JavaTypeAttributes): Boolean { - if (attr.flexibility == FLEXIBLE_LOWER_BOUND) return false - if (attr.flexibility == FLEXIBLE_UPPER_BOUND) return true + private fun JavaTypeAttributes.isNullable(): Boolean { + if (flexibility == FLEXIBLE_LOWER_BOUND) return false + if (flexibility == FLEXIBLE_UPPER_BOUND) return true - return !attr.isMarkedNotNull && - // 'L extends List' in Java is a List in Kotlin, not a List - // nullability will be taken care of in individual member signatures - when (javaType.classifier) { - is JavaTypeParameter -> { - attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, UPPER_BOUND, SUPERTYPE_ARGUMENT, SUPERTYPE) - } - is JavaClass, - null -> attr.howThisTypeIsUsed !in setOf(TYPE_ARGUMENT, SUPERTYPE_ARGUMENT, SUPERTYPE) - else -> error("Unknown classifier: ${javaType.classifier}") - } + return !isMarkedNotNull && !isForAnnotationParameter && howThisTypeIsUsed != SUPERTYPE } }