From 18cd07ea5fe8b3e78b0bba7322f8e693e9f05c61 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Wed, 26 Jan 2022 19:38:44 +0300 Subject: [PATCH] [Test] Fix proper use of annotation parameter names in generated classes When an annotation parameter has a different name than "value" or there is more than one annotation parameter we need to specify annotation parameter names explicitly in generated Java source code. Fix using classes as annotation parameters in generated classes --- .../model/AnnotationArgumentModel.kt | 15 ++++++++++ .../generators/model/AnnotationModel.kt | 30 +++++++++++-------- 2 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 generators/test-generator/tests/org/jetbrains/kotlin/generators/model/AnnotationArgumentModel.kt 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) }) }