Map intrinsic companion objects in JavaToKotlinClassMap

Get rid of multiple usages of IntrinsicObjects where JavaToKotlinClassMap was
already used, simplify code, and support loading of *CompanionObject as Kotlin
built-in companions from Java code.

Also fix a small bug where Boolean was considered a class with an intrinsic
companion in IntrinsicObjects, although it was not
This commit is contained in:
Alexander Udalov
2015-04-23 19:33:37 +03:00
parent 987206bf95
commit d2cd7b00bb
12 changed files with 62 additions and 111 deletions
@@ -1,41 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.jvm.IntrinsicObjects
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.name.FqName
object InverseIntrinsicObjectsMapping {
private val map = linkedMapOf<FqName, ClassDescriptor>()
init {
for (descriptor in KotlinBuiltIns.getInstance().getBuiltInsPackageFragment().getMemberScope().getAllDescriptors()) {
val companion = (descriptor as? ClassDescriptor)?.getCompanionObjectDescriptor() ?: continue
IntrinsicObjects.mapType(companion)?.let { fqName ->
map[fqName] = companion
}
}
}
/**
* Maps a FQ name of an internal JVM class representing a built-in companion object to the relevant Kotlin descriptor,
* e.g. [kotlin.jvm.internal.StringCompanionObject] -> class descriptor of [kotlin.String.Companion]
*/
fun mapJvmClassToKotlinDescriptor(javaFqName: FqName): ClassDescriptor? = map[javaFqName]
}
@@ -18,7 +18,6 @@ package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.builtins.jvm.IntrinsicObjects
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.load.java.structure.reflect.classId
@@ -60,12 +59,6 @@ object RuntimeTypeMapper {
JavaToKotlinClassMap.INSTANCE.mapKotlinToJava(fqName)?.let { return it.desc }
if (classDescriptor.isCompanionObject()) {
IntrinsicObjects.mapType(classDescriptor)?.let { fqName ->
return ClassId.topLevel(fqName).desc
}
}
return classDescriptor.classId.desc
}
@@ -83,11 +76,7 @@ object RuntimeTypeMapper {
val classId = klass.classId
if (!classId.isLocal()) {
val fqName = classId.asSingleFqName()
val kotlinClass = JavaToKotlinClassMap.INSTANCE.mapJavaToKotlin(fqName)
kotlinClass?.let { return it.classId }
InverseIntrinsicObjectsMapping.mapJvmClassToKotlinDescriptor(fqName)?.let { return it.classId }
JavaToKotlinClassMap.INSTANCE.mapJavaToKotlin(classId.asSingleFqName())?.let { return it.classId }
}
return classId