Extract FE 1.0 dependent part from JavaToKotlinClassMap

Also move JavaToKotlinClassMap to :core:compiler.common.jvm
This commit is contained in:
Dmitriy Novozhilov
2020-08-31 16:20:01 +03:00
parent 136435bcc2
commit 10fd164b77
22 changed files with 156 additions and 143 deletions
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.DECLARATION
@@ -360,7 +360,7 @@ private val READ_ONLY_ARE_EQUAL_TO_MUTABLE_TYPE_CHECKER = KotlinTypeCheckerImpl.
val firstClass = x.declarationDescriptor as? ClassDescriptor ?: return@withAxioms x == y
val secondClass = y.declarationDescriptor as? ClassDescriptor ?: return@withAxioms x == y
val j2k = JavaToKotlinClassMap
val j2k = JavaToKotlinClassMapper
val firstReadOnly = if (j2k.isMutable(firstClass)) j2k.convertMutableToReadOnly(firstClass) else firstClass
val secondReadOnly = if (j2k.isMutable(secondClass)) j2k.convertMutableToReadOnly(secondClass) else secondClass
firstReadOnly.typeConstructor == secondReadOnly.typeConstructor
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
@@ -42,7 +42,7 @@ class JvmBackendClassResolverForModuleWithDependencies(
val platformClass = moduleDescriptor.findClassAcrossModuleDependencies(type.classId) ?: return emptyList()
return JavaToKotlinClassMap.mapPlatformClass(platformClass) + platformClass
return JavaToKotlinClassMapper.mapPlatformClass(platformClass) + platformClass
}
}
@@ -54,4 +54,4 @@ val Type.classId: ClassId
val classRelativeNameWithDollars = if (lastDotIndex >= 0) className.substring(lastDotIndex + 1) else className
val classFQN = FqName(classRelativeNameWithDollars.replace('$', '.'))
return ClassId(packageFQN, classFQN, false)
}
}
@@ -5,12 +5,11 @@
package org.jetbrains.kotlin.resolve.jvm.platform
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.container.PlatformExtensionsClashResolver
import org.jetbrains.kotlin.container.StorageComponentContainer
import org.jetbrains.kotlin.container.useImpl
import org.jetbrains.kotlin.container.useInstance
import org.jetbrains.kotlin.resolve.sam.SamConversionResolver
import org.jetbrains.kotlin.load.java.sam.JvmSamConversionOracle
import org.jetbrains.kotlin.resolve.PlatformConfiguratorBase
import org.jetbrains.kotlin.resolve.checkers.BigFunctionTypeAvailabilityChecker
@@ -18,6 +17,7 @@ import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
import org.jetbrains.kotlin.resolve.jvm.*
import org.jetbrains.kotlin.resolve.jvm.checkers.*
import org.jetbrains.kotlin.resolve.jvm.multiplatform.JavaActualAnnotationArgumentExtractor
import org.jetbrains.kotlin.resolve.sam.SamConversionResolver
import org.jetbrains.kotlin.resolve.sam.SamConversionResolverImpl
import org.jetbrains.kotlin.synthetic.JavaSyntheticScopes
import org.jetbrains.kotlin.types.expressions.FunctionWithBigAritySupport
@@ -85,7 +85,7 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
overloadFilter = JvmOverloadFilter,
platformToKotlinClassMap = JavaToKotlinClassMap,
platformToKotlinClassMapper = JavaToKotlinClassMapper,
delegationFilter = JvmDelegationFilter,
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap;
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.psi.KtElement;
@@ -30,7 +30,7 @@ import static org.jetbrains.kotlin.diagnostics.Errors.PLATFORM_CLASS_MAPPED_TO_K
public class PlatformClassesMappedToKotlinChecker {
public static void checkPlatformClassesMappedToKotlin(
@NotNull PlatformToKotlinClassMap platformToKotlinMap,
@NotNull PlatformToKotlinClassMapper platformToKotlinMap,
@NotNull BindingTrace trace,
@NotNull KtImportDirective importDirective,
@NotNull Collection<? extends DeclarationDescriptor> descriptors
@@ -44,7 +44,7 @@ public class PlatformClassesMappedToKotlinChecker {
}
public static void reportPlatformClassMappedToKotlin(
@NotNull PlatformToKotlinClassMap platformToKotlinMap,
@NotNull PlatformToKotlinClassMapper platformToKotlinMap,
@NotNull BindingTrace trace,
@NotNull KtElement element,
@NotNull DeclarationDescriptor descriptor
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper
import org.jetbrains.kotlin.container.*
import org.jetbrains.kotlin.resolve.calls.checkers.*
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
@@ -95,7 +95,7 @@ abstract class PlatformConfiguratorBase(
private val additionalClashResolvers: List<PlatformExtensionsClashResolver<*>> = emptyList(),
private val identifierChecker: IdentifierChecker? = null,
private val overloadFilter: OverloadFilter? = null,
private val platformToKotlinClassMap: PlatformToKotlinClassMap? = null,
private val platformToKotlinClassMapper: PlatformToKotlinClassMapper? = null,
private val delegationFilter: DelegationFilter? = null,
private val overridesBackwardCompatibilityHelper: OverridesBackwardCompatibilityHelper? = null,
private val declarationReturnTypeSanitizer: DeclarationReturnTypeSanitizer? = null
@@ -120,7 +120,7 @@ abstract class PlatformConfiguratorBase(
additionalClashResolvers.forEach { useClashResolver(it) }
useInstanceIfNotNull(identifierChecker)
useInstanceIfNotNull(overloadFilter)
useInstanceIfNotNull(platformToKotlinClassMap)
useInstanceIfNotNull(platformToKotlinClassMapper)
useInstanceIfNotNull(delegationFilter)
useInstanceIfNotNull(overridesBackwardCompatibilityHelper)
useInstanceIfNotNull(declarationReturnTypeSanitizer)
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.resolve
import com.intellij.util.SmartList
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper
import org.jetbrains.kotlin.builtins.createFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
@@ -53,7 +53,6 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromMeAndParent
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.*
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
@@ -70,7 +69,7 @@ class TypeResolver(
private val dynamicTypesSettings: DynamicTypesSettings,
private val dynamicCallableDescriptors: DynamicCallableDescriptors,
private val identifierChecker: IdentifierChecker,
private val platformToKotlinClassMap: PlatformToKotlinClassMap,
private val platformToKotlinClassMapper: PlatformToKotlinClassMapper,
private val languageVersionSettings: LanguageVersionSettings
) {
private val isNonParenthesizedAnnotationsOnFunctionalTypesEnabled =
@@ -932,7 +931,7 @@ class TypeResolver(
return qualifiedExpressionResolver.resolveDescriptorForType(userType, scope, trace, isDebuggerContext).apply {
if (classifierDescriptor != null) {
PlatformClassesMappedToKotlinChecker.reportPlatformClassMappedToKotlin(
platformToKotlinClassMap, trace, userType, classifierDescriptor
platformToKotlinClassMapper, trace, userType, classifierDescriptor
)
}
}
@@ -20,7 +20,7 @@ import com.google.common.collect.HashMultimap
import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ListMultimap
import gnu.trove.THashSet
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
@@ -39,7 +39,6 @@ import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.NotNullLazyValue
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.expressions.OperatorConventions
@@ -90,7 +89,7 @@ class ImportResolutionComponents(
val storageManager: StorageManager,
val qualifiedExpressionResolver: QualifiedExpressionResolver,
val moduleDescriptor: ModuleDescriptor,
val platformToKotlinClassMap: PlatformToKotlinClassMap,
val platformToKotlinClassMapper: PlatformToKotlinClassMapper,
val languageVersionSettings: LanguageVersionSettings,
val deprecationResolver: DeprecationResolver
)
@@ -157,7 +156,7 @@ class LazyImportResolverForKtImportDirective(
if (scope is LazyExplicitImportScope) {
val allDescriptors = scope.storeReferencesToDescriptors()
PlatformClassesMappedToKotlinChecker.checkPlatformClassesMappedToKotlin(
components.platformToKotlinClassMap, traceForImportResolve, directive, allDescriptors
components.platformToKotlinClassMapper, traceForImportResolve, directive, allDescriptors
)
}
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.types
import com.google.common.collect.Maps
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.descriptors.ClassDescriptor
@@ -41,7 +41,7 @@ object CastDiagnosticsUtil {
fun isCastPossible(
lhsType: KotlinType,
rhsType: KotlinType,
platformToKotlinClassMap: PlatformToKotlinClassMap
platformToKotlinClassMapper: PlatformToKotlinClassMapper
): Boolean {
val rhsNullable = TypeUtils.isNullableType(rhsType)
val lhsNullable = TypeUtils.isNullableType(lhsType)
@@ -51,7 +51,7 @@ object CastDiagnosticsUtil {
if (KotlinBuiltIns.isNullableNothing(rhsType)) return lhsNullable
if (lhsNullable && rhsNullable) return true
if (lhsType.isError) return true
if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true
if (isRelated(lhsType, rhsType, platformToKotlinClassMapper)) return true
// This is an oversimplification (which does not render the method incomplete):
// we consider any type parameter capable of taking any value, which may be made more precise if we considered bounds
if (TypeUtils.isTypeParameter(lhsType) || TypeUtils.isTypeParameter(rhsType)) return true
@@ -70,20 +70,20 @@ object CastDiagnosticsUtil {
* Due to limitations in PlatformToKotlinClassMap, we only consider mapping of platform classes to Kotlin classed
* (i.e. java.lang.String -> kotlin.String) and ignore mappings that go the other way.
*/
private fun isRelated(a: KotlinType, b: KotlinType, platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean {
val aClasses = mapToPlatformIndependentClasses(a, platformToKotlinClassMap)
val bClasses = mapToPlatformIndependentClasses(b, platformToKotlinClassMap)
private fun isRelated(a: KotlinType, b: KotlinType, platformToKotlinClassMapper: PlatformToKotlinClassMapper): Boolean {
val aClasses = mapToPlatformIndependentClasses(a, platformToKotlinClassMapper)
val bClasses = mapToPlatformIndependentClasses(b, platformToKotlinClassMapper)
return aClasses.any { DescriptorUtils.isSubtypeOfClass(b, it) } || bClasses.any { DescriptorUtils.isSubtypeOfClass(a, it) }
}
private fun mapToPlatformIndependentClasses(
type: KotlinType,
platformToKotlinClassMap: PlatformToKotlinClassMap
platformToKotlinClassMapper: PlatformToKotlinClassMapper
): List<ClassDescriptor> {
val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return listOf()
return platformToKotlinClassMap.mapPlatformClass(descriptor) + descriptor
return platformToKotlinClassMapper.mapPlatformClass(descriptor) + descriptor
}
private fun isFinal(type: KotlinType) = !TypeUtils.canHaveSubtypes(KotlinTypeChecker.DEFAULT, type)
@@ -280,4 +280,4 @@ object CastDiagnosticsUtil {
else -> false
}
}
}
}
@@ -360,7 +360,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return;
}
if (!CastDiagnosticsUtil.isCastPossible(actualType, targetType, components.platformToKotlinClassMap)) {
if (!CastDiagnosticsUtil.isCastPossible(actualType, targetType, components.platformToKotlinClassMapper)) {
context.trace.report(CAST_NEVER_SUCCEEDS.on(expression.getOperationReference()));
return;
}
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.types.expressions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap;
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper;
import org.jetbrains.kotlin.config.LanguageVersionSettings;
import org.jetbrains.kotlin.context.GlobalContext;
import org.jetbrains.kotlin.contracts.EffectSystem;
@@ -33,7 +33,7 @@ public class ExpressionTypingComponents {
/*package*/ ModuleDescriptor moduleDescriptor;
/*package*/ ExpressionTypingServices expressionTypingServices;
/*package*/ CallResolver callResolver;
/*package*/ PlatformToKotlinClassMap platformToKotlinClassMap;
/*package*/ PlatformToKotlinClassMapper platformToKotlinClassMapper;
/*package*/ ControlStructureTypingUtils controlStructureTypingUtils;
/*package*/ ForLoopConventionsChecker forLoopConventionsChecker;
/*package*/ FakeCallResolver fakeCallResolver;
@@ -90,8 +90,8 @@ public class ExpressionTypingComponents {
}
@Inject
public void setPlatformToKotlinClassMap(@NotNull PlatformToKotlinClassMap platformToKotlinClassMap) {
this.platformToKotlinClassMap = platformToKotlinClassMap;
public void setPlatformToKotlinClassMap(@NotNull PlatformToKotlinClassMapper platformToKotlinClassMapper) {
this.platformToKotlinClassMapper = platformToKotlinClassMapper;
}
@Inject
@@ -1,42 +1,34 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.builtins.jvm
import org.jetbrains.kotlin.builtins.CompanionObjectMapping
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.StandardNames.FqNames
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.functions.BuiltInFunctionArity
import org.jetbrains.kotlin.builtins.functions.FunctionClassKind
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.name.*
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import java.util.*
object JavaToKotlinClassMap : PlatformToKotlinClassMap {
private val NUMBERED_FUNCTION_PREFIX =
object JavaToKotlinClassMap {
private val NUMBERED_FUNCTION_PREFIX: String =
FunctionClassKind.Function.packageFqName.toString() + "." + FunctionClassKind.Function.classNamePrefix
private val NUMBERED_K_FUNCTION_PREFIX =
private val NUMBERED_K_FUNCTION_PREFIX: String =
FunctionClassKind.KFunction.packageFqName.toString() + "." + FunctionClassKind.KFunction.classNamePrefix
private val NUMBERED_SUSPEND_FUNCTION_PREFIX =
private val NUMBERED_SUSPEND_FUNCTION_PREFIX: String =
FunctionClassKind.SuspendFunction.packageFqName.toString() + "." + FunctionClassKind.SuspendFunction.classNamePrefix
private val NUMBERED_K_SUSPEND_FUNCTION_PREFIX =
private val NUMBERED_K_SUSPEND_FUNCTION_PREFIX: String =
FunctionClassKind.KSuspendFunction.packageFqName.toString() + "." + FunctionClassKind.KSuspendFunction.classNamePrefix
private val FUNCTION_N_CLASS_ID = ClassId.topLevel(FqName("kotlin.jvm.functions.FunctionN"))
val FUNCTION_N_FQ_NAME = FUNCTION_N_CLASS_ID.asSingleFqName()
private val K_FUNCTION_CLASS_ID = ClassId.topLevel(FqName("kotlin.reflect.KFunction"))
private val K_CLASS_CLASS_ID = ClassId.topLevel(FqName("kotlin.reflect.KClass"))
private val CLASS_CLASS_ID = classId(java.lang.Class::class.java)
private val FUNCTION_N_CLASS_ID: ClassId = ClassId.topLevel(FqName("kotlin.jvm.functions.FunctionN"))
val FUNCTION_N_FQ_NAME: FqName = FUNCTION_N_CLASS_ID.asSingleFqName()
private val K_FUNCTION_CLASS_ID: ClassId = ClassId.topLevel(FqName("kotlin.reflect.KFunction"))
private val K_CLASS_CLASS_ID: ClassId = ClassId.topLevel(FqName("kotlin.reflect.KClass"))
private val CLASS_CLASS_ID: ClassId = classId(Class::class.java)
private val javaToKotlin = HashMap<FqNameUnsafe, ClassId>()
private val kotlinToJava = HashMap<FqNameUnsafe, ClassId>()
@@ -131,13 +123,6 @@ object JavaToKotlinClassMap : PlatformToKotlinClassMap {
return mapJavaToKotlin(fqName)
}
fun mapJavaToKotlin(fqName: FqName, builtIns: KotlinBuiltIns, functionTypeArity: Int? = null): ClassDescriptor? {
val kotlinClassId =
if (functionTypeArity != null && fqName == FUNCTION_N_FQ_NAME) StandardNames.getFunctionClassId(functionTypeArity)
else mapJavaToKotlin(fqName)
return if (kotlinClassId != null) builtIns.getBuiltInClassByFqName(kotlinClassId.asSingleFqName()) else null
}
/**
* E.g.
* kotlin.Throwable -> java.lang.Throwable
@@ -205,52 +190,14 @@ object JavaToKotlinClassMap : PlatformToKotlinClassMap {
fun isJavaPlatformClass(fqName: FqName): Boolean = mapJavaToKotlin(fqName) != null
fun mapPlatformClass(fqName: FqName, builtIns: KotlinBuiltIns): Collection<ClassDescriptor> {
val kotlinAnalog = mapJavaToKotlin(fqName, builtIns) ?: return emptySet()
val kotlinMutableAnalogFqName = readOnlyToMutable[kotlinAnalog.fqNameUnsafe] ?: return setOf(kotlinAnalog)
return listOf(kotlinAnalog, builtIns.getBuiltInClassByFqName(kotlinMutableAnalogFqName))
}
override fun mapPlatformClass(classDescriptor: ClassDescriptor): Collection<ClassDescriptor> {
val className = DescriptorUtils.getFqName(classDescriptor)
return if (className.isSafe)
mapPlatformClass(className.toSafe(), classDescriptor.builtIns)
else
emptySet()
}
fun mutableToReadOnly(fqNameUnsafe: FqNameUnsafe?): FqName? = mutableToReadOnly[fqNameUnsafe]
fun readOnlyToMutable(fqNameUnsafe: FqNameUnsafe?): FqName? = readOnlyToMutable[fqNameUnsafe]
fun isMutable(fqNameUnsafe: FqNameUnsafe?): Boolean = mutableToReadOnly.containsKey(fqNameUnsafe)
fun isMutable(mutable: ClassDescriptor): Boolean = isMutable(DescriptorUtils.getFqName(mutable))
fun isMutable(type: KotlinType): Boolean {
val classDescriptor = TypeUtils.getClassDescriptor(type)
return classDescriptor != null && isMutable(classDescriptor)
}
fun isReadOnly(fqNameUnsafe: FqNameUnsafe?): Boolean = readOnlyToMutable.containsKey(fqNameUnsafe)
fun isReadOnly(readOnly: ClassDescriptor): Boolean = isReadOnly(DescriptorUtils.getFqName(readOnly))
fun isReadOnly(type: KotlinType): Boolean {
val classDescriptor = TypeUtils.getClassDescriptor(type)
return classDescriptor != null && isReadOnly(classDescriptor)
}
fun convertMutableToReadOnly(mutable: ClassDescriptor): ClassDescriptor {
return convertToOppositeMutability(mutable, mutableToReadOnly, "mutable")
}
fun convertReadOnlyToMutable(readOnly: ClassDescriptor): ClassDescriptor {
return convertToOppositeMutability(readOnly, readOnlyToMutable, "read-only")
}
private fun classId(clazz: Class<*>): ClassId {
assert(!clazz.isPrimitive && !clazz.isArray) { "Invalid class: $clazz" }
val outer = clazz.declaringClass
@@ -259,14 +206,4 @@ object JavaToKotlinClassMap : PlatformToKotlinClassMap {
else
classId(outer).createNestedClassId(Name.identifier(clazz.simpleName))
}
private fun convertToOppositeMutability(
descriptor: ClassDescriptor,
map: Map<FqNameUnsafe, FqName>,
mutabilityKindName: String
): ClassDescriptor {
val oppositeClassFqName = map[DescriptorUtils.getFqName(descriptor)]
?: throw IllegalArgumentException("Given class $descriptor is not a $mutabilityKindName collection")
return descriptor.builtIns.getBuiltInClassByFqName(oppositeClassFqName)
}
}
@@ -0,0 +1,79 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.builtins.jvm
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMapper
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
object JavaToKotlinClassMapper : PlatformToKotlinClassMapper {
override fun mapPlatformClass(classDescriptor: ClassDescriptor): Collection<ClassDescriptor> {
val className = DescriptorUtils.getFqName(classDescriptor)
return if (className.isSafe)
mapPlatformClass(className.toSafe(), classDescriptor.builtIns)
else
emptySet()
}
fun mapPlatformClass(fqName: FqName, builtIns: KotlinBuiltIns): Collection<ClassDescriptor> {
val kotlinAnalog = mapJavaToKotlin(fqName, builtIns) ?: return emptySet()
val kotlinMutableAnalogFqName = JavaToKotlinClassMap.readOnlyToMutable(kotlinAnalog.fqNameUnsafe) ?: return setOf(kotlinAnalog)
return listOf(kotlinAnalog, builtIns.getBuiltInClassByFqName(kotlinMutableAnalogFqName))
}
fun mapJavaToKotlin(fqName: FqName, builtIns: KotlinBuiltIns, functionTypeArity: Int? = null): ClassDescriptor? {
val kotlinClassId =
if (functionTypeArity != null && fqName == JavaToKotlinClassMap.FUNCTION_N_FQ_NAME) StandardNames.getFunctionClassId(functionTypeArity)
else JavaToKotlinClassMap.mapJavaToKotlin(fqName)
return if (kotlinClassId != null) builtIns.getBuiltInClassByFqName(kotlinClassId.asSingleFqName()) else null
}
fun isMutable(mutable: ClassDescriptor): Boolean = JavaToKotlinClassMap.isMutable(DescriptorUtils.getFqName(mutable))
fun isMutable(type: KotlinType): Boolean {
val classDescriptor = TypeUtils.getClassDescriptor(type)
return classDescriptor != null && isMutable(classDescriptor)
}
fun isReadOnly(readOnly: ClassDescriptor): Boolean = JavaToKotlinClassMap.isReadOnly(DescriptorUtils.getFqName(readOnly))
fun isReadOnly(type: KotlinType): Boolean {
val classDescriptor = TypeUtils.getClassDescriptor(type)
return classDescriptor != null && isReadOnly(classDescriptor)
}
fun convertMutableToReadOnly(mutable: ClassDescriptor): ClassDescriptor {
return convertToOppositeMutability(mutable,"mutable") {
JavaToKotlinClassMap.mutableToReadOnly(it)
}
}
fun convertReadOnlyToMutable(readOnly: ClassDescriptor): ClassDescriptor {
return convertToOppositeMutability(readOnly, "read-only") {
JavaToKotlinClassMap.readOnlyToMutable(it)
}
}
private inline fun convertToOppositeMutability(
descriptor: ClassDescriptor,
mutabilityKindName: String,
oppositeNameExtractor: (FqNameUnsafe?) -> FqName?
): ClassDescriptor {
val oppositeClassFqName = oppositeNameExtractor(DescriptorUtils.getFqName(descriptor))
?: throw IllegalArgumentException("Given class $descriptor is not a $mutabilityKindName collection")
return descriptor.builtIns.getBuiltInClassByFqName(oppositeClassFqName)
}
}
@@ -47,7 +47,7 @@ open class JvmBuiltInsSettings(
deferredOwnerModuleDescriptor: () -> ModuleDescriptor,
isAdditionalBuiltInsFeatureSupported: () -> Boolean
) : AdditionalClassPartsProvider, PlatformDependentDeclarationFilter {
private val j2kClassMap = JavaToKotlinClassMap
private val j2kClassMapper = JavaToKotlinClassMapper
private val ownerModuleDescriptor: ModuleDescriptor by lazy(deferredOwnerModuleDescriptor)
private val isAdditionalBuiltInsFeatureSupported: Boolean by lazy(isAdditionalBuiltInsFeatureSupported)
@@ -164,11 +164,11 @@ open class JvmBuiltInsSettings(
): Collection<SimpleFunctionDescriptor> {
val javaAnalogueDescriptor = classDescriptor.getJavaAnalogue() ?: return emptyList()
val kotlinClassDescriptors = j2kClassMap.mapPlatformClass(javaAnalogueDescriptor.fqNameSafe, FallbackBuiltIns.Instance)
val kotlinClassDescriptors = j2kClassMapper.mapPlatformClass(javaAnalogueDescriptor.fqNameSafe, FallbackBuiltIns.Instance)
val kotlinMutableClassIfContainer = kotlinClassDescriptors.lastOrNull() ?: return emptyList()
val kotlinVersions = SmartSet.create(kotlinClassDescriptors.map { it.fqNameSafe })
val isMutable = j2kClassMap.isMutable(classDescriptor)
val isMutable = j2kClassMapper.isMutable(classDescriptor)
val fakeJavaClassDescriptor = javaAnalogueClassesWithCustomSupertypeCache.computeIfAbsent(javaAnalogueDescriptor.fqNameSafe) {
javaAnalogueDescriptor.copy(
@@ -214,7 +214,7 @@ open class JvmBuiltInsSettings(
{ it.original.overriddenDescriptors }
) { overridden ->
overridden.kind == CallableMemberDescriptor.Kind.DECLARATION &&
j2kClassMap.isMutable(overridden.containingDeclaration as ClassDescriptor)
j2kClassMapper.isMutable(overridden.containingDeclaration as ClassDescriptor)
}
}
@@ -262,7 +262,7 @@ open class JvmBuiltInsSettings(
val fqName = fqNameUnsafe
if (!fqName.isSafe) return null
val javaAnalogueFqName = j2kClassMap.mapKotlinToJava(fqName)?.asSingleFqName() ?: return null
val javaAnalogueFqName = JavaToKotlinClassMap.mapKotlinToJava(fqName)?.asSingleFqName() ?: return null
return ownerModuleDescriptor.resolveClassByFqName(javaAnalogueFqName, NoLookupLocation.FROM_BUILTINS) as? LazyJavaClassDescriptor
}
@@ -273,7 +273,7 @@ open class JvmBuiltInsSettings(
val javaAnalogueDescriptor = classDescriptor.getJavaAnalogue() ?: return emptyList()
val defaultKotlinVersion =
j2kClassMap.mapJavaToKotlin(javaAnalogueDescriptor.fqNameSafe, FallbackBuiltIns.Instance) ?: return emptyList()
j2kClassMapper.mapJavaToKotlin(javaAnalogueDescriptor.fqNameSafe, FallbackBuiltIns.Instance) ?: return emptyList()
val substitutor = createMappedTypeParametersSubstitution(defaultKotlinVersion, javaAnalogueDescriptor).buildSubstitutor()
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.load.java.lazy.descriptors
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.findNonGenericClassAcrossDependencies
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME
@@ -47,7 +47,7 @@ class LazyJavaAnnotationDescriptor(
override val type by c.storageManager.createLazyValue {
val fqName = fqName ?: return@createLazyValue ErrorUtils.createErrorType("No fqName: $javaAnnotation")
val annotationClass = JavaToKotlinClassMap.mapJavaToKotlin(fqName, c.module.builtIns)
val annotationClass = JavaToKotlinClassMapper.mapJavaToKotlin(fqName, c.module.builtIns)
?: javaAnnotation.resolve()?.let { javaClass -> c.components.moduleClassResolver.resolveClass(javaClass) }
?: createTypeForMissingDependencies(fqName)
annotationClass.defaultType
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.load.java.lazy.types
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.load.java.components.TypeUsage
@@ -156,7 +156,7 @@ class JavaTypeResolver(
return c.components.reflectionTypes.kClass
}
val javaToKotlin = JavaToKotlinClassMap
val javaToKotlin = JavaToKotlinClassMapper
val kotlinDescriptor = javaToKotlin.mapJavaToKotlin(fqName, c.module.builtIns) ?: return null
@@ -182,7 +182,7 @@ class JavaTypeResolver(
fun JavaType?.isSuperWildcard(): Boolean = (this as? JavaWildcardType)?.let { it.bound != null && !it.isExtends } ?: false
if (!typeArguments.lastOrNull().isSuperWildcard()) return false
val mutableLastParameterVariance = JavaToKotlinClassMap.convertReadOnlyToMutable(readOnlyContainer)
val mutableLastParameterVariance = JavaToKotlinClassMapper.convertReadOnlyToMutable(readOnlyContainer)
.typeConstructor.parameters.lastOrNull()?.variance ?: return false
return mutableLastParameterVariance != OUT_VARIANCE
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.load.java.typeEnhancement
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotated
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
@@ -257,7 +258,7 @@ class SignatureEnhancement(
asFlexibleType().let { Pair(it.lowerBound, it.upperBound) }
else Pair(this, this)
val mapping = JavaToKotlinClassMap
val mapper = JavaToKotlinClassMapper
return JavaTypeQualifiers(
when {
lower.isMarkedNullable -> NullabilityQualifier.NULLABLE
@@ -265,8 +266,8 @@ class SignatureEnhancement(
else -> null
},
when {
mapping.isReadOnly(lower) -> MutabilityQualifier.READ_ONLY
mapping.isMutable(upper) -> MutabilityQualifier.MUTABLE
mapper.isReadOnly(lower) -> MutabilityQualifier.READ_ONLY
mapper.isMutable(upper) -> MutabilityQualifier.MUTABLE
else -> null
},
isNotNullTypeParameter = unwrap() is NotNullTypeParameter
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.load.java.typeEnhancement
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.SourceElement
@@ -180,17 +180,17 @@ private fun ClassifierDescriptor.enhanceMutability(
if (!position.shouldEnhance()) return this.noChange()
if (this !is ClassDescriptor) return this.noChange() // mutability is not applicable for type parameters
val mapping = JavaToKotlinClassMap
val mapper = JavaToKotlinClassMapper
when (qualifiers.mutability) {
READ_ONLY -> {
if (position == TypeComponentPosition.FLEXIBLE_LOWER && mapping.isMutable(this)) {
return mapping.convertMutableToReadOnly(this).enhancedMutability()
if (position == TypeComponentPosition.FLEXIBLE_LOWER && mapper.isMutable(this)) {
return mapper.convertMutableToReadOnly(this).enhancedMutability()
}
}
MUTABLE -> {
if (position == TypeComponentPosition.FLEXIBLE_UPPER && mapping.isReadOnly(this)) {
return mapping.convertReadOnlyToMutable(this).enhancedMutability()
if (position == TypeComponentPosition.FLEXIBLE_UPPER && mapper.isReadOnly(this)) {
return mapper.convertReadOnlyToMutable(this).enhancedMutability()
}
}
}
@@ -12,12 +12,12 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import java.util.Collection;
import java.util.Collections;
@DefaultImplementation(impl = PlatformToKotlinClassMap.Default.class)
public interface PlatformToKotlinClassMap {
@DefaultImplementation(impl = PlatformToKotlinClassMapper.Default.class)
public interface PlatformToKotlinClassMapper {
@NotNull
Collection<ClassDescriptor> mapPlatformClass(@NotNull ClassDescriptor classDescriptor);
class Default implements PlatformToKotlinClassMap {
class Default implements PlatformToKotlinClassMapper {
@NotNull
@Override
public Collection<ClassDescriptor> mapPlatformClass(@NotNull ClassDescriptor classDescriptor) {
@@ -10,7 +10,7 @@ package org.jetbrains.kotlin.idea.util
import com.intellij.psi.*
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.builtins.replaceReturnType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
@@ -58,7 +58,7 @@ private fun KotlinType.approximateNonDynamicFlexibleTypes(
if (isFlexible()) {
val flexible = asFlexibleType()
val lowerClass = flexible.lowerBound.constructor.declarationDescriptor as? ClassDescriptor?
val isCollection = lowerClass != null && JavaToKotlinClassMap.isMutable(lowerClass)
val isCollection = lowerClass != null && JavaToKotlinClassMapper.isMutable(lowerClass)
// (Mutable)Collection<T>! -> MutableCollection<T>?
// Foo<(Mutable)Collection<T>!>! -> Foo<Collection<T>>?
// Foo! -> Foo?
@@ -258,4 +258,4 @@ private fun PsiType.collectTypeParameters(): List<PsiTypeParameter> {
}
)
return results
}
}
@@ -29,8 +29,6 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.idea.core.util.start
import org.jetbrains.kotlin.idea.core.util.end
import org.jetbrains.kotlin.idea.core.util.range
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.types.KotlinType
import java.util.*
@@ -21,7 +21,7 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.psi.*
import com.intellij.psi.search.PsiShortNamesCache
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
import org.jetbrains.kotlin.idea.KotlinBundle
@@ -164,7 +164,7 @@ class PlainTextPasteImportResolver(private val dataForConversion: DataForConvers
}
classes.find { (_, descriptor) ->
JavaToKotlinClassMap.mapPlatformClass(descriptor!!).isNotEmpty()
JavaToKotlinClassMapper.mapPlatformClass(descriptor!!).isNotEmpty()
}?.let { (psiClass, _) ->
performWriteAction { addImport(psiElementFactory.createImportStatement(psiClass)) }
}
@@ -236,4 +236,4 @@ class PlainTextPasteImportResolver(private val dataForConversion: DataForConvers
)
}
}
}
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMapper
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -54,7 +54,7 @@ object AssignOperatorAmbiguityFactory : KotlinIntentionActionsFactory() {
private fun KotlinType?.isMutableCollection(): Boolean {
if (this == null) return false
return JavaToKotlinClassMap.isMutable(this) || constructor.supertypes.reversed().any { JavaToKotlinClassMap.isMutable(it) }
return JavaToKotlinClassMapper.isMutable(this) || constructor.supertypes.reversed().any { JavaToKotlinClassMapper.isMutable(it) }
}
private class ReplaceWithAssignFunctionCallFix(