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
@@ -19,14 +19,15 @@ class IDEParcelableApplicabilityExtension : LightClassApplicabilityCheckExtensio
override fun checkApplicabilityType(declaration: KtDeclaration, descriptor: Lazy<DeclarationDescriptor?>): LightClassApplicabilityType {
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
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,28 +1,104 @@
/*
* 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.
* 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.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() {
class IDEParcelableCodegenExtension : ParcelableCodegenExtension(), UltraLightClassCodegenSupport {
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)
)
}
}
}
}
}