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.DelegatingClassBuilder
|
||||||
import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension
|
import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension
|
||||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
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.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||||
import org.jetbrains.org.objectweb.asm.*
|
import org.jetbrains.org.objectweb.asm.*
|
||||||
@@ -68,7 +68,7 @@ class ParcelableClinitClassBuilderInterceptorExtension : ClassBuilderInterceptor
|
|||||||
internal val delegateClassBuilder: ClassBuilder,
|
internal val delegateClassBuilder: ClassBuilder,
|
||||||
val bindingContext: BindingContext
|
val bindingContext: BindingContext
|
||||||
) : DelegatingClassBuilder() {
|
) : DelegatingClassBuilder() {
|
||||||
private var currentClass: KtClass? = null
|
private var currentClass: KtClassOrObject? = null
|
||||||
private var currentClassName: String? = null
|
private var currentClassName: String? = null
|
||||||
private var isClinitGenerated = false
|
private var isClinitGenerated = false
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ class ParcelableClinitClassBuilderInterceptorExtension : ClassBuilderInterceptor
|
|||||||
superName: String,
|
superName: String,
|
||||||
interfaces: Array<out String>
|
interfaces: Array<out String>
|
||||||
) {
|
) {
|
||||||
if (origin is KtClass) {
|
if (origin is KtClassOrObject) {
|
||||||
currentClass = origin
|
currentClass = origin
|
||||||
} else {
|
} else {
|
||||||
currentClass = null
|
currentClass = null
|
||||||
|
|||||||
+34
-14
@@ -60,6 +60,8 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
|||||||
private companion object {
|
private companion object {
|
||||||
private val FILE_DESCRIPTOR_FQNAME = FqName(FileDescriptor::class.java.canonicalName)
|
private val FILE_DESCRIPTOR_FQNAME = FqName(FileDescriptor::class.java.canonicalName)
|
||||||
private val CREATOR_NAME = Name.identifier("CREATOR")
|
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
|
protected open fun isExperimental(element: KtElement) = true
|
||||||
@@ -74,7 +76,7 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
|||||||
val sourceElement = (codegen.myClass as? KtClassOrObject) ?: return
|
val sourceElement = (codegen.myClass as? KtClassOrObject) ?: return
|
||||||
if (!isExperimental(sourceElement)) 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)
|
val propertiesToSerialize = getPropertiesToSerialize(codegen, parcelableClass)
|
||||||
|
|
||||||
@@ -163,15 +165,25 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
|||||||
|
|
||||||
val globalContext = ParcelSerializer.ParcelSerializerContext(codegen.typeMapper, containerAsmType, emptyList(), frameMap)
|
val globalContext = ParcelSerializer.ParcelSerializerContext(codegen.typeMapper, containerAsmType, emptyList(), frameMap)
|
||||||
|
|
||||||
for ((fieldName, type, parcelers) in properties) {
|
if (properties.isEmpty()) {
|
||||||
val asmType = codegen.typeMapper.mapType(type)
|
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(1, parcelAsmType)
|
||||||
v.load(0, containerAsmType)
|
v.load(0, containerAsmType)
|
||||||
v.getfield(containerAsmType.internalName, fieldName, asmType.descriptor)
|
|
||||||
|
|
||||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
|
||||||
serializer.writeValue(v)
|
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)
|
val globalContext = ParcelSerializer.ParcelSerializerContext(codegen.typeMapper, containerAsmType, emptyList(), frameMap)
|
||||||
|
|
||||||
for ((_, type, parcelers) in properties) {
|
if (properties.isEmpty()) {
|
||||||
val asmType = codegen.typeMapper.mapType(type)
|
val entityType = parcelableClass.defaultType
|
||||||
asmConstructorParameters.append(asmType.descriptor)
|
val asmType = codegen.state.typeMapper.mapType(entityType)
|
||||||
|
val serializer = ParcelSerializer.get(entityType, asmType, globalContext, strict = true)
|
||||||
val serializer = ParcelSerializer.get(type, asmType, globalContext.copy(typeParcelers = parcelers))
|
|
||||||
v.load(1, parcelAsmType)
|
v.load(1, parcelAsmType)
|
||||||
serializer.readValue(v)
|
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)
|
v.areturn(containerAsmType)
|
||||||
|
|||||||
+5
-6
@@ -109,14 +109,13 @@ class ParcelableDeclarationChecker : DeclarationChecker {
|
|||||||
) {
|
) {
|
||||||
if (!descriptor.isParcelize) return
|
if (!descriptor.isParcelize) return
|
||||||
|
|
||||||
if (declaration !is KtClass || (declaration.isAnnotation() || declaration.isInterface())) {
|
if (declaration !is KtClassOrObject) {
|
||||||
val reportElement = (declaration as? KtClassOrObject)?.nameIdentifier ?: declaration
|
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(declaration), DefaultErrorMessagesAndroid)
|
||||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (declaration.isEnum()) {
|
if (declaration is KtClass && (declaration.isAnnotation() || declaration.isInterface())) {
|
||||||
val reportElement = (declaration as? KtClass)?.nameIdentifier ?: declaration
|
val reportElement = declaration.nameIdentifier ?: declaration
|
||||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -133,7 +132,7 @@ class ParcelableDeclarationChecker : DeclarationChecker {
|
|||||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_SHOULD_BE_INSTANTIABLE.on(sealedOrAbstract), DefaultErrorMessagesAndroid)
|
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
|
val reportElement = declaration.modifierList?.getModifier(KtTokens.INNER_KEYWORD) ?: declaration.nameIdentifier ?: declaration
|
||||||
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_CANT_BE_INNER_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
diagnosticHolder.reportFromPlugin(ErrorsAndroid.PARCELABLE_CANT_BE_INNER_CLASS.on(reportElement), DefaultErrorMessagesAndroid)
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -230,6 +230,12 @@ interface ParcelSerializer {
|
|||||||
Method("writeRawFileDescriptor"),
|
Method("writeRawFileDescriptor"),
|
||||||
Method("readRawFileDescriptor")))
|
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() -> {
|
type.isParcelable() -> {
|
||||||
val clazz = type.constructor.declarationDescriptor as? ClassDescriptor
|
val clazz = type.constructor.declarationDescriptor as? ClassDescriptor
|
||||||
if (clazz != null && clazz.modality == Modality.FINAL && clazz.source is PsiSourceElement) {
|
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,
|
type.isSerializable() -> NullCompliantObjectParcelSerializer(asmType,
|
||||||
Method("writeSerializable", "(Ljava/io/Serializable;)V"),
|
Method("writeSerializable", "(Ljava/io/Serializable;)V"),
|
||||||
Method("readSerializable", "()Ljava/io/Serializable;"))
|
Method("readSerializable", "()Ljava/io/Serializable;"))
|
||||||
|
|||||||
+1
@@ -54,6 +54,7 @@ class ParcelBoxTest : AbstractParcelBoxTest() {
|
|||||||
fun testCustomSerializerWriteWith() = doTest("customSerializerWriteWith")
|
fun testCustomSerializerWriteWith() = doTest("customSerializerWriteWith")
|
||||||
fun testCustomSerializerBoxing() = doTest("customSerializerBoxing")
|
fun testCustomSerializerBoxing() = doTest("customSerializerBoxing")
|
||||||
fun testKt20717() = doTest("kt20717")
|
fun testKt20717() = doTest("kt20717")
|
||||||
|
fun testEnumObject() = doTest("enumObject")
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParcelBoxTestWithSerializableLikeExtension : AbstractParcelBoxTest() {
|
class ParcelBoxTestWithSerializableLikeExtension : AbstractParcelBoxTest() {
|
||||||
|
|||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
@file:JvmName("TestKt")
|
||||||
|
package test
|
||||||
|
|
||||||
|
import kotlinx.android.parcel.*
|
||||||
|
import android.os.Parcel
|
||||||
|
import android.os.Parcelable
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
enum class Color : Parcelable { BLACK, WHITE }
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
object Obj : Parcelable
|
||||||
|
|
||||||
|
fun box() = parcelTest { parcel ->
|
||||||
|
val black = Color.BLACK
|
||||||
|
val obj = Obj
|
||||||
|
|
||||||
|
black.writeToParcel(parcel, 0)
|
||||||
|
obj.writeToParcel(parcel, 0)
|
||||||
|
|
||||||
|
println(black)
|
||||||
|
println(obj)
|
||||||
|
|
||||||
|
val bytes = parcel.marshall()
|
||||||
|
parcel.unmarshall(bytes, 0, bytes.size)
|
||||||
|
|
||||||
|
val black2 = readFromParcel<Color>(parcel)
|
||||||
|
val obj2 = readFromParcel<Obj>(parcel)
|
||||||
|
|
||||||
|
println(black2)
|
||||||
|
println(obj2)
|
||||||
|
|
||||||
|
assert(black2 == black)
|
||||||
|
assert(obj2 != null)
|
||||||
|
}
|
||||||
Vendored
+1
-1
@@ -24,7 +24,7 @@ class Outer {
|
|||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
@Parcelize
|
@Parcelize
|
||||||
<error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">object</error> : Parcelable {}
|
<error descr="[PLUGIN_ERROR] 'Parcelable' can't be a local class">object</error> : Parcelable {}
|
||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
class <error descr="[PLUGIN_ERROR] 'Parcelable' can't be a local class"><error descr="[PLUGIN_ERROR] No 'Parcelable' supertype">Local</error></error> {}
|
class <error descr="[PLUGIN_ERROR] 'Parcelable' can't be a local class"><error descr="[PLUGIN_ERROR] No 'Parcelable' supertype">Local</error></error> {}
|
||||||
|
|||||||
+3
-3
@@ -7,17 +7,17 @@ import android.os.Parcelable
|
|||||||
interface <error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">Intf</error> : Parcelable
|
interface <error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">Intf</error> : Parcelable
|
||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
object <error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">Obj</error>
|
object <error descr="[PLUGIN_ERROR] No 'Parcelable' supertype">Obj</error>
|
||||||
|
|
||||||
class A {
|
class A {
|
||||||
@Parcelize
|
@Parcelize
|
||||||
companion <error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">object</error> {
|
companion <error descr="[PLUGIN_ERROR] No 'Parcelable' supertype">object</error> {
|
||||||
fun foo() {}
|
fun foo() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
enum class <error descr="[PLUGIN_ERROR] 'Parcelable' should be a class">Enum</error> {
|
enum class <error descr="[PLUGIN_ERROR] No 'Parcelable' supertype">Enum</error> {
|
||||||
WHITE, BLACK
|
WHITE, BLACK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user