StdLib generator: refactor deprecation DSL to be able to set deprecation level.

This commit is contained in:
Ilya Gorbunov
2015-10-13 20:16:46 +03:00
parent 66417ed329
commit a5ccecfc3d
4 changed files with 29 additions and 27 deletions
@@ -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<TKey: Any, TValue : Any>() {
private val values = HashMap<TKey?, TValue>()
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<TKey>, 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<TValue: Any>() : SpecializedProperty<Family, TValue>()
open class PrimitiveProperty<TValue: Any>() : SpecializedProperty<PrimitiveType, TValue>()
class DeprecationProperty() : FamilyProperty<Deprecation>() {
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<Set<PrimitiveType>>()
val customSignature = FamilyProperty<String>()
val deprecate = FamilyProperty<String>()
val deprecateReplacement = FamilyProperty<String>()
val deprecate = DeprecationProperty()
val doc = FamilyProperty<String>()
val platformName = PrimitiveProperty<String>()
val inline = FamilyProperty<Boolean>()
@@ -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')
@@ -449,9 +449,7 @@ fun generators(): List<GenericFunction> {
typeParam("V")
returns("List<V>")
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<R>, transform: (T, R) -> V)") {
@@ -496,9 +494,7 @@ fun generators(): List<GenericFunction> {
typeParam("V")
returns("List<V>")
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<out R>, transform: (T, R) -> V)") {
@@ -542,9 +538,7 @@ fun generators(): List<GenericFunction> {
typeParam("V")
returns("List<V>")
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<GenericFunction> {
typeParam("R")
typeParam("V")
returns("Sequence<V>")
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<R>, transform: (T, R) -> V)") {
@@ -75,8 +75,7 @@ fun mapping(): List<GenericFunction> {
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<R>")
@@ -153,8 +152,8 @@ fun mapping(): List<GenericFunction> {
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<in R>")
@@ -98,9 +98,7 @@ fun snapshots(): List<GenericFunction> {
include(Strings)
typeParam("K")
returns("Map<K, T>")
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)") {