Add CodegenApplicabilityCheckerExtension and use it to fallback to Heavy LigthClasses
+ Fixed #KT-33584
This commit is contained in:
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* 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.noarg
|
||||
@@ -49,18 +38,19 @@ class NoArgExpressionCodegenExtension(val invokeInitializers: Boolean = false) :
|
||||
// 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 { it.isZeroParameterConstructor() }
|
||||
|
||||
functionCodegen.generateMethod(JvmDeclarationOrigin.NO_ORIGIN, constructorDescriptor, object: CodegenBased(state) {
|
||||
functionCodegen.generateMethod(JvmDeclarationOrigin.NO_ORIGIN, constructorDescriptor, object : CodegenBased(state) {
|
||||
override fun doGenerateBody(codegen: ExpressionCodegen, signature: JvmMethodSignature) {
|
||||
codegen.v.load(0, AsmTypes.OBJECT_TYPE)
|
||||
|
||||
if (isParentASealedClassWithDefaultConstructor) {
|
||||
codegen.v.aconst(null)
|
||||
codegen.v.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassInternalName, "<init>",
|
||||
"(Lkotlin/jvm/internal/DefaultConstructorMarker;)V", false)
|
||||
}
|
||||
else {
|
||||
codegen.v.visitMethodInsn(
|
||||
Opcodes.INVOKESPECIAL, superClassInternalName, "<init>",
|
||||
"(Lkotlin/jvm/internal/DefaultConstructorMarker;)V", false
|
||||
)
|
||||
} else {
|
||||
codegen.v.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassInternalName, "<init>", "()V", false)
|
||||
}
|
||||
|
||||
@@ -74,8 +64,10 @@ class NoArgExpressionCodegenExtension(val invokeInitializers: Boolean = false) :
|
||||
|
||||
private 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)
|
||||
initialize(
|
||||
null, calculateDispatchReceiverParameter(), emptyList(), emptyList(),
|
||||
containingClass.builtIns.unitType, Modality.OPEN, Visibilities.PUBLIC
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,4 +88,6 @@ class NoArgExpressionCodegenExtension(val invokeInitializers: Boolean = false) :
|
||||
return parameters.isEmpty() ||
|
||||
(parameters.all { it.declaresDefaultValue() } && (isPrimary || findJvmOverloadsAnnotation() != null))
|
||||
}
|
||||
|
||||
override val shouldGenerateClassSyntheticPartsInLightClassesMode = true
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ plugins {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testRuntime(project(":kotlin-reflect"))
|
||||
compile(project(":kotlin-noarg-compiler-plugin"))
|
||||
compile(project(":compiler:util"))
|
||||
compile(project(":compiler:frontend"))
|
||||
@@ -19,11 +20,41 @@ dependencies {
|
||||
compileOnly(intellijDep())
|
||||
excludeInAndroidStudio(rootProject) { compileOnly(intellijPluginDep("maven")) }
|
||||
compileOnly(intellijPluginDep("gradle"))
|
||||
|
||||
testCompileOnly(project(":kotlin-serialization"))
|
||||
testCompileOnly(project(":plugins:lint"))
|
||||
testCompileOnly(project(":plugins:kapt3-idea"))
|
||||
testCompileOnly(project(":plugins:android-extensions-compiler"))
|
||||
testCompileOnly(project(":kotlin-android-extensions"))
|
||||
testCompileOnly(project(":kotlin-android-extensions-runtime"))
|
||||
testCompileOnly(project(":plugins:android-extensions-ide"))
|
||||
testCompileOnly(project(":kotlin-allopen-compiler-plugin"))
|
||||
testCompileOnly(project(":allopen-ide-plugin"))
|
||||
testCompileOnly(project(":kotlin-imports-dumper-compiler-plugin"))
|
||||
testCompileOnly(project(":kotlin-source-sections-compiler-plugin"))
|
||||
testCompileOnly(project(":kotlinx-serialization-compiler-plugin"))
|
||||
testCompileOnly(project(":kotlinx-serialization-ide-plugin"))
|
||||
testCompileOnly(project(":kotlin-sam-with-receiver-compiler-plugin"))
|
||||
testCompileOnly(project(":sam-with-receiver-ide-plugin"))
|
||||
testCompileOnly(project(":idea:idea-native"))
|
||||
testCompileOnly(project(":idea:idea-gradle-native"))
|
||||
testCompileOnly(projectTests(":idea:idea-test-framework"))
|
||||
testCompileOnly(intellijDep())
|
||||
testRuntimeOnly(intellijDep())
|
||||
|
||||
Platform[192].orHigher {
|
||||
testCompileOnly(intellijPluginDep("java"))
|
||||
testRuntimeOnly(intellijPluginDep("java"))
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
"main" { projectDefault() }
|
||||
"test" {}
|
||||
"test" { projectDefault() }
|
||||
}
|
||||
|
||||
runtimeJar()
|
||||
|
||||
projectTest(parallel = true) {
|
||||
|
||||
}
|
||||
|
||||
@@ -16,41 +16,24 @@
|
||||
|
||||
package org.jetbrains.kotlin.noarg.ide
|
||||
|
||||
import com.intellij.openapi.module.Module
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.ProjectRootModificationTracker
|
||||
import com.intellij.psi.util.CachedValue
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import com.intellij.util.containers.ContainerUtil
|
||||
import org.jetbrains.kotlin.annotation.plugin.ide.getSpecialAnnotations
|
||||
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.ANNOTATION_OPTION
|
||||
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.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.noarg.NoArgCommandLineProcessor
|
||||
import org.jetbrains.kotlin.noarg.diagnostic.AbstractNoArgDeclarationChecker
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import java.util.concurrent.ConcurrentMap
|
||||
|
||||
class IdeNoArgDeclarationChecker(val project: Project) : AbstractNoArgDeclarationChecker() {
|
||||
private companion object {
|
||||
val ANNOTATION_OPTION_PREFIX = "plugin:$PLUGIN_ID:${ANNOTATION_OPTION.optionName}="
|
||||
}
|
||||
internal val NO_ARG_ANNOTATION_OPTION_PREFIX =
|
||||
"plugin:${NoArgCommandLineProcessor.PLUGIN_ID}:${NoArgCommandLineProcessor.ANNOTATION_OPTION.optionName}="
|
||||
|
||||
private val cache: CachedValue<ConcurrentMap<Module, List<String>>> = cachedValue(project) {
|
||||
CachedValueProvider.Result.create(
|
||||
ContainerUtil.createConcurrentWeakMap<Module, List<String>>(),
|
||||
ProjectRootModificationTracker.getInstance(project)
|
||||
)
|
||||
}
|
||||
class IdeNoArgApplicabilityExtension(project: Project) :
|
||||
AnnotationBasedLightClassApplicabilityExtension(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> {
|
||||
if (modifierListOwner == null) return emptyList()
|
||||
val module = ModuleUtilCore.findModuleForPsiElement(modifierListOwner) ?: return emptyList()
|
||||
class IdeNoArgDeclarationChecker(project: Project) : AbstractNoArgDeclarationChecker() {
|
||||
|
||||
return cache.value.getOrPut(module) { module.getSpecialAnnotations(ANNOTATION_OPTION_PREFIX) }
|
||||
}
|
||||
private val cachedAnnotationNames = CachedAnnotationNames(project, NO_ARG_ANNOTATION_OPTION_PREFIX)
|
||||
|
||||
private fun <T> cachedValue(project: Project, result: () -> CachedValueProvider.Result<T>): CachedValue<T> {
|
||||
return CachedValuesManager.getManager(project).createCachedValue(result, false)
|
||||
}
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> =
|
||||
cachedAnnotationNames.getAnnotationNames(modifierListOwner)
|
||||
}
|
||||
@@ -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.noarg
|
||||
|
||||
import com.intellij.testFramework.LightProjectDescriptor
|
||||
import org.jetbrains.kotlin.config.LanguageVersion
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithAllCompilerChecks
|
||||
import org.jetbrains.kotlin.idea.facet.KotlinFacet
|
||||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.kotlin.idea.test.KotlinProjectDescriptorWithFacet
|
||||
import org.jetbrains.kotlin.noarg.ide.NO_ARG_ANNOTATION_OPTION_PREFIX
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
private const val targetClassName = "TargetClassName"
|
||||
private const val noArgAnnotationName = "HelloNoArg"
|
||||
|
||||
@RunWith(JUnit3WithIdeaConfigurationRunner::class)
|
||||
class TestNoArgForLightClass : KotlinLightCodeInsightFixtureTestCase() {
|
||||
|
||||
override fun getProjectDescriptor(): LightProjectDescriptor =
|
||||
KotlinProjectDescriptorWithFacet(LanguageVersion.LATEST_STABLE, multiPlatform = false)
|
||||
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
|
||||
val facet = KotlinFacet.get(module) ?: error { "Facet not found" }
|
||||
val configurationArguments = facet.configuration.settings.compilerArguments ?: error { "CompilerArguments not found" }
|
||||
|
||||
configurationArguments.pluginClasspaths = arrayOf("SomeClasspath")
|
||||
configurationArguments.pluginOptions = arrayOf("$NO_ARG_ANNOTATION_OPTION_PREFIX$noArgAnnotationName")
|
||||
}
|
||||
|
||||
fun testNoArgAnnotation() {
|
||||
val file = myFixture.configureByText(
|
||||
"A.kt",
|
||||
"annotation class $noArgAnnotationName\n"
|
||||
+ "@$noArgAnnotationName class $targetClassName(val e: Int)"
|
||||
) as KtFile
|
||||
|
||||
//We have to call it because NoArg checker need to be executed and mark needed PSI for CodegenExtension
|
||||
file.analyzeWithAllCompilerChecks()
|
||||
|
||||
val classes = file.classes
|
||||
assertEquals(2, 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