Move UL compiler plugin support to separate extension point
This commit is contained in:
-39
@@ -1,39 +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.extensions
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
|
||||
enum class LightClassApplicabilityType {
|
||||
LightClass,
|
||||
UltraLightClass
|
||||
}
|
||||
|
||||
/**
|
||||
* This extension point needs to be implemented by the Kotlin compiler plugins in case they change the requested declaration (i.e. producing synthetic parts)
|
||||
* with backend extension points. The light class provider will request this EP to check either to create LightClass or UltraLight class implementations.
|
||||
* @see org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
|
||||
* @see org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension
|
||||
*/
|
||||
interface LightClassApplicabilityCheckExtension {
|
||||
companion object : ProjectExtensionDescriptor<LightClassApplicabilityCheckExtension>(
|
||||
"org.jetbrains.kotlin.lightClassApplicabilityCheckExtension",
|
||||
LightClassApplicabilityCheckExtension::class.java
|
||||
)
|
||||
|
||||
/**
|
||||
* This method should return LightClass if any changes in Kotlin declarations is going to be produced during JVM-backend code generation.
|
||||
* This method ought to be as fast as possible but never return UltraLightClass when not sure.
|
||||
* Next advice could be given to make best:
|
||||
* 1) Try to cover as much cases as possible without forcing descriptor evaluation
|
||||
* 2) After you've forced descriptor evaluation, ideally, you should never return LightClass while in fact no synthetic parts going to be generated
|
||||
* 3) So, you should force descriptor evaluation only if you're sure that you will be able to return UltraLightClass significantly more often
|
||||
* @see org.jetbrains.kotlin.noarg.ide.IdeNoArgApplicabilityExtension
|
||||
* @see org.jetbrains.kotlin.android.parcel.IDEParcelableApplicabilityExtension
|
||||
*/
|
||||
fun checkApplicabilityType(declaration: KtDeclaration, descriptor: Lazy<DeclarationDescriptor?>): LightClassApplicabilityType
|
||||
}
|
||||
+9
-2
@@ -9,9 +9,16 @@ import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightField
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
|
||||
interface UltraLightClassCodegenSupport {
|
||||
interface UltraLightClassModifierExtension {
|
||||
|
||||
companion object : ProjectExtensionDescriptor<UltraLightClassModifierExtension>(
|
||||
"org.jetbrains.kotlin.ultraLightClassModifierExtension",
|
||||
UltraLightClassModifierExtension::class.java
|
||||
)
|
||||
|
||||
fun interceptMethodsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
@@ -23,6 +30,6 @@ interface UltraLightClassCodegenSupport {
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
methodsList: MutableList<KtLightField>
|
||||
fieldsList: MutableList<KtLightField>
|
||||
) = Unit
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import com.intellij.psi.impl.light.LightMethodBuilder
|
||||
import com.intellij.psi.util.CachedValue
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import org.jetbrains.kotlin.asJava.UltraLightClassCodegenSupport
|
||||
import org.jetbrains.kotlin.asJava.UltraLightClassModifierExtension
|
||||
import org.jetbrains.kotlin.asJava.builder.LightClassData
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightField
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||
@@ -340,10 +340,8 @@ open class KtUltraLightClass(classOrObject: KtClassOrObject, internal val suppor
|
||||
return result
|
||||
}
|
||||
|
||||
private inline fun applyCompilerPlugins(body: (UltraLightClassCodegenSupport) -> Unit) {
|
||||
ExpressionCodegenExtension.getInstances(project)
|
||||
.filterIsInstance<UltraLightClassCodegenSupport>()
|
||||
.forEach { body(it) }
|
||||
private inline fun applyCompilerPlugins(body: (UltraLightClassModifierExtension) -> Unit) {
|
||||
UltraLightClassModifierExtension.getInstances(project).forEach { body(it) }
|
||||
}
|
||||
|
||||
private val _ownMethods: CachedValue<List<KtLightMethod>> = CachedValuesManager.getManager(project).createCachedValue(
|
||||
|
||||
+1
-28
@@ -29,6 +29,7 @@ import com.intellij.psi.util.PsiModificationTracker
|
||||
import com.intellij.util.containers.ConcurrentFactoryMap
|
||||
import org.jetbrains.kotlin.asJava.LightClassBuilder
|
||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
||||
import org.jetbrains.kotlin.asJava.UltraLightClassModifierExtension
|
||||
import org.jetbrains.kotlin.asJava.builder.InvalidLightClassDataHolder
|
||||
import org.jetbrains.kotlin.asJava.builder.LightClassDataHolder
|
||||
import org.jetbrains.kotlin.asJava.classes.*
|
||||
@@ -40,8 +41,6 @@ import org.jetbrains.kotlin.config.JvmTarget
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityCheckExtension
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType
|
||||
import org.jetbrains.kotlin.idea.caches.lightClasses.IDELightClassContexts
|
||||
import org.jetbrains.kotlin.idea.caches.lightClasses.LazyLightClassDataHolder
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
@@ -69,33 +68,7 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
|
||||
override val isReleasedCoroutine
|
||||
get() = module?.languageVersionSettings?.supportsFeature(LanguageFeature.ReleaseCoroutines) ?: true
|
||||
|
||||
private fun KtDeclaration.mayBeModifiedByCompilerPlugins(): Boolean {
|
||||
|
||||
module?.let {
|
||||
val facet = KotlinFacet.get(it)
|
||||
val pluginClasspaths = facet?.configuration?.settings?.compilerArguments?.pluginClasspaths
|
||||
if (pluginClasspaths.isNullOrEmpty()) return false
|
||||
}
|
||||
|
||||
val resolvedDescriptor = lazy(LazyThreadSafetyMode.NONE) {
|
||||
resolveToDescriptorIfAny(
|
||||
getResolutionFacade(),
|
||||
bodyResolveMode = BodyResolveMode.PARTIAL
|
||||
)
|
||||
}
|
||||
|
||||
return LightClassApplicabilityCheckExtension.getInstances(project).any {
|
||||
it.checkApplicabilityType(this, resolvedDescriptor) == LightClassApplicabilityType.LightClass
|
||||
}
|
||||
}
|
||||
|
||||
override fun isTooComplexForUltraLightGeneration(element: KtDeclaration): Boolean {
|
||||
|
||||
val codegenExtensionsEnabled = element.mayBeModifiedByCompilerPlugins()
|
||||
if (codegenExtensionsEnabled) {
|
||||
LOG.debug { "Using heavy light classes because of compiler plugins" }
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
|
||||
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.parcel.IDEParcelableCodegenExtension"/>
|
||||
<syntheticResolveExtension implementation="org.jetbrains.kotlin.android.parcel.IDEParcelableResolveExtension"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlin.android.parcel.IDEParcelableApplicabilityExtension"/>
|
||||
<ultraLightClassModifierExtension implementation="org.jetbrains.kotlin.android.parcel.ParcelableUltraLightClassModifierExtension"/>
|
||||
<classBuilderFactoryInterceptorExtension implementation="org.jetbrains.kotlin.android.synthetic.codegen.ParcelableClinitClassBuilderInterceptorExtension"/>
|
||||
<quickFixContributor implementation="org.jetbrains.kotlin.android.parcel.quickfixes.ParcelableQuickFixContributor"/>
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.highlighterExtension"
|
||||
interface="org.jetbrains.kotlin.idea.highlighter.HighlighterExtension"/>
|
||||
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.lightClassApplicabilityCheckExtension"
|
||||
interface="org.jetbrains.kotlin.extensions.LightClassApplicabilityCheckExtension"
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.ultraLightClassModifierExtension"
|
||||
interface="org.jetbrains.kotlin.asJava.UltraLightClassModifierExtension"
|
||||
area="IDEA_PROJECT"/>
|
||||
|
||||
<extensionPoint name="scratchFileLanguageProvider" beanClass="com.intellij.lang.LanguageExtensionPoint">
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.highlighterExtension"
|
||||
interface="org.jetbrains.kotlin.idea.highlighter.HighlighterExtension"/>
|
||||
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.lightClassApplicabilityCheckExtension"
|
||||
interface="org.jetbrains.kotlin.extensions.LightClassApplicabilityCheckExtension"
|
||||
<extensionPoint qualifiedName="org.jetbrains.kotlin.ultraLightClassModifierExtension"
|
||||
interface="org.jetbrains.kotlin.asJava.UltraLightClassModifierExtension"
|
||||
area="IDEA_PROJECT"/>
|
||||
|
||||
<extensionPoint name="scratchFileLanguageProvider" beanClass="com.intellij.lang.LanguageExtensionPoint">
|
||||
|
||||
@@ -198,11 +198,10 @@
|
||||
<storageComponentContainerContributor implementation="org.jetbrains.kotlin.samWithReceiver.ide.IdeSamWithReceiverComponentContributor"/>
|
||||
|
||||
<declarationAttributeAltererExtension implementation="org.jetbrains.kotlin.allopen.ide.IdeAllOpenDeclarationAttributeAltererExtension"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlin.allopen.ide.IdeAllOpenApplicabilityExtension"/>
|
||||
|
||||
<storageComponentContainerContributor implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgComponentContainerContributor"/>
|
||||
<expressionCodegenExtension implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgExpressionCodegenExtension"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgApplicabilityExtension"/>
|
||||
<ultraLightClassModifierExtension implementation="org.jetbrains.kotlin.noarg.ide.NoArgUltraLightClassModifierExtension"/>
|
||||
<defaultErrorMessages implementation="org.jetbrains.kotlin.noarg.diagnostic.DefaultErrorMessagesNoArg"/>
|
||||
|
||||
<completionExtension implementation="org.jetbrains.kotlin.idea.properties.PropertyKeyCompletion"/>
|
||||
|
||||
@@ -198,11 +198,10 @@
|
||||
<storageComponentContainerContributor implementation="org.jetbrains.kotlin.samWithReceiver.ide.IdeSamWithReceiverComponentContributor"/>
|
||||
|
||||
<declarationAttributeAltererExtension implementation="org.jetbrains.kotlin.allopen.ide.IdeAllOpenDeclarationAttributeAltererExtension"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlin.allopen.ide.IdeAllOpenApplicabilityExtension"/>
|
||||
|
||||
<storageComponentContainerContributor implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgComponentContainerContributor"/>
|
||||
<expressionCodegenExtension implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgExpressionCodegenExtension"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlin.noarg.ide.IdeNoArgApplicabilityExtension"/>
|
||||
<ultraLightClassModifierExtension implementation="org.jetbrains.kotlin.noarg.ide.NoArgUltraLightClassModifierExtension"/>
|
||||
<defaultErrorMessages implementation="org.jetbrains.kotlin.noarg.diagnostic.DefaultErrorMessagesNoArg"/>
|
||||
|
||||
<completionExtension implementation="org.jetbrains.kotlin.idea.properties.PropertyKeyCompletion"/>
|
||||
|
||||
@@ -5,6 +5,5 @@
|
||||
<jsSyntheticTranslateExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationIDEJsExtension"/>
|
||||
<irGenerationExtension implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationIDEIrExtension"/>
|
||||
<storageComponentContainerContributor implementation="org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationIDEContainerContributor"/>
|
||||
<lightClassApplicabilityCheckExtension implementation="org.jetbrains.kotlinx.serialization.idea.IdeSerializationPluginApplicabilityExtension"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
|
||||
@@ -21,16 +21,12 @@ import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.allopen.AbstractAllOpenDeclarationAttributeAltererExtension
|
||||
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.ANNOTATION_OPTION
|
||||
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.PLUGIN_ID
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedLightClassApplicabilityExtension
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.CachedAnnotationNames
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getAnnotationNames
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
|
||||
internal val ALL_OPEN_ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.optionName}="
|
||||
|
||||
class IdeAllOpenApplicabilityExtension(project: Project) :
|
||||
AnnotationBasedLightClassApplicabilityExtension(project, ALL_OPEN_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
class IdeAllOpenDeclarationAttributeAltererExtension(val project: Project) :
|
||||
AbstractAllOpenDeclarationAttributeAltererExtension() {
|
||||
|
||||
|
||||
+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)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-48
@@ -1,48 +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.annotation.plugin.ide
|
||||
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.extensions.AnnotationBasedExtension
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityCheckExtension
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.LightClass
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.UltraLightClass
|
||||
|
||||
abstract class AnnotationBasedLightClassApplicabilityExtension(project: Project, annotationOptionPrefix: String) :
|
||||
LightClassApplicabilityCheckExtension,
|
||||
AnnotationBasedExtension
|
||||
{
|
||||
private val cachedAnnotationsNames = CachedAnnotationNames(project, annotationOptionPrefix)
|
||||
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> =
|
||||
cachedAnnotationsNames.getAnnotationNames(modifierListOwner)
|
||||
|
||||
override fun checkApplicabilityType(declaration: KtDeclaration, descriptor: Lazy<DeclarationDescriptor?>): LightClassApplicabilityType {
|
||||
|
||||
return UltraLightClass
|
||||
//
|
||||
// if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return UltraLightClass
|
||||
//
|
||||
// if (cachedAnnotationsNames.getAnnotationNames(declaration).isEmpty()) return UltraLightClass
|
||||
//
|
||||
// val descriptorValue = descriptor.value ?: return UltraLightClass
|
||||
//
|
||||
// val classDescriptor = (descriptorValue as? ClassDescriptor)
|
||||
// ?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
// ?: return UltraLightClass
|
||||
//
|
||||
// val hasSpecialAnnotation = run { classDescriptor.hasSpecialAnnotation(declaration) }
|
||||
//
|
||||
// return if (hasSpecialAnnotation) LightClass else UltraLightClass
|
||||
}
|
||||
}
|
||||
-31
@@ -1,31 +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.kotlinx.serialization.idea
|
||||
|
||||
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.kotlinx.serialization.compiler.resolve.SerializationAnnotations.serializableAnnotationFqName
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.LightClass
|
||||
import org.jetbrains.kotlin.extensions.LightClassApplicabilityType.UltraLightClass
|
||||
|
||||
class IdeSerializationPluginApplicabilityExtension : LightClassApplicabilityCheckExtension {
|
||||
override fun checkApplicabilityType(declaration: KtDeclaration, descriptor: Lazy<DeclarationDescriptor?>): LightClassApplicabilityType {
|
||||
|
||||
if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return UltraLightClass
|
||||
|
||||
return (descriptor.value as? ClassDescriptor)?.let {
|
||||
getIfEnabledOn(it) {
|
||||
if (it.annotations.hasAnnotation(serializableAnnotationFqName)) LightClass
|
||||
else UltraLightClass
|
||||
}
|
||||
} ?: UltraLightClass
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@ class CliNoArgExpressionCodegenExtension(private val annotations: List<String>,
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> = annotations
|
||||
}
|
||||
|
||||
abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: Boolean) : ExpressionCodegenExtension, AnnotationBasedExtension {
|
||||
abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: Boolean) : ExpressionCodegenExtension,
|
||||
AnnotationBasedExtension {
|
||||
|
||||
override fun generateClassSyntheticParts(codegen: ImplementationBodyCodegen) = with(codegen) {
|
||||
if (shouldGenerateNoArgConstructor()) {
|
||||
@@ -48,7 +49,7 @@ abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: B
|
||||
// If a parent sealed class has not a zero-parameter constructor, user must write @NoArg annotation for the parent class as well,
|
||||
// and then we generate <init>()V
|
||||
val isParentASealedClassWithDefaultConstructor =
|
||||
superClass.modality == Modality.SEALED && superClass.constructors.any { it.isZeroParameterConstructor() }
|
||||
superClass.modality == Modality.SEALED && superClass.constructors.any { isZeroParameterConstructor(it) }
|
||||
|
||||
functionCodegen.generateMethod(JvmDeclarationOrigin.NO_ORIGIN, constructorDescriptor, object : CodegenBased(state) {
|
||||
override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) {
|
||||
@@ -72,15 +73,6 @@ abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: B
|
||||
})
|
||||
}
|
||||
|
||||
protected fun createNoArgConstructorDescriptor(containingClass: ClassDescriptor): ConstructorDescriptor {
|
||||
return ClassConstructorDescriptorImpl.createSynthesized(containingClass, Annotations.EMPTY, false, SourceElement.NO_SOURCE).apply {
|
||||
initialize(
|
||||
null, calculateDispatchReceiverParameter(), emptyList(), emptyList(),
|
||||
containingClass.builtIns.unitType, Modality.OPEN, Visibilities.PUBLIC
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun ImplementationBodyCodegen.shouldGenerateNoArgConstructor(): Boolean {
|
||||
val origin = myClass as? KtClass ?: return false
|
||||
|
||||
@@ -88,14 +80,30 @@ abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: B
|
||||
return false
|
||||
}
|
||||
|
||||
return descriptor.constructors.none { it.isZeroParameterConstructor() }
|
||||
}
|
||||
|
||||
private fun ClassConstructorDescriptor.isZeroParameterConstructor(): Boolean {
|
||||
val parameters = this.valueParameters
|
||||
return parameters.isEmpty() ||
|
||||
(parameters.all { it.declaresDefaultValue() } && (isPrimary || findJvmOverloadsAnnotation() != null))
|
||||
return descriptor.constructors.none { isZeroParameterConstructor(it) }
|
||||
}
|
||||
|
||||
override val shouldGenerateClassSyntheticPartsInLightClassesMode = true
|
||||
|
||||
companion object {
|
||||
|
||||
fun isZeroParameterConstructor(constructor: ClassConstructorDescriptor): Boolean {
|
||||
val parameters = constructor.valueParameters
|
||||
return parameters.isEmpty() ||
|
||||
(parameters.all { it.declaresDefaultValue() } && (constructor.isPrimary || constructor.findJvmOverloadsAnnotation() != null))
|
||||
}
|
||||
|
||||
fun createNoArgConstructorDescriptor(containingClass: ClassDescriptor): ConstructorDescriptor =
|
||||
ClassConstructorDescriptorImpl.createSynthesized(containingClass, Annotations.EMPTY, false, SourceElement.NO_SOURCE).apply {
|
||||
initialize(
|
||||
null,
|
||||
calculateDispatchReceiverParameter(),
|
||||
emptyList(),
|
||||
emptyList(),
|
||||
containingClass.builtIns.unitType,
|
||||
Modality.OPEN,
|
||||
Visibilities.PUBLIC
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,49 +8,13 @@ package org.jetbrains.kotlin.noarg.ide
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.CachedAnnotationNames
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getAnnotationNames
|
||||
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.KtLightMethod
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.noarg.AbstractNoArgExpressionCodegenExtension
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
|
||||
class IdeNoArgExpressionCodegenExtension(project: Project) :
|
||||
AbstractNoArgExpressionCodegenExtension(invokeInitializers = false),
|
||||
UltraLightClassCodegenSupport {
|
||||
class IdeNoArgExpressionCodegenExtension(project: Project) : AbstractNoArgExpressionCodegenExtension(invokeInitializers = false) {
|
||||
|
||||
private val cachedAnnotationsNames = CachedAnnotationNames(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> =
|
||||
cachedAnnotationsNames.getAnnotationNames(modifierListOwner)
|
||||
|
||||
override fun interceptMethodsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
methodsList: MutableList<KtLightMethod>
|
||||
) {
|
||||
if (!declaration.isOrdinaryClass || !declaration.isAnnotated) return
|
||||
|
||||
if (cachedAnnotationsNames.getAnnotationNames(declaration).isEmpty()) return
|
||||
|
||||
val descriptorValue = descriptor.value ?: return
|
||||
|
||||
val classDescriptor = (descriptorValue as? ClassDescriptor)
|
||||
?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
?: return
|
||||
|
||||
if (!run { classDescriptor.hasSpecialAnnotation(declaration) }) return
|
||||
|
||||
val parentClass = containingDeclaration as? KtUltraLightClass ?: return
|
||||
|
||||
val constructorDescriptor = createNoArgConstructorDescriptor(classDescriptor)
|
||||
|
||||
methodsList.add(parentClass.createGeneratedMethodFromDescriptor(constructorDescriptor))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.noarg.ide
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.AnnotationBasedLightClassApplicabilityExtension
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.CachedAnnotationNames
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getAnnotationNames
|
||||
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor
|
||||
@@ -27,9 +15,6 @@ import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
internal val NO_ARG_ANNOTATION_OPTION_PREFIX =
|
||||
"plugin:${NoArgCommandLineProcessor.PLUGIN_ID}:${NoArgCommandLineProcessor.ANNOTATION_OPTION.optionName}="
|
||||
|
||||
class IdeNoArgApplicabilityExtension(project: Project) :
|
||||
AnnotationBasedLightClassApplicabilityExtension(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
class IdeNoArgDeclarationChecker(project: Project) : AbstractNoArgDeclarationChecker() {
|
||||
|
||||
private val cachedAnnotationNames = CachedAnnotationNames(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.noarg.ide
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.CachedAnnotationNames
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getAnnotationNames
|
||||
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.KtLightMethod
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.extensions.AnnotationBasedExtension
|
||||
import org.jetbrains.kotlin.noarg.AbstractNoArgExpressionCodegenExtension
|
||||
import org.jetbrains.kotlin.noarg.AbstractNoArgExpressionCodegenExtension.Companion.isZeroParameterConstructor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.util.isAnnotated
|
||||
import org.jetbrains.kotlin.util.isOrdinaryClass
|
||||
|
||||
class NoArgUltraLightClassModifierExtension(project: Project) :
|
||||
AnnotationBasedExtension,
|
||||
UltraLightClassModifierExtension {
|
||||
|
||||
private val cachedAnnotationsNames = CachedAnnotationNames(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> =
|
||||
cachedAnnotationsNames.getAnnotationNames(modifierListOwner)
|
||||
|
||||
private fun isSuitableDeclaration(declaration: KtDeclaration): Boolean {
|
||||
if (getAnnotationFqNames(declaration).isEmpty()) return false
|
||||
|
||||
if (!declaration.isOrdinaryClass || declaration !is KtClassOrObject) return false
|
||||
|
||||
if (declaration.allConstructors.isEmpty()) return false
|
||||
|
||||
if (declaration.allConstructors.any { it.getValueParameters().isEmpty() }) return false
|
||||
|
||||
if (declaration.superTypeListEntries.isEmpty() && !declaration.isAnnotated) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun interceptMethodsBuilding(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: Lazy<DeclarationDescriptor?>,
|
||||
containingDeclaration: KtUltraLightClass,
|
||||
methodsList: MutableList<KtLightMethod>
|
||||
) {
|
||||
val parentClass = containingDeclaration as? KtUltraLightClass ?: return
|
||||
|
||||
if (methodsList.any { it.isConstructor && it.parameters.isEmpty() }) return
|
||||
|
||||
if (!isSuitableDeclaration(declaration)) return
|
||||
|
||||
val descriptorValue = descriptor.value ?: return
|
||||
|
||||
val classDescriptor = (descriptorValue as? ClassDescriptor)
|
||||
?: descriptorValue.containingDeclaration as? ClassDescriptor
|
||||
?: return
|
||||
|
||||
if (!classDescriptor.hasSpecialAnnotation(declaration)) return
|
||||
|
||||
if (classDescriptor.constructors.any { isZeroParameterConstructor(it) }) return
|
||||
|
||||
val constructorDescriptor = AbstractNoArgExpressionCodegenExtension.createNoArgConstructorDescriptor(classDescriptor)
|
||||
|
||||
methodsList.add(parentClass.createGeneratedMethodFromDescriptor(constructorDescriptor))
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
private const val targetClassName = "TargetClassName"
|
||||
private const val baseClassName = "BaseClassName"
|
||||
private const val noArgAnnotationName = "HelloNoArg"
|
||||
|
||||
@RunWith(JUnit3WithIdeaConfigurationRunner::class)
|
||||
@@ -51,4 +52,23 @@ class TestNoArgForLightClass : KotlinLightCodeInsightFixtureTestCase() {
|
||||
assertEquals(constructors.size, 2)
|
||||
assertTrue(constructors.any { it.parameters.isEmpty() })
|
||||
}
|
||||
|
||||
fun testNoArgDerivedAnnotation() {
|
||||
val file = myFixture.configureByText(
|
||||
"A.kt",
|
||||
"annotation class $noArgAnnotationName\n"
|
||||
+ "@$noArgAnnotationName class $baseClassName(val e: Int)\n"
|
||||
+ "class $targetClassName(val k: Int) : $baseClassName(k)"
|
||||
) as KtFile
|
||||
|
||||
val classes = file.classes
|
||||
assertEquals(3, classes.size)
|
||||
|
||||
val targetClass = classes.firstOrNull { it.name == targetClassName }
|
||||
?: error { "Expected class $targetClassName not found" }
|
||||
|
||||
val constructors = targetClass.constructors
|
||||
assertEquals(constructors.size, 2)
|
||||
assertTrue(constructors.any { it.parameters.isEmpty() })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user