Add "java.io.Serializable" as supertype to java builtIns mapped to classes that are serializable on jvm
Implementation is hacky, relies on adding fictional supertype to corresponding classes
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement
|
||||
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.platform.JvmBuiltIns
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.AdditionalSupertypes
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
||||
import org.jetbrains.kotlin.types.DelegatingType
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
import java.io.Serializable
|
||||
|
||||
class BuiltInClassesAreSerializableOnJvm(
|
||||
private val moduleDescriptor: ModuleDescriptor
|
||||
) : AdditionalSupertypes {
|
||||
|
||||
private val mockSerializableType = createMockJavaIoSerializableType()
|
||||
|
||||
private fun createMockJavaIoSerializableType(): JetType {
|
||||
val mockJavaIoPackageFragment = object : PackageFragmentDescriptorImpl(moduleDescriptor, FqName("java.io")) {
|
||||
override fun getMemberScope() = JetScope.Empty
|
||||
}
|
||||
|
||||
//NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing
|
||||
val superTypes = listOf(object : DelegatingType() {
|
||||
override fun getDelegate(): JetType {
|
||||
return JvmBuiltIns.Instance.anyType
|
||||
}
|
||||
})
|
||||
|
||||
val mockSerializableClass = ClassDescriptorImpl(
|
||||
mockJavaIoPackageFragment, Name.identifier("Serializable"), Modality.ABSTRACT, superTypes, SourceElement.NO_SOURCE
|
||||
)
|
||||
|
||||
mockSerializableClass.initialize(JetScope.Empty, emptySet(), null)
|
||||
return mockSerializableClass.defaultType
|
||||
}
|
||||
|
||||
override fun forClass(classDescriptor: DeserializedClassDescriptor): Collection<JetType> {
|
||||
if (isSerializableInJava(classDescriptor.fqNameSafe)) {
|
||||
return listOf(mockSerializableType)
|
||||
}
|
||||
else return listOf()
|
||||
}
|
||||
|
||||
private fun isSerializableInJava(classFqName: FqName): Boolean {
|
||||
val fqNameUnsafe = classFqName.toUnsafe()
|
||||
if (fqNameUnsafe == KotlinBuiltIns.FQ_NAMES.array || KotlinBuiltIns.isPrimitiveArray(fqNameUnsafe)) {
|
||||
return true
|
||||
}
|
||||
val javaClassId = JavaToKotlinClassMap.INSTANCE.mapKotlinToJava(fqNameUnsafe) ?: return false
|
||||
val classViaReflection = try {
|
||||
Class.forName(javaClassId.asSingleFqName().asString())
|
||||
}
|
||||
catch (e: ClassNotFoundException) {
|
||||
return false
|
||||
}
|
||||
return Serializable::class.java.isAssignableFrom(classViaReflection)
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -39,9 +39,9 @@ public class DeserializationComponentsForJava(
|
||||
init {
|
||||
val localClassResolver = LocalClassResolverImpl()
|
||||
components = DeserializationComponents(
|
||||
storageManager, moduleDescriptor, classDataFinder, annotationAndConstantLoader, packageFragmentProvider,
|
||||
localClassResolver, errorReporter, JavaFlexibleTypeCapabilitiesDeserializer, ClassDescriptorFactory.EMPTY,
|
||||
JavaTypeCapabilitiesLoader
|
||||
storageManager, moduleDescriptor, classDataFinder, annotationAndConstantLoader, packageFragmentProvider, localClassResolver,
|
||||
errorReporter, JavaFlexibleTypeCapabilitiesDeserializer, ClassDescriptorFactory.EMPTY, JavaTypeCapabilitiesLoader,
|
||||
additionalSupertypes = BuiltInClassesAreSerializableOnJvm(moduleDescriptor)
|
||||
)
|
||||
localClassResolver.setDeserializationComponents(components)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin.platform
|
||||
|
||||
import org.jetbrains.kotlin.builtins.BuiltInsInitializer
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.load.kotlin.BuiltInClassesAreSerializableOnJvm
|
||||
import org.jetbrains.kotlin.serialization.deserialization.AdditionalSupertypes
|
||||
|
||||
class JvmBuiltIns private constructor() : KotlinBuiltIns() {
|
||||
companion object {
|
||||
@@ -29,4 +31,8 @@ class JvmBuiltIns private constructor() : KotlinBuiltIns() {
|
||||
val Instance: KotlinBuiltIns
|
||||
get() = initializer.get()
|
||||
}
|
||||
|
||||
override fun getAdditionalSupertypesProvider(): AdditionalSupertypes {
|
||||
return BuiltInClassesAreSerializableOnJvm(builtInsModule)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user