Refactoring NoArg compiler plugin
Remove NoArgClassKey relation betwen AbstractNoArgDeclarationChecker and NoArgExpressionCodegenExtension Separate NoArgExpressionCodegenExtension to Ide and Cli versions
This commit is contained in:
+12
-4
@@ -12,7 +12,9 @@ import org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl
|
||||
import org.jetbrains.kotlin.extensions.AnnotationBasedExtension
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
|
||||
@@ -21,7 +23,15 @@ import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.org.objectweb.asm.Opcodes
|
||||
|
||||
class NoArgExpressionCodegenExtension(val invokeInitializers: Boolean = false) : ExpressionCodegenExtension {
|
||||
|
||||
class CliNoArgExpressionCodegenExtension(private val annotations: List<String>, invokeInitializers: Boolean = false) :
|
||||
AbstractNoArgExpressionCodegenExtension(invokeInitializers) {
|
||||
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List<String> = annotations
|
||||
}
|
||||
|
||||
abstract class AbstractNoArgExpressionCodegenExtension(val invokeInitializers: Boolean) : ExpressionCodegenExtension, AnnotationBasedExtension {
|
||||
|
||||
override fun generateClassSyntheticParts(codegen: ImplementationBodyCodegen) = with(codegen) {
|
||||
if (shouldGenerateNoArgConstructor()) {
|
||||
generateNoArgConstructor()
|
||||
@@ -71,12 +81,10 @@ class NoArgExpressionCodegenExtension(val invokeInitializers: Boolean = false) :
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtClass.isNoArgClass() = this.getUserData(NO_ARG_CLASS_KEY) ?: false
|
||||
|
||||
private fun ImplementationBodyCodegen.shouldGenerateNoArgConstructor(): Boolean {
|
||||
val origin = myClass as? KtClass ?: return false
|
||||
|
||||
if (descriptor.kind != ClassKind.CLASS || !origin.isNoArgClass()) {
|
||||
if (descriptor.kind != ClassKind.CLASS || !descriptor.hasSpecialAnnotation(origin)) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.noarg
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
|
||||
val NO_ARG_CLASS_KEY = Key<Boolean>("org.jetbrains.kotlin.noarg.NoArgClassKey")
|
||||
@@ -83,7 +83,7 @@ class NoArgComponentRegistrar : ComponentRegistrar {
|
||||
StorageComponentContainerContributor.registerExtension(project, CliNoArgComponentContainerContributor(annotations))
|
||||
|
||||
val invokeInitializers = configuration[INVOKE_INITIALIZERS] ?: false
|
||||
ExpressionCodegenExtension.registerExtension(project, NoArgExpressionCodegenExtension(invokeInitializers))
|
||||
ExpressionCodegenExtension.registerExtension(project, CliNoArgExpressionCodegenExtension(annotations, invokeInitializers))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-10
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.noarg.diagnostic
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.reportFromPlugin
|
||||
import org.jetbrains.kotlin.extensions.AnnotationBasedExtension
|
||||
import org.jetbrains.kotlin.noarg.NO_ARG_CLASS_KEY
|
||||
import org.jetbrains.kotlin.psi.KtClass
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
@@ -27,7 +26,7 @@ import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
||||
|
||||
class CliNoArgDeclarationChecker(val noArgAnnotationFqNames: List<String>) : AbstractNoArgDeclarationChecker() {
|
||||
class CliNoArgDeclarationChecker(private val noArgAnnotationFqNames: List<String>) : AbstractNoArgDeclarationChecker() {
|
||||
override fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?) = noArgAnnotationFqNames
|
||||
}
|
||||
|
||||
@@ -36,15 +35,12 @@ abstract class AbstractNoArgDeclarationChecker : DeclarationChecker, AnnotationB
|
||||
// Handle only classes
|
||||
if (descriptor !is ClassDescriptor || declaration !is KtClass) return
|
||||
if (descriptor.kind != ClassKind.CLASS) return
|
||||
if (!descriptor.hasSpecialAnnotation(declaration)) return
|
||||
|
||||
val hasSpecialAnnotation = descriptor.hasSpecialAnnotation(declaration)
|
||||
declaration.putUserData(NO_ARG_CLASS_KEY, hasSpecialAnnotation)
|
||||
if (hasSpecialAnnotation) {
|
||||
val superClass = descriptor.getSuperClassOrAny()
|
||||
if (superClass.constructors.none { it.isNoArgConstructor() } && !superClass.hasSpecialAnnotation(declaration)) {
|
||||
val reportTarget = declaration.nameIdentifier ?: declaration.getClassOrInterfaceKeyword() ?: declaration
|
||||
context.trace.reportFromPlugin(ErrorsNoArg.NO_NOARG_CONSTRUCTOR_IN_SUPERCLASS.on(reportTarget), DefaultErrorMessagesNoArg)
|
||||
}
|
||||
val superClass = descriptor.getSuperClassOrAny()
|
||||
if (superClass.constructors.none { it.isNoArgConstructor() } && !superClass.hasSpecialAnnotation(declaration)) {
|
||||
val reportTarget = declaration.nameIdentifier ?: declaration.getClassOrInterfaceKeyword() ?: declaration
|
||||
context.trace.reportFromPlugin(ErrorsNoArg.NO_NOARG_CONSTRUCTOR_IN_SUPERCLASS.on(reportTarget), DefaultErrorMessagesNoArg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user