Move UL compiler plugin support to separate extension point
This commit is contained in:
+2
-40
@@ -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> {
|
||||
|
||||
+58
@@ -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
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityCheckExtension
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.LightClass
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.UltraLightClass
|
||||
|
||||
class IDEParcelableApplicabilityExtension : LightClassApplicabilityCheckExtension {
|
||||
|
||||
override fun checkApplicabilityType(declaration: KtDeclaration, descriptor: Lazy<DeclarationDescriptor?>): LightClassApplicabilityType {
|
||||
|
||||
return UltraLightClass
|
||||
// if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return UltraLightClass
|
||||
//
|
||||
// val descriptorValue = descriptor.value ?: return UltraLightClass
|
||||
//
|
||||
// val classDescriptor = (descriptorValue as? ClassDescriptor)
|
||||
// ?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
// ?: return UltraLightClass
|
||||
//
|
||||
// return if (classDescriptor.isParcelize) LightClass else UltraLightClass
|
||||
}
|
||||
}
|
||||
+1
-88
@@ -5,100 +5,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.android.parcel
|
||||
|
||||
import com.intellij.psi.impl.light.LightFieldBuilder
|
||||
import org.jetbrains.kotlin.android.synthetic.idea.androidExtensionsIsExperimental
|
||||
import org.jetbrains.kotlin.asJava.UltraLightClassCodegenSupport
|
||||
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
|
||||
import org.jetbrains.kotlin.asJava.classes.createGeneratedMethodFromDescriptor
|
||||
import org.jetbrains.kotlin.asJava.elements.*
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.project.getModuleInfo
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
|
||||
class IDEParcelableCodegenExtension : ParcelableCodegenExtension(), UltraLightClassCodegenSupport {
|
||||
class IDEParcelableCodegenExtension : ParcelableCodegenExtension() {
|
||||
override fun isExperimental(element: KtElement): Boolean {
|
||||
val moduleInfo = element.getModuleInfo()
|
||||
return moduleInfo.androidExtensionsIsExperimental
|
||||
}
|
||||
|
||||
private fun tryGetParcelableClass(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>
|
||||
): ClassDescriptor? {
|
||||
|
||||
if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return null
|
||||
|
||||
val descriptorValue = descriptor.value ?: return null
|
||||
|
||||
val parcelableClass = (descriptorValue as? ClassDescriptor)
|
||||
?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
?: return null
|
||||
|
||||
if (!parcelableClass.isParcelableClassDescriptor) return null
|
||||
|
||||
return parcelableClass
|
||||
}
|
||||
|
||||
override fun interceptFieldsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
fieldsList: MutableList<KtLightField>
|
||||
) {
|
||||
|
||||
val parcelableClass = tryGetParcelableClass(
|
||||
declaration = declaration,
|
||||
descriptor = descriptor
|
||||
) ?: return
|
||||
|
||||
if (parcelableClass.hasCreatorField()) return
|
||||
|
||||
val fieldWrapper = KtLightFieldImpl.KtLightFieldForSourceDeclaration(
|
||||
origin = null,
|
||||
computeDelegate = {
|
||||
LightFieldBuilder("CREATOR", "android.os.Parcelable.Creator", containingDeclaration).also {
|
||||
it.setModifiers("public", "static", "final")
|
||||
}
|
||||
},
|
||||
containingClass = containingDeclaration,
|
||||
dummyDelegate = null
|
||||
)
|
||||
|
||||
fieldsList.add(fieldWrapper)
|
||||
}
|
||||
|
||||
override fun interceptMethodsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
methodsList: MutableList<KtLightMethod>
|
||||
) {
|
||||
|
||||
val parcelableClass = tryGetParcelableClass(
|
||||
declaration = declaration,
|
||||
descriptor = descriptor
|
||||
) ?: return
|
||||
|
||||
with(parcelableClass) {
|
||||
if (hasSyntheticDescribeContents()) {
|
||||
findFunction(ParcelableSyntheticComponent.ComponentKind.DESCRIBE_CONTENTS)?.let {
|
||||
methodsList.add(
|
||||
containingDeclaration.createGeneratedMethodFromDescriptor(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (hasSyntheticWriteToParcel()) {
|
||||
findFunction(ParcelableSyntheticComponent.ComponentKind.WRITE_TO_PARCEL)?.let {
|
||||
methodsList.add(
|
||||
containingDeclaration.createGeneratedMethodFromDescriptor(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import com.intellij.psi.impl.light.LightFieldBuilder
|
||||
import org.jetbrains.kotlin.android.parcel.serializers.ParcelableExtensionBase
|
||||
import org.jetbrains.kotlin.asJava.UltraLightClassModifierExtension
|
||||
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
|
||||
import org.jetbrains.kotlin.asJava.classes.createGeneratedMethodFromDescriptor
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightField
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightFieldImpl
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
|
||||
class ParcelableUltraLightClassModifierExtension : ParcelableExtensionBase, UltraLightClassModifierExtension {
|
||||
|
||||
private fun tryGetParcelableClass(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>
|
||||
): ClassDescriptor? {
|
||||
|
||||
if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return null
|
||||
|
||||
val descriptorValue = descriptor.value ?: return null
|
||||
|
||||
val parcelableClass = (descriptorValue as? ClassDescriptor)
|
||||
?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
?: return null
|
||||
|
||||
if (!parcelableClass.isParcelableClassDescriptor) return null
|
||||
|
||||
return parcelableClass
|
||||
}
|
||||
|
||||
override fun interceptFieldsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
fieldsList: MutableList<KtLightField>
|
||||
) {
|
||||
|
||||
val parcelableClass = tryGetParcelableClass(
|
||||
declaration = declaration,
|
||||
descriptor = descriptor
|
||||
) ?: return
|
||||
|
||||
if (parcelableClass.hasCreatorField()) return
|
||||
|
||||
val fieldWrapper = KtLightFieldImpl.KtLightFieldForSourceDeclaration(
|
||||
origin = null,
|
||||
computeDelegate = {
|
||||
LightFieldBuilder("CREATOR", "android.os.Parcelable.Creator", containingDeclaration).also {
|
||||
it.setModifiers("public", "static", "final")
|
||||
}
|
||||
},
|
||||
containingClass = containingDeclaration,
|
||||
dummyDelegate = null
|
||||
)
|
||||
|
||||
fieldsList.add(fieldWrapper)
|
||||
}
|
||||
|
||||
override fun interceptMethodsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
methodsList: MutableList<KtLightMethod>
|
||||
) {
|
||||
|
||||
val parcelableClass = tryGetParcelableClass(
|
||||
declaration = declaration,
|
||||
descriptor = descriptor
|
||||
) ?: return
|
||||
|
||||
with(parcelableClass) {
|
||||
if (hasSyntheticDescribeContents()) {
|
||||
findFunction(ParcelableSyntheticComponent.ComponentKind.DESCRIBE_CONTENTS)?.let {
|
||||
methodsList.add(
|
||||
containingDeclaration.createGeneratedMethodFromDescriptor(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (hasSyntheticWriteToParcel()) {
|
||||
findFunction(ParcelableSyntheticComponent.ComponentKind.WRITE_TO_PARCEL)?.let {
|
||||
methodsList.add(
|
||||
containingDeclaration.createGeneratedMethodFromDescriptor(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user