From feb5397f6f592e84b0e66aee7e032323a3d4d150 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 31 Jan 2017 19:03:40 +0300 Subject: [PATCH] AllOpen, NoArg: Refactoring, support presets in order to hold special annotations for Spring and JPA in one place --- .../compiler/plugin/CommandLineProcessor.kt | 7 +++++ .../allopen/allopen-cli/src/AllOpenPlugin.kt | 28 ++++++++++++++----- .../src/AllOpenMavenProjectImportHandler.kt | 14 ++++------ .../synthetic/AndroidComponentRegistrar.kt | 6 +--- .../processing/AnnotationProcessingPlugin.kt | 6 ---- .../org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt | 6 ---- plugins/noarg/noarg-cli/src/NoArgPlugin.kt | 26 +++++++++++------ .../src/NoArgMavenProjectImportHandler.kt | 7 +++-- .../src/SamWithReceiverPlugin.kt | 7 ++--- 9 files changed, 58 insertions(+), 49 deletions(-) diff --git a/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CommandLineProcessor.kt b/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CommandLineProcessor.kt index 34f71f3dced..cb855c0fc17 100644 --- a/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CommandLineProcessor.kt +++ b/compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CommandLineProcessor.kt @@ -17,10 +17,17 @@ package org.jetbrains.kotlin.compiler.plugin import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.config.CompilerConfigurationKey interface CommandLineProcessor { val pluginId: String val pluginOptions: Collection @Throws(CliOptionProcessingException::class) fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) + + fun CompilerConfiguration.appendList(option: CompilerConfigurationKey>, value: T) { + val paths = getList(option).toMutableList() + paths.add(value) + put(option, paths) + } } \ No newline at end of file diff --git a/plugins/allopen/allopen-cli/src/AllOpenPlugin.kt b/plugins/allopen/allopen-cli/src/AllOpenPlugin.kt index 5eae22eb22a..616f724c85e 100644 --- a/plugins/allopen/allopen-cli/src/AllOpenPlugin.kt +++ b/plugins/allopen/allopen-cli/src/AllOpenPlugin.kt @@ -17,6 +17,9 @@ package org.jetbrains.kotlin.allopen import com.intellij.mock.MockProject +import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.SUPPORTED_PRESETS +import org.jetbrains.kotlin.allopen.AllOpenConfigurationKeys.ANNOTATION +import org.jetbrains.kotlin.allopen.AllOpenConfigurationKeys.PRESET import org.jetbrains.kotlin.compiler.plugin.CliOption import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor @@ -28,32 +31,43 @@ import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension object AllOpenConfigurationKeys { val ANNOTATION: CompilerConfigurationKey> = CompilerConfigurationKey.create("annotation qualified name") + + val PRESET: CompilerConfigurationKey> = CompilerConfigurationKey.create("annotation preset") } class AllOpenCommandLineProcessor : CommandLineProcessor { companion object { + val SUPPORTED_PRESETS = mapOf("spring" to listOf( + "org.springframework.stereotype.Component", + "org.springframework.transaction.annotation.Transactional", + "org.springframework.scheduling.annotation.Async", + "org.springframework.cache.annotation.Cacheable")) + val ANNOTATION_OPTION = CliOption("annotation", "", "Annotation qualified names", required = false, allowMultipleOccurrences = true) + val PRESET_OPTION = CliOption("preset", "", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})", + required = false, allowMultipleOccurrences = true) + val PLUGIN_ID = "org.jetbrains.kotlin.allopen" } override val pluginId = PLUGIN_ID - override val pluginOptions = listOf(ANNOTATION_OPTION) + override val pluginOptions = listOf(ANNOTATION_OPTION, PRESET_OPTION) override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) { - ANNOTATION_OPTION -> { - val paths = configuration.getList(AllOpenConfigurationKeys.ANNOTATION).toMutableList() - paths.add(value) - configuration.put(AllOpenConfigurationKeys.ANNOTATION, paths) - } + ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value) + PRESET_OPTION -> configuration.appendList(PRESET, value) else -> throw CliOptionProcessingException("Unknown option: ${option.name}") } } class AllOpenComponentRegistrar : ComponentRegistrar { override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) { - val annotations = configuration.get(AllOpenConfigurationKeys.ANNOTATION) ?: return + val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf() + configuration.get(PRESET)?.forEach { preset -> + SUPPORTED_PRESETS[preset]?.let { annotations += it } + } if (annotations.isEmpty()) return DeclarationAttributeAltererExtension.registerExtension(project, CliAllOpenDeclarationAttributeAltererExtension(annotations)) diff --git a/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt b/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt index 5e78491d4b0..6c3985d51a6 100644 --- a/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt +++ b/plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt @@ -22,13 +22,6 @@ import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { private companion object { val ANNOTATION_PARAMETER_PREFIX = "all-open:${AllOpenCommandLineProcessor.ANNOTATION_OPTION.name}=" - - private val SPRING_ALLOPEN_ANNOTATIONS = listOf( - "org.springframework.stereotype.Component", - "org.springframework.transaction.annotation.Transactional", - "org.springframework.scheduling.annotation.Async", - "org.springframework.cache.annotation.Cacheable" - ) } override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID @@ -42,8 +35,11 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() { } val annotations = mutableListOf() - if ("spring" in enabledCompilerPlugins) { - annotations.addAll(SPRING_ALLOPEN_ANNOTATIONS) + + for ((presetName, presetAnnotations) in AllOpenCommandLineProcessor.SUPPORTED_PRESETS) { + if (presetName in enabledCompilerPlugins) { + annotations.addAll(presetAnnotations) + } } annotations.addAll(compilerPluginOptions.mapNotNull { text -> diff --git a/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt b/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt index 7cd66796c4c..c1baae2b2ca 100644 --- a/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt +++ b/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt @@ -61,11 +61,7 @@ class AndroidCommandLineProcessor : CommandLineProcessor { override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) { when (option) { - VARIANT_OPTION -> { - val paths = configuration.getList(AndroidConfigurationKeys.VARIANT).toMutableList() - paths.add(value) - configuration.put(AndroidConfigurationKeys.VARIANT, paths) - } + VARIANT_OPTION -> configuration.appendList(AndroidConfigurationKeys.VARIANT, value) PACKAGE_OPTION -> configuration.put(AndroidConfigurationKeys.PACKAGE, value) else -> throw CliOptionProcessingException("Unknown option: ${option.name}") } diff --git a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingPlugin.kt b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingPlugin.kt index a839de7f899..4ccd8284e43 100755 --- a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingPlugin.kt +++ b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingPlugin.kt @@ -90,12 +90,6 @@ class AnnotationProcessingCommandLineProcessor : CommandLineProcessor { listOf(GENERATED_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION, CLASS_FILES_OUTPUT_DIR_OPTION, INCREMENTAL_DATA_FILE_OPTION, VERBOSE_MODE_OPTION) - private fun CompilerConfiguration.appendList(option: CompilerConfigurationKey>, value: T) { - val paths = getList(option).toMutableList() - paths.add(value) - put(option, paths) - } - override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) { when (option) { ANNOTATION_PROCESSOR_CLASSPATH_OPTION -> configuration.appendList(ANNOTATION_PROCESSOR_CLASSPATH, value) diff --git a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt index 40e320dc765..ba332e9b3f2 100644 --- a/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt +++ b/plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt @@ -116,12 +116,6 @@ class Kapt3CommandLineProcessor : CommandLineProcessor { listOf(SOURCE_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION, CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION, USE_LIGHT_ANALYSIS_OPTION) - private fun CompilerConfiguration.appendList(option: CompilerConfigurationKey>, value: T) { - val paths = getList(option).toMutableList() - paths.add(value) - put(option, paths) - } - override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) { when (option) { ANNOTATION_PROCESSOR_CLASSPATH_OPTION -> configuration.appendList(ANNOTATION_PROCESSOR_CLASSPATH, value) diff --git a/plugins/noarg/noarg-cli/src/NoArgPlugin.kt b/plugins/noarg/noarg-cli/src/NoArgPlugin.kt index a5ab81d5ae8..cc25f59a5bf 100644 --- a/plugins/noarg/noarg-cli/src/NoArgPlugin.kt +++ b/plugins/noarg/noarg-cli/src/NoArgPlugin.kt @@ -30,6 +30,9 @@ import org.jetbrains.kotlin.container.StorageComponentContainer import org.jetbrains.kotlin.container.useInstance import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor +import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.SUPPORTED_PRESETS +import org.jetbrains.kotlin.noarg.NoArgConfigurationKeys.ANNOTATION +import org.jetbrains.kotlin.noarg.NoArgConfigurationKeys.PRESET import org.jetbrains.kotlin.noarg.diagnostic.CliNoArgDeclarationChecker import org.jetbrains.kotlin.resolve.TargetPlatform import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform @@ -37,32 +40,39 @@ import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform object NoArgConfigurationKeys { val ANNOTATION: CompilerConfigurationKey> = CompilerConfigurationKey.create("annotation qualified name") + + val PRESET: CompilerConfigurationKey> = CompilerConfigurationKey.create("annotation preset") } class NoArgCommandLineProcessor : CommandLineProcessor { companion object { - val PLUGIN_ID = "org.jetbrains.kotlin.noarg" + val SUPPORTED_PRESETS = mapOf("jpa" to listOf("javax.persistence.Entity")) val ANNOTATION_OPTION = CliOption("annotation", "", "Annotation qualified names", required = false, allowMultipleOccurrences = true) + + val PRESET_OPTION = CliOption("preset", "", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})", + required = false, allowMultipleOccurrences = true) + + val PLUGIN_ID = "org.jetbrains.kotlin.noarg" } override val pluginId = PLUGIN_ID - override val pluginOptions = listOf(ANNOTATION_OPTION) + override val pluginOptions = listOf(ANNOTATION_OPTION, PRESET_OPTION) override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) { - ANNOTATION_OPTION -> { - val paths = configuration.getList(NoArgConfigurationKeys.ANNOTATION).toMutableList() - paths.add(value) - configuration.put(NoArgConfigurationKeys.ANNOTATION, paths) - } + ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value) + PRESET_OPTION -> configuration.appendList(PRESET, value) else -> throw CliOptionProcessingException("Unknown option: ${option.name}") } } class NoArgComponentRegistrar : ComponentRegistrar { override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) { - val annotations = configuration.get(NoArgConfigurationKeys.ANNOTATION) ?: return + val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf() + configuration.get(PRESET)?.forEach { preset -> + SUPPORTED_PRESETS[preset]?.let { annotations += it } + } if (annotations.isEmpty()) return Extensions.getRootArea().getExtensionPoint(DefaultErrorMessages.Extension.EP_NAME).registerExtension(DefaultErrorMessagesNoArg()) diff --git a/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt b/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt index 1a8ef64d1a1..84923b25105 100644 --- a/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt +++ b/plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() { private companion object { val ANNOTATATION_PARAMETER_PREFIX = "no-arg:${NoArgCommandLineProcessor.ANNOTATION_OPTION.name}=" - private val JPA_NOARG_ANNOTATIONS = listOf("javax.persistence.Entity") } override val compilerPluginId = NoArgCommandLineProcessor.PLUGIN_ID @@ -36,8 +35,10 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() { } val annotations = mutableListOf() - if ("jpa" in enabledCompilerPlugins) { - annotations.addAll(JPA_NOARG_ANNOTATIONS) + for ((presetName, presetAnnotations) in NoArgCommandLineProcessor.SUPPORTED_PRESETS) { + if (presetName in enabledCompilerPlugins) { + annotations.addAll(presetAnnotations) + } } annotations.addAll(compilerPluginOptions.mapNotNull { text -> diff --git a/plugins/sam-with-receiver/sam-with-receiver-cli/src/SamWithReceiverPlugin.kt b/plugins/sam-with-receiver/sam-with-receiver-cli/src/SamWithReceiverPlugin.kt index 73023cd5271..f262b25f0d6 100644 --- a/plugins/sam-with-receiver/sam-with-receiver-cli/src/SamWithReceiverPlugin.kt +++ b/plugins/sam-with-receiver/sam-with-receiver-cli/src/SamWithReceiverPlugin.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.container.ComponentProvider import org.jetbrains.kotlin.container.get import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver +import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.ANNOTATION object SamWithReceiverConfigurationKeys { val ANNOTATION: CompilerConfigurationKey> = @@ -46,11 +47,7 @@ class SamWithReceiverCommandLineProcessor : CommandLineProcessor { override val pluginOptions = listOf(ANNOTATION_OPTION) override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) { - ANNOTATION_OPTION -> { - val paths = configuration.getList(SamWithReceiverConfigurationKeys.ANNOTATION).toMutableList() - paths.add(value) - configuration.put(SamWithReceiverConfigurationKeys.ANNOTATION, paths) - } + ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value) else -> throw CliOptionProcessingException("Unknown option: ${option.name}") } }