Skip to content

Commit feb5397

Browse files
committed
AllOpen, NoArg: Refactoring, support presets in order to hold special annotations for Spring and JPA in one place
1 parent f4690eb commit feb5397

File tree

9 files changed

+58
-49
lines changed

9 files changed

+58
-49
lines changed

compiler/plugin-api/src/org/jetbrains/kotlin/compiler/plugin/CommandLineProcessor.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
package org.jetbrains.kotlin.compiler.plugin
1818

1919
import org.jetbrains.kotlin.config.CompilerConfiguration
20+
import org.jetbrains.kotlin.config.CompilerConfigurationKey
2021

2122
interface CommandLineProcessor {
2223
val pluginId: String
2324
val pluginOptions: Collection<CliOption>
2425

2526
@Throws(CliOptionProcessingException::class) fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration)
27+
28+
fun <T> CompilerConfiguration.appendList(option: CompilerConfigurationKey<List<T>>, value: T) {
29+
val paths = getList(option).toMutableList()
30+
paths.add(value)
31+
put(option, paths)
32+
}
2633
}

plugins/allopen/allopen-cli/src/AllOpenPlugin.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.jetbrains.kotlin.allopen
1818

1919
import com.intellij.mock.MockProject
20+
import org.jetbrains.kotlin.allopen.AllOpenCommandLineProcessor.Companion.SUPPORTED_PRESETS
21+
import org.jetbrains.kotlin.allopen.AllOpenConfigurationKeys.ANNOTATION
22+
import org.jetbrains.kotlin.allopen.AllOpenConfigurationKeys.PRESET
2023
import org.jetbrains.kotlin.compiler.plugin.CliOption
2124
import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException
2225
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
@@ -28,32 +31,43 @@ import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
2831
object AllOpenConfigurationKeys {
2932
val ANNOTATION: CompilerConfigurationKey<List<String>> =
3033
CompilerConfigurationKey.create("annotation qualified name")
34+
35+
val PRESET: CompilerConfigurationKey<List<String>> = CompilerConfigurationKey.create("annotation preset")
3136
}
3237

3338
class AllOpenCommandLineProcessor : CommandLineProcessor {
3439
companion object {
40+
val SUPPORTED_PRESETS = mapOf("spring" to listOf(
41+
"org.springframework.stereotype.Component",
42+
"org.springframework.transaction.annotation.Transactional",
43+
"org.springframework.scheduling.annotation.Async",
44+
"org.springframework.cache.annotation.Cacheable"))
45+
3546
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
3647
required = false, allowMultipleOccurrences = true)
3748

49+
val PRESET_OPTION = CliOption("preset", "<name>", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})",
50+
required = false, allowMultipleOccurrences = true)
51+
3852
val PLUGIN_ID = "org.jetbrains.kotlin.allopen"
3953
}
4054

4155
override val pluginId = PLUGIN_ID
42-
override val pluginOptions = listOf(ANNOTATION_OPTION)
56+
override val pluginOptions = listOf(ANNOTATION_OPTION, PRESET_OPTION)
4357

4458
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
45-
ANNOTATION_OPTION -> {
46-
val paths = configuration.getList(AllOpenConfigurationKeys.ANNOTATION).toMutableList()
47-
paths.add(value)
48-
configuration.put(AllOpenConfigurationKeys.ANNOTATION, paths)
49-
}
59+
ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value)
60+
PRESET_OPTION -> configuration.appendList(PRESET, value)
5061
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
5162
}
5263
}
5364

5465
class AllOpenComponentRegistrar : ComponentRegistrar {
5566
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
56-
val annotations = configuration.get(AllOpenConfigurationKeys.ANNOTATION) ?: return
67+
val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf()
68+
configuration.get(PRESET)?.forEach { preset ->
69+
SUPPORTED_PRESETS[preset]?.let { annotations += it }
70+
}
5771
if (annotations.isEmpty()) return
5872

5973
DeclarationAttributeAltererExtension.registerExtension(project, CliAllOpenDeclarationAttributeAltererExtension(annotations))

plugins/allopen/allopen-ide/src/AllOpenMavenProjectImportHandler.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
2222
class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
2323
private companion object {
2424
val ANNOTATION_PARAMETER_PREFIX = "all-open:${AllOpenCommandLineProcessor.ANNOTATION_OPTION.name}="
25-
26-
private val SPRING_ALLOPEN_ANNOTATIONS = listOf(
27-
"org.springframework.stereotype.Component",
28-
"org.springframework.transaction.annotation.Transactional",
29-
"org.springframework.scheduling.annotation.Async",
30-
"org.springframework.cache.annotation.Cacheable"
31-
)
3225
}
3326

3427
override val compilerPluginId = AllOpenCommandLineProcessor.PLUGIN_ID
@@ -42,8 +35,11 @@ class AllOpenMavenProjectImportHandler : AbstractMavenImportHandler() {
4235
}
4336

4437
val annotations = mutableListOf<String>()
45-
if ("spring" in enabledCompilerPlugins) {
46-
annotations.addAll(SPRING_ALLOPEN_ANNOTATIONS)
38+
39+
for ((presetName, presetAnnotations) in AllOpenCommandLineProcessor.SUPPORTED_PRESETS) {
40+
if (presetName in enabledCompilerPlugins) {
41+
annotations.addAll(presetAnnotations)
42+
}
4743
}
4844

4945
annotations.addAll(compilerPluginOptions.mapNotNull { text ->

plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ class AndroidCommandLineProcessor : CommandLineProcessor {
6161

6262
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
6363
when (option) {
64-
VARIANT_OPTION -> {
65-
val paths = configuration.getList(AndroidConfigurationKeys.VARIANT).toMutableList()
66-
paths.add(value)
67-
configuration.put(AndroidConfigurationKeys.VARIANT, paths)
68-
}
64+
VARIANT_OPTION -> configuration.appendList(AndroidConfigurationKeys.VARIANT, value)
6965
PACKAGE_OPTION -> configuration.put(AndroidConfigurationKeys.PACKAGE, value)
7066
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
7167
}

plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingPlugin.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ class AnnotationProcessingCommandLineProcessor : CommandLineProcessor {
9090
listOf(GENERATED_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION,
9191
CLASS_FILES_OUTPUT_DIR_OPTION, INCREMENTAL_DATA_FILE_OPTION, VERBOSE_MODE_OPTION)
9292

93-
private fun <T> CompilerConfiguration.appendList(option: CompilerConfigurationKey<List<T>>, value: T) {
94-
val paths = getList(option).toMutableList()
95-
paths.add(value)
96-
put(option, paths)
97-
}
98-
9993
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
10094
when (option) {
10195
ANNOTATION_PROCESSOR_CLASSPATH_OPTION -> configuration.appendList(ANNOTATION_PROCESSOR_CLASSPATH, value)

plugins/kapt3/src/org/jetbrains/kotlin/kapt3/Kapt3Plugin.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
116116
listOf(SOURCE_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION,
117117
CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION, USE_LIGHT_ANALYSIS_OPTION)
118118

119-
private fun <T> CompilerConfiguration.appendList(option: CompilerConfigurationKey<List<T>>, value: T) {
120-
val paths = getList(option).toMutableList()
121-
paths.add(value)
122-
put(option, paths)
123-
}
124-
125119
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
126120
when (option) {
127121
ANNOTATION_PROCESSOR_CLASSPATH_OPTION -> configuration.appendList(ANNOTATION_PROCESSOR_CLASSPATH, value)

plugins/noarg/noarg-cli/src/NoArgPlugin.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,49 @@ import org.jetbrains.kotlin.container.StorageComponentContainer
3030
import org.jetbrains.kotlin.container.useInstance
3131
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
3232
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
33+
import org.jetbrains.kotlin.noarg.NoArgCommandLineProcessor.Companion.SUPPORTED_PRESETS
34+
import org.jetbrains.kotlin.noarg.NoArgConfigurationKeys.ANNOTATION
35+
import org.jetbrains.kotlin.noarg.NoArgConfigurationKeys.PRESET
3336
import org.jetbrains.kotlin.noarg.diagnostic.CliNoArgDeclarationChecker
3437
import org.jetbrains.kotlin.resolve.TargetPlatform
3538
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
3639

3740
object NoArgConfigurationKeys {
3841
val ANNOTATION: CompilerConfigurationKey<List<String>> =
3942
CompilerConfigurationKey.create("annotation qualified name")
43+
44+
val PRESET: CompilerConfigurationKey<List<String>> = CompilerConfigurationKey.create("annotation preset")
4045
}
4146

4247
class NoArgCommandLineProcessor : CommandLineProcessor {
4348
companion object {
44-
val PLUGIN_ID = "org.jetbrains.kotlin.noarg"
49+
val SUPPORTED_PRESETS = mapOf("jpa" to listOf("javax.persistence.Entity"))
4550

4651
val ANNOTATION_OPTION = CliOption("annotation", "<fqname>", "Annotation qualified names",
4752
required = false, allowMultipleOccurrences = true)
53+
54+
val PRESET_OPTION = CliOption("preset", "<name>", "Preset name (${SUPPORTED_PRESETS.keys.joinToString()})",
55+
required = false, allowMultipleOccurrences = true)
56+
57+
val PLUGIN_ID = "org.jetbrains.kotlin.noarg"
4858
}
4959

5060
override val pluginId = PLUGIN_ID
51-
override val pluginOptions = listOf(ANNOTATION_OPTION)
61+
override val pluginOptions = listOf(ANNOTATION_OPTION, PRESET_OPTION)
5262

5363
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
54-
ANNOTATION_OPTION -> {
55-
val paths = configuration.getList(NoArgConfigurationKeys.ANNOTATION).toMutableList()
56-
paths.add(value)
57-
configuration.put(NoArgConfigurationKeys.ANNOTATION, paths)
58-
}
64+
ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value)
65+
PRESET_OPTION -> configuration.appendList(PRESET, value)
5966
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
6067
}
6168
}
6269

6370
class NoArgComponentRegistrar : ComponentRegistrar {
6471
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
65-
val annotations = configuration.get(NoArgConfigurationKeys.ANNOTATION) ?: return
72+
val annotations = configuration.get(ANNOTATION)?.toMutableList() ?: mutableListOf()
73+
configuration.get(PRESET)?.forEach { preset ->
74+
SUPPORTED_PRESETS[preset]?.let { annotations += it }
75+
}
6676
if (annotations.isEmpty()) return
6777

6878
Extensions.getRootArea().getExtensionPoint(DefaultErrorMessages.Extension.EP_NAME).registerExtension(DefaultErrorMessagesNoArg())

plugins/noarg/noarg-ide/src/NoArgMavenProjectImportHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.annotation.plugin.ide.AbstractMavenImportHandler
2222
class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() {
2323
private companion object {
2424
val ANNOTATATION_PARAMETER_PREFIX = "no-arg:${NoArgCommandLineProcessor.ANNOTATION_OPTION.name}="
25-
private val JPA_NOARG_ANNOTATIONS = listOf("javax.persistence.Entity")
2625
}
2726

2827
override val compilerPluginId = NoArgCommandLineProcessor.PLUGIN_ID
@@ -36,8 +35,10 @@ class NoArgMavenProjectImportHandler : AbstractMavenImportHandler() {
3635
}
3736

3837
val annotations = mutableListOf<String>()
39-
if ("jpa" in enabledCompilerPlugins) {
40-
annotations.addAll(JPA_NOARG_ANNOTATIONS)
38+
for ((presetName, presetAnnotations) in NoArgCommandLineProcessor.SUPPORTED_PRESETS) {
39+
if (presetName in enabledCompilerPlugins) {
40+
annotations.addAll(presetAnnotations)
41+
}
4142
}
4243

4344
annotations.addAll(compilerPluginOptions.mapNotNull { text ->

plugins/sam-with-receiver/sam-with-receiver-cli/src/SamWithReceiverPlugin.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.container.ComponentProvider
2828
import org.jetbrains.kotlin.container.get
2929
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
3030
import org.jetbrains.kotlin.load.java.sam.SamWithReceiverResolver
31+
import org.jetbrains.kotlin.samWithReceiver.SamWithReceiverConfigurationKeys.ANNOTATION
3132

3233
object SamWithReceiverConfigurationKeys {
3334
val ANNOTATION: CompilerConfigurationKey<List<String>> =
@@ -46,11 +47,7 @@ class SamWithReceiverCommandLineProcessor : CommandLineProcessor {
4647
override val pluginOptions = listOf(ANNOTATION_OPTION)
4748

4849
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) = when (option) {
49-
ANNOTATION_OPTION -> {
50-
val paths = configuration.getList(SamWithReceiverConfigurationKeys.ANNOTATION).toMutableList()
51-
paths.add(value)
52-
configuration.put(SamWithReceiverConfigurationKeys.ANNOTATION, paths)
53-
}
50+
ANNOTATION_OPTION -> configuration.appendList(ANNOTATION, value)
5451
else -> throw CliOptionProcessingException("Unknown option: ${option.name}")
5552
}
5653
}

0 commit comments

Comments
 (0)