diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index 580c6e69432..c32d45526d0 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -58,6 +58,8 @@ enum class PrimitiveType { fun PrimitiveType.isIntegral(): Boolean = this in PrimitiveType.integralPrimitives fun PrimitiveType.isNumeric(): Boolean = this in PrimitiveType.numericPrimitives +data class Deprecation(val message: String, val replaceWith: String? = null, val level: DeprecationLevel = DeprecationLevel.WARNING) + class ConcreteFunction(val textBuilder: (Appendable) -> Unit, val sourceFile: SourceFile) class GenericFunction(val signature: String, val keyword: String = "fun") { @@ -65,8 +67,6 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { open class SpecializedProperty() { private val values = HashMap() - val default: TValue? get() = values.get(null) - operator fun get(key: TKey): TValue? = values.getOrElse(key, { values.getOrElse(null, { null }) }) operator fun set(keys: Collection, value: TValue) { @@ -79,7 +79,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { } } - operator fun invoke(vararg keys: TKey, valueBuilder: ()-> TValue) = set(keys.asList(), valueBuilder()) + operator fun invoke(vararg keys: TKey, valueBuilder: () -> TValue) = set(keys.asList(), valueBuilder()) operator fun invoke(value: TValue, vararg keys: TKey) = set(keys.asList(), value) protected open fun onKeySet(key: TKey) {} @@ -88,6 +88,11 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { open class FamilyProperty() : SpecializedProperty() open class PrimitiveProperty() : SpecializedProperty() + class DeprecationProperty() : FamilyProperty() { + operator fun invoke(value: String, vararg keys: Family) = set(keys.asList(), Deprecation(value)) + } + + val defaultFamilies = Family.defaultFamilies val defaultPrimitives = PrimitiveType.defaultPrimitives @@ -102,8 +107,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { val buildFamilyPrimitives = FamilyProperty>() val customSignature = FamilyProperty() - val deprecate = FamilyProperty() - val deprecateReplacement = FamilyProperty() + val deprecate = DeprecationProperty() val doc = FamilyProperty() val platformName = PrimitiveProperty() val inline = FamilyProperty() @@ -358,8 +362,12 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { } deprecate[f]?.let { deprecated -> - val replacement = deprecateReplacement[f]?.let { ", ReplaceWith(\"$it\")" } ?: "" - builder.append("@Deprecated(\"$deprecated\"$replacement)\n") + val args = listOfNotNull( + "\"${deprecated.message}\"", + deprecated.replaceWith?.let { "ReplaceWith(\"$it\")" }, + deprecated.level.let { if (it != DeprecationLevel.WARNING) "level = DeprecationLevel.$it" else null } + ) + builder.append("@Deprecated(${args.joinToString(", ")})\n") } if (!f.isPrimitiveSpecialization && primitive != null) { @@ -395,7 +403,12 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { builder.append(".${(customSignature[f] ?: signature).renderType()}: ${returnType.renderType()}") if (keyword == "fun") builder.append(" {") - val body = (customPrimitiveBodies[f to primitive] ?: body[f] ?: throw RuntimeException("No body specified for $signature for ${f to primitive}")).trim('\n') + val body = ( + customPrimitiveBodies[f to primitive] ?: + body[f] ?: + deprecate[f]?.replaceWith?.let { "return $it" } ?: + throw RuntimeException("No body specified for $signature for ${f to primitive}") + ).trim('\n') val indent: Int = body.takeWhile { it == ' ' }.length() builder.append('\n') diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 7de3f78bd8e..6f68e52d230 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -449,9 +449,7 @@ fun generators(): List { typeParam("V") returns("List") inline(true) - deprecate("Use zip() with transform instead.") - deprecateReplacement("zip(other, transform)") - body { """return ${deprecateReplacement.default}""" } + deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(other, transform)")) } templates add f("zip(other: Iterable, transform: (T, R) -> V)") { @@ -496,9 +494,7 @@ fun generators(): List { typeParam("V") returns("List") inline(true) - deprecate("Use zip() with transform instead.") - deprecateReplacement("zip(array, transform)") - body { """return ${deprecateReplacement.default}""" } + deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(array, transform)")) } templates add f("zip(array: Array, transform: (T, R) -> V)") { @@ -542,9 +538,7 @@ fun generators(): List { typeParam("V") returns("List") inline(true) - deprecate("Use zip() with transform instead.") - deprecateReplacement("zip(array, transform)") - body { """return ${deprecateReplacement.default}""" } + deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(array, transform)")) } templates add f("zip(array: SELF, transform: (T, T) -> V)") { @@ -574,9 +568,7 @@ fun generators(): List { typeParam("R") typeParam("V") returns("Sequence") - deprecate("Use zip() with transform instead.") - deprecateReplacement("zip(sequence, transform)") - body { """return ${deprecateReplacement.default}""" } + deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(sequence, transform)")) } templates add f("zip(sequence: Sequence, transform: (T, R) -> V)") { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 4867b53be3b..632b6c9c91e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -75,8 +75,7 @@ fun mapping(): List { inline(true) exclude(Strings, ArraysOfPrimitives) doc { "Returns a list containing the results of applying the given [transform] function to each non-null element of the original collection." } - deprecate("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().map {} instead.") - deprecateReplacement("filterNotNull().map(transform)") + deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().map {} instead.", replaceWith = "filterNotNull().map(transform)")) typeParam("T : Any") typeParam("R") returns("List") @@ -153,8 +152,8 @@ fun mapping(): List { to the given [destination]. """ } - deprecate("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().mapTo(destination) {} instead.") - deprecateReplacement("filterNotNull().mapTo(destination, transform)") + deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().mapTo(destination) {} instead.", + replaceWith = "filterNotNull().mapTo(destination, transform)")) typeParam("T : Any") typeParam("R") typeParam("C : MutableCollection") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 71f0e0a5189..b0622a3148f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -98,9 +98,7 @@ fun snapshots(): List { include(Strings) typeParam("K") returns("Map") - deprecate("Use toMapBy instead.") - deprecateReplacement("toMapBy(selector)") - body("return ${deprecateReplacement.default}") + deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)")) } templates add f("toMapBy(selector: (T) -> K)") {