Move UL compiler plugin support to separate extension point

This commit is contained in:
Igor Yakovlev
2019-10-09 19:53:19 +03:00
parent 6ce37fff95
commit 3f9bffcc5f
23 changed files with 304 additions and 400 deletions
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.android.parcel
import kotlinx.android.parcel.TypeParceler
import org.jetbrains.kotlin.android.parcel.ParcelableResolveExtension.Companion.createMethod
import org.jetbrains.kotlin.android.parcel.ParcelableSyntheticComponent.*
import org.jetbrains.kotlin.android.parcel.serializers.*
import org.jetbrains.kotlin.android.parcel.ParcelableSyntheticComponent.ComponentKind.*
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
@@ -44,13 +43,10 @@ import org.jetbrains.org.objectweb.asm.Opcodes.*
import org.jetbrains.org.objectweb.asm.Type
import java.io.FileDescriptor
open class ParcelableCodegenExtension : ExpressionCodegenExtension {
open class ParcelableCodegenExtension : ParcelableExtensionBase, ExpressionCodegenExtension {
private companion object {
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)
}
@Deprecated(
@@ -60,8 +56,6 @@ 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
@@ -99,32 +93,6 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
}
}
protected fun ClassDescriptor.hasCreatorField(): Boolean {
val companionObject = companionObjectDescriptor ?: return false
if (companionObject.name == CREATOR_NAME) {
return true
}
return companionObject.unsubstitutedMemberScope
.getContributedVariables(CREATOR_NAME, NoLookupLocation.FROM_BACKEND)
.isNotEmpty()
}
protected fun ClassDescriptor.hasSyntheticDescribeContents() = hasParcelizeSyntheticMethod(ComponentKind.DESCRIBE_CONTENTS)
protected fun ClassDescriptor.hasSyntheticWriteToParcel() = hasParcelizeSyntheticMethod(ComponentKind.WRITE_TO_PARCEL)
protected fun ClassDescriptor.hasParcelizeSyntheticMethod(componentKind: ParcelableSyntheticComponent.ComponentKind): Boolean {
val methodName = Name.identifier(componentKind.methodName)
val writeToParcelMethods = unsubstitutedMemberScope
.getContributedFunctions(methodName, NoLookupLocation.FROM_BACKEND)
.filter { it is ParcelableSyntheticComponent && it.componentKind == componentKind }
return writeToParcelMethods.size == 1
}
private fun getCompanionClassType(containerAsmType: Type, parcelerObject: ClassDescriptor): Pair<Type, String> {
val shortName = parcelerObject.name
return Pair(Type.getObjectType(containerAsmType.internalName + "\$$shortName"), shortName.asString())
@@ -411,12 +379,6 @@ open class ParcelableCodegenExtension : ExpressionCodegenExtension {
}
})
}
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 }
}
}
internal fun getTypeParcelers(annotations: Annotations): List<TypeParcelerMapping> {
@@ -0,0 +1,58 @@
/*
* 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.serializers
import org.jetbrains.kotlin.android.parcel.ParcelableSyntheticComponent
import org.jetbrains.kotlin.android.parcel.isParcelize
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.android.parcel.ParcelableSyntheticComponent.ComponentKind.*
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
interface ParcelableExtensionBase {
companion object {
private val CREATOR_NAME = Name.identifier("CREATOR")
private val ALLOWED_CLASS_KINDS = listOf(ClassKind.CLASS, ClassKind.OBJECT, ClassKind.ENUM_CLASS)
}
fun ClassDescriptor.hasCreatorField(): Boolean {
val companionObject = companionObjectDescriptor ?: return false
if (companionObject.name == CREATOR_NAME) {
return true
}
return companionObject.unsubstitutedMemberScope
.getContributedVariables(CREATOR_NAME, NoLookupLocation.FROM_BACKEND)
.isNotEmpty()
}
val ClassDescriptor.isParcelableClassDescriptor get() = kind in ALLOWED_CLASS_KINDS && isParcelize
fun ClassDescriptor.hasSyntheticDescribeContents() = hasParcelizeSyntheticMethod(DESCRIBE_CONTENTS)
fun ClassDescriptor.hasSyntheticWriteToParcel() = hasParcelizeSyntheticMethod(WRITE_TO_PARCEL)
fun ClassDescriptor.findFunction(componentKind: ParcelableSyntheticComponent.ComponentKind): SimpleFunctionDescriptor? {
return unsubstitutedMemberScope
.getContributedFunctions(Name.identifier(componentKind.methodName), NoLookupLocation.WHEN_GET_ALL_DESCRIPTORS)
.firstOrNull { (it as? ParcelableSyntheticComponent)?.componentKind == componentKind }
}
private fun ClassDescriptor.hasParcelizeSyntheticMethod(componentKind: ParcelableSyntheticComponent.ComponentKind): Boolean {
val methodName = Name.identifier(componentKind.methodName)
val writeToParcelMethods = unsubstitutedMemberScope
.getContributedFunctions(methodName, NoLookupLocation.FROM_BACKEND)
.filter { it is ParcelableSyntheticComponent && it.componentKind == componentKind }
return writeToParcelMethods.size == 1
}
}