Parcelable: Support Android Extensions experimental flag
This commit is contained in:
@@ -85,8 +85,8 @@
|
||||
<platformGradleDetector implementation="org.jetbrains.kotlin.android.configure.PlatformAndroidGradleDetector"/>
|
||||
<completionInformationProvider implementation="org.jetbrains.kotlin.AndroidExtensionsCompletionInformationProvider" />
|
||||
|
||||
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.parcel.ParcelableCodegenExtension"/>
|
||||
<syntheticResolveExtension implementation="org.jetbrains.kotlin.android.parcel.ParcelableResolveExtension"/>
|
||||
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.parcel.IDEParcelableCodegenExtension"/>
|
||||
<syntheticResolveExtension implementation="org.jetbrains.kotlin.android.parcel.IDEParcelableResolveExtension"/>
|
||||
<classBuilderFactoryInterceptorExtension implementation="org.jetbrains.kotlin.android.synthetic.codegen.ParcelableClinitClassBuilderInterceptorExtension"/>
|
||||
|
||||
<androidDexer implementation="org.jetbrains.kotlin.android.debugger.AndroidDexerImpl"/>
|
||||
|
||||
+9
-1
@@ -39,6 +39,8 @@ import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
@@ -50,7 +52,7 @@ import org.jetbrains.org.objectweb.asm.Opcodes.*
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import java.io.FileDescriptor
|
||||
|
||||
class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
open class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
private companion object {
|
||||
private val FILE_DESCRIPTOR_FQNAME = FqName(FileDescriptor::class.java.canonicalName)
|
||||
private val PARCELER_FQNAME = FqName(Parceler::class.java.canonicalName)
|
||||
@@ -58,9 +60,15 @@ class ParcelableCodegenExtension : ExpressionCodegenExtension {
|
||||
fun KotlinType.isParceler() = constructor.declarationDescriptor?.fqNameSafe == PARCELER_FQNAME
|
||||
}
|
||||
|
||||
protected open fun isExperimental(element: KtElement) = true
|
||||
|
||||
override fun generateClassSyntheticParts(codegen: ImplementationBodyCodegen) {
|
||||
val parcelableClass = codegen.descriptor
|
||||
if (!parcelableClass.isMagicParcelable) return
|
||||
|
||||
val sourceElement = (codegen.myClass as? KtClassOrObject) ?: return
|
||||
if (!isExperimental(sourceElement)) return
|
||||
|
||||
assert(parcelableClass.kind == ClassKind.CLASS || parcelableClass.kind == ClassKind.OBJECT)
|
||||
|
||||
val propertiesToSerialize = getPropertiesToSerialize(codegen, parcelableClass)
|
||||
|
||||
+20
-3
@@ -26,14 +26,16 @@ import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.SimpleType
|
||||
|
||||
class ParcelableResolveExtension : SyntheticResolveExtension {
|
||||
open class ParcelableResolveExtension : SyntheticResolveExtension {
|
||||
companion object {
|
||||
fun resolveParcelClassType(module: ModuleDescriptor): SimpleType {
|
||||
return module.findClassAcrossModuleDependencies(
|
||||
@@ -72,6 +74,8 @@ class ParcelableResolveExtension : SyntheticResolveExtension {
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun isExperimental(element: KtElement) = true
|
||||
|
||||
override fun getSyntheticCompanionObjectNameIfNeeded(thisDescriptor: ClassDescriptor) = null
|
||||
|
||||
override fun generateSyntheticMethods(
|
||||
@@ -80,9 +84,22 @@ class ParcelableResolveExtension : SyntheticResolveExtension {
|
||||
fromSupertypes: List<SimpleFunctionDescriptor>,
|
||||
result: MutableCollection<SimpleFunctionDescriptor>
|
||||
) {
|
||||
if (name.asString() == DESCRIBE_CONTENTS.methodName && clazz.isMagicParcelable && result.none { it.isDescribeContents() }) {
|
||||
fun isExperimental(): Boolean {
|
||||
val sourceElement = (clazz.source as? PsiSourceElement)?.psi as? KtElement ?: return false
|
||||
return isExperimental(sourceElement)
|
||||
}
|
||||
|
||||
if (name.asString() == DESCRIBE_CONTENTS.methodName
|
||||
&& clazz.isMagicParcelable
|
||||
&& isExperimental()
|
||||
&& result.none { it.isDescribeContents() }
|
||||
) {
|
||||
result += createMethod(clazz, DESCRIBE_CONTENTS, clazz.builtIns.intType)
|
||||
} else if (name.asString() == WRITE_TO_PARCEL.methodName && clazz.isMagicParcelable && result.none { it.isWriteToParcel() }) {
|
||||
} else if (name.asString() == WRITE_TO_PARCEL.methodName
|
||||
&& clazz.isMagicParcelable
|
||||
&& isExperimental()
|
||||
&& result.none { it.isWriteToParcel() }
|
||||
) {
|
||||
val builtIns = clazz.builtIns
|
||||
val parcelClassType = resolveParcelClassType(clazz.module)
|
||||
result += createMethod(clazz, WRITE_TO_PARCEL, builtIns.unitType, "parcel" to parcelClassType, "flags" to builtIns.intType)
|
||||
|
||||
+4
-2
@@ -90,12 +90,14 @@ class AndroidComponentRegistrar : ComponentRegistrar {
|
||||
}
|
||||
|
||||
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
|
||||
registerParcelExtensions(project)
|
||||
|
||||
val applicationPackage = configuration.get(AndroidConfigurationKeys.PACKAGE)
|
||||
val variants = configuration.get(AndroidConfigurationKeys.VARIANT)?.mapNotNull { parseVariant(it) } ?: emptyList()
|
||||
val isExperimental = configuration.get(AndroidConfigurationKeys.EXPERIMENTAL) == "true"
|
||||
|
||||
if (isExperimental) {
|
||||
registerParcelExtensions(project)
|
||||
}
|
||||
|
||||
if (variants.isNotEmpty() && !applicationPackage.isNullOrBlank()) {
|
||||
val layoutXmlFileManager = CliAndroidLayoutXmlFileManager(project, applicationPackage!!, variants)
|
||||
project.registerService(AndroidLayoutXmlFileManager::class.java, layoutXmlFileManager)
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.android.parcel
|
||||
|
||||
import org.jetbrains.kotlin.android.synthetic.idea.androidExtensionsIsExperimental
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
class IDEParcelableCodegenExtension : ParcelableCodegenExtension() {
|
||||
override fun isExperimental(element: KtElement): Boolean {
|
||||
val moduleInfo = element.getModuleInfo()
|
||||
return moduleInfo.androidExtensionsIsExperimental
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.android.parcel
|
||||
|
||||
import org.jetbrains.kotlin.android.synthetic.idea.androidExtensionsIsExperimental
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getModuleInfo
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
class IDEParcelableResolveExtension : ParcelableResolveExtension() {
|
||||
override fun isExperimental(element: KtElement): Boolean {
|
||||
val moduleInfo = element.getModuleInfo()
|
||||
return moduleInfo.androidExtensionsIsExperimental
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user