diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationArgumentModel.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationArgumentModel.kt new file mode 100644 index 00000000000..be38adb59dd --- /dev/null +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationArgumentModel.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2010-2022 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.generators.model + +class AnnotationArgumentModel( + val name: String = DEFAULT_NAME, + val value: Any +) { + companion object { + const val DEFAULT_NAME = "value" + } +} diff --git a/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationModel.kt b/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationModel.kt index 8c0872a0934..f4682e7b6a3 100644 --- a/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationModel.kt +++ b/generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationModel.kt @@ -10,15 +10,17 @@ import org.jetbrains.kotlin.utils.Printer class AnnotationModel( val annotation: Class, - val arguments: List + val arguments: List ) { fun generate(p: Printer) { - val argumentsString = arguments.joinToString(separator = ",") { - when (it) { - is Enum<*> -> "${it.javaClass.simpleName}.${it.name}" - is Class<*> -> "${it.simpleName}.class" - else -> "\"$it\"" + val needExplicitNames = arguments.singleOrNull()?.name != AnnotationArgumentModel.DEFAULT_NAME + val argumentsString = arguments.joinToString(separator = ", ") { argument -> + val valueString = when (val value = argument.value) { + is Enum<*> -> "${value.javaClass.simpleName}.${value.name}" + is Class<*> -> "${value.simpleName}.class" + else -> "\"$value\"" } + if (needExplicitNames) "${argument.name} = $valueString" else valueString } p.print("@${annotation.simpleName}($argumentsString)") } @@ -27,10 +29,10 @@ class AnnotationModel( fun imports(): List> { return buildList { add(annotation) - arguments.mapNotNullTo(this) { - when (it) { - is Enum<*> -> it.javaClass - is Class<*> -> it + arguments.mapNotNullTo(this) { argument -> + when (val value = argument.value) { + is Enum<*> -> value.javaClass + is Class<*> -> value else -> null } } @@ -38,6 +40,10 @@ class AnnotationModel( } } -fun annotation(annotation: Class, vararg arguments: Any): AnnotationModel { - return AnnotationModel(annotation, arguments.toList()) +fun annotation(annotation: Class, singleArgumentValue: Any): AnnotationModel { + return AnnotationModel(annotation, listOf(AnnotationArgumentModel(value = singleArgumentValue))) +} + +fun annotation(annotation: Class, vararg arguments: Pair): AnnotationModel { + return AnnotationModel(annotation, arguments.map { AnnotationArgumentModel(it.first, it.second) }) }