Move UL compiler plugin support to separate extension point
This commit is contained in:
@@ -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