Parcelize: Support objects and enums (#KT-22576)
This commit is contained in:
+3
-3
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.codegen.ClassBuilderFactory
|
||||
import org.jetbrains.kotlin.codegen.DelegatingClassBuilder
|
||||
import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||
import org.jetbrains.org.objectweb.asm.*
|
||||
@@ -68,7 +68,7 @@ class ParcelableClinitClassBuilderInterceptorExtension : ClassBuilderInterceptor
|
||||
internal val delegateClassBuilder: ClassBuilder,
|
||||
val bindingContext: BindingContext
|
||||
) : DelegatingClassBuilder() {
|
||||
private var currentClass: KtClass? = null
|
||||
private var currentClass: KtClassOrObject? = null
|
||||
private var currentClassName: String? = null
|
||||
private var isClinitGenerated = false
|
||||
|
||||
@@ -83,7 +83,7 @@ class ParcelableClinitClassBuilderInterceptorExtension : ClassBuilderInterceptor
|
||||
superName: String,
|
||||
interfaces: Array<out String>
|
||||
) {
|
||||
if (origin is KtClass) {
|
||||
if (origin is KtClassOrObject) {
|
||||
currentClass = origin
|
||||
} else {
|
||||
currentClass = null
|
||||
|
||||
+34
-14
@@ -60,6 +60,8 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
private companion object {
|
||||
private val FILE_DESCRIPTOR_FQNAME = FqName(FileDescriptor::class.java.canonicalName)
|
||||
private val CREATOR_NAME = Name.identifier("CREATOR")
|
||||
|
||||
private val ALLOWED_CLASS_KINDS = listOf(ClassKind.CLASS, ClassKind.OBJECT, ClassKind.ENUM_CLASS)
|
||||
}
|
||||
|
||||
protected open fun isExperimental(element: KtElement) = true
|
||||
@@ -74,7 +76,7 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
val sourceElement = (codegen.myClass as? KtClassOrObject) ?: return
|
||||
if (!isExperimental(sourceElement)) return
|
||||
|
||||
if (parcelableClass.kind != ClassKind.CLASS && parcelableClass.kind != ClassKind.OBJECT) return
|
||||
if (parcelableClass.kind !in ALLOWED_CLASS_KINDS) return
|
||||
|
||||
val propertiesToSerialize = getPropertiesToSerialize(codegen, parcelableClass)
|
||||
|
||||
@@ -163,15 +165,25 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
|
||||
val globalContext = ParcelSerializer.ParcelSerializerContext(codegen.typeMapper, containerAsmType, emptyList(), frameMap)
|
||||
|
||||
for ((fieldName, type, parcelers) in properties) {
|
||||
val asmType = codegen.typeMapper.mapType(type)
|
||||
|
||||
if (properties.isEmpty()) {
|
||||
val entityType = this@writeWriteToParcel.defaultType
|
||||
val asmType = codegen.state.typeMapper.mapType(entityType)
|
||||
val serializer = ParcelSerializer.get(entityType, asmType, globalContext, strict = true)
|
||||
v.load(1, parcelAsmType)
|
||||
v.load(0, containerAsmType)
|
||||
v.getfield(containerAsmType.internalName, fieldName, asmType.descriptor)
|
||||
|
||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
||||
serializer.writeValue(v)
|
||||
} else {
|
||||
for ((fieldName, type, parcelers) in properties) {
|
||||
val asmType = codegen.typeMapper.mapType(type)
|
||||
|
||||
v.load(1, parcelAsmType)
|
||||
v.load(0, containerAsmType)
|
||||
v.getfield(containerAsmType.internalName, fieldName, asmType.descriptor)
|
||||
|
||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
||||
serializer.writeValue(v)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,16 +265,24 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
|
||||
val globalContext = ParcelSerializer.ParcelSerializerContext(codegen.typeMapper, containerAsmType, emptyList(), frameMap)
|
||||
|
||||
for ((_, type, parcelers) in properties) {
|
||||
val asmType = codegen.typeMapper.mapType(type)
|
||||
asmConstructorParameters.append(asmType.descriptor)
|
||||
|
||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
||||
if (properties.isEmpty()) {
|
||||
val entityType = parcelableClass.defaultType
|
||||
val asmType = codegen.state.typeMapper.mapType(entityType)
|
||||
val serializer = ParcelSerializer.get(entityType, asmType, globalContext, strict = true)
|
||||
v.load(1, parcelAsmType)
|
||||
serializer.readValue(v)
|
||||
}
|
||||
} else {
|
||||
for ((_, type, parcelers) in properties) {
|
||||
val asmType = codegen.typeMapper.mapType(type)
|
||||
asmConstructorParameters.append(asmType.descriptor)
|
||||
|
||||
v.invokespecial(containerAsmType.internalName, "<init>", "($asmConstructorParameters)V", false)
|
||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
||||
v.load(1, parcelAsmType)
|
||||
serializer.readValue(v)
|
||||
}
|
||||
|
||||
v.invokespecial(containerAsmType.internalName, "<init>", "($asmConstructorParameters)V", false)
|
||||
}
|
||||
}
|
||||
|
||||
v.areturn(containerAsmType)
|
||||
|
||||
+5
-6
@@ -109,14 +109,13 @@ class ParcelableDeclarationChecker : DeclarationChecker {
|
||||
) {
|
||||
if (!descriptor.isParcelize) return
|
||||
|
||||
if (declaration !is KtClass || (declaration.isAnnotation() || declaration.isInterface())) {
|
||||
val reportElement = (declaration as? KtClassOrObject)?.nameIdentifier ?: declaration
|
||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
||||
if (declaration !is KtClassOrObject) {
|
||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(declaration), DefaultErrorMessagesAndroid)
|
||||
return
|
||||
}
|
||||
|
||||
if (declaration.isEnum()) {
|
||||
val reportElement = (declaration as? KtClass)?.nameIdentifier ?: declaration
|
||||
if (declaration is KtClass && (declaration.isAnnotation() || declaration.isInterface())) {
|
||||
val reportElement = declaration.nameIdentifier ?: declaration
|
||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
||||
return
|
||||
}
|
||||
@@ -133,7 +132,7 @@ class ParcelableDeclarationChecker : DeclarationChecker {
|
||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_INSTANTIABLE.on(sealedOrAbstract), DefaultErrorMessagesAndroid)
|
||||
}
|
||||
|
||||
if (declaration.isInner()) {
|
||||
if (declaration is KtClass && declaration.isInner()) {
|
||||
val reportElement = declaration.modifierList?.getModifier(KtTokens.INNER_KEYWORD) ?: declaration.nameIdentifier ?: declaration
|
||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_CANT_BE_INNER_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
||||
}
|
||||
|
||||
+6
-6
@@ -230,6 +230,12 @@ interface ParcelSerializer {
|
||||
Method("writeRawFileDescriptor"),
|
||||
Method("readRawFileDescriptor")))
|
||||
|
||||
// Write at least a nullability byte.
|
||||
// We don't want parcel to be empty in case if all constructor parameters are objects
|
||||
type.isNamedObject() -> NullAwareParcelSerializerWrapper(ObjectParcelSerializer(asmType, type, typeMapper))
|
||||
|
||||
type.isEnum() -> wrapToNullAwareIfNeeded(type, EnumParcelSerializer(asmType))
|
||||
|
||||
type.isParcelable() -> {
|
||||
val clazz = type.constructor.declarationDescriptor as? ClassDescriptor
|
||||
if (clazz != null && clazz.modality == Modality.FINAL && clazz.source is PsiSourceElement) {
|
||||
@@ -257,12 +263,6 @@ interface ParcelSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
// Write at least a nullability byte.
|
||||
// We don't want parcel to be empty in case if all constructor parameters are objects
|
||||
type.isNamedObject() -> NullAwareParcelSerializerWrapper(ObjectParcelSerializer(asmType, type, typeMapper))
|
||||
|
||||
type.isEnum() -> wrapToNullAwareIfNeeded(type, EnumParcelSerializer(asmType))
|
||||
|
||||
type.isSerializable() -> NullCompliantObjectParcelSerializer(asmType,
|
||||
Method("writeSerializable", "(Ljava/io/Serializable;)V"),
|
||||
Method("readSerializable", "()Ljava/io/Serializable;"))
|
||||
|
||||
Reference in New Issue
Block a user