Add UL support for ParcelableCodegenExtension compiler plugin

This commit is contained in:
Igor Yakovlev
2019-10-08 21:08:29 +03:00
parent 110a6700e4
commit 558a700f51
5 changed files with 150 additions and 54 deletions
@@ -1,20 +1,9 @@
/*
* Copyright 2010-2019 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.android.parcel
/*
* Copyright 2010-2017 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.
*/
import kotlinx.android.parcel.TypeParceler
import org.jetbrains.kotlin.android.parcel.ParcelableResolveExtension.Companion.createMethod
@@ -71,14 +60,16 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
)
protected open fun isExperimental(element: KtElement) = true
protected val ClassDescriptor.isParcelableClassDescriptor get() = kind in ALLOWED_CLASS_KINDS && isParcelize
override val shouldGenerateClassSyntheticPartsInLightClassesMode: Boolean
get() = true
override fun generateClassSyntheticParts(codegen: ImplementationBodyCodegen) {
val parcelableClass = codegen.descriptor
if (!parcelableClass.isParcelize) return
if (parcelableClass.kind !in ALLOWED_CLASS_KINDS) return
val parcelableClass = codegen.descriptor
if (!parcelableClass.isParcelableClassDescriptor) return
val propertiesToSerialize = getPropertiesToSerialize(codegen, parcelableClass)
@@ -108,7 +99,7 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
}
}
private fun ClassDescriptor.hasCreatorField(): Boolean {
protected fun ClassDescriptor.hasCreatorField(): Boolean {
val companionObject = companionObjectDescriptor ?: return false
if (companionObject.name == CREATOR_NAME) {
@@ -120,11 +111,11 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
.isNotEmpty()
}
private fun ClassDescriptor.hasSyntheticDescribeContents() = hasParcelizeSyntheticMethod(ComponentKind.DESCRIBE_CONTENTS)
protected fun ClassDescriptor.hasSyntheticDescribeContents() = hasParcelizeSyntheticMethod(ComponentKind.DESCRIBE_CONTENTS)
private fun ClassDescriptor.hasSyntheticWriteToParcel() = hasParcelizeSyntheticMethod(ComponentKind.WRITE_TO_PARCEL)
protected fun ClassDescriptor.hasSyntheticWriteToParcel() = hasParcelizeSyntheticMethod(ComponentKind.WRITE_TO_PARCEL)
private fun ClassDescriptor.hasParcelizeSyntheticMethod(componentKind: ParcelableSyntheticComponent.ComponentKind): Boolean {
protected fun ClassDescriptor.hasParcelizeSyntheticMethod(componentKind: ParcelableSyntheticComponent.ComponentKind): Boolean {
val methodName = Name.identifier(componentKind.methodName)
val writeToParcelMethods = unsubstitutedMemberScope
@@ -421,7 +412,7 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
})
}
private fun ClassDescriptor.findFunction(componentKind: ParcelableSyntheticComponent.ComponentKind): SimpleFunctionDescriptor? {
protected fun ClassDescriptor.findFunction(componentKind: ParcelableSyntheticComponent.ComponentKind): SimpleFunctionDescriptor? {
return unsubstitutedMemberScope
.getContributedFunctions(Name.identifier(componentKind.methodName), WHEN_GET_ALL_DESCRIPTORS)
.firstOrNull { (it as? ParcelableSyntheticComponent)?.componentKind == componentKind }