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.isIntegral(): Boolean = this in PrimitiveType.integralPrimitives
fun PrimitiveType.isNumeric(): Boolean = this in PrimitiveType.numericPrimitives 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 ConcreteFunction(val textBuilder: (Appendable) -> Unit, val sourceFile: SourceFile)
class GenericFunction(val signature: String, val keyword: String = "fun") { 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>() { open class SpecializedProperty<TKey: Any, TValue : Any>() {
private val values = HashMap<TKey?, TValue>() 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 get(key: TKey): TValue? = values.getOrElse(key, { values.getOrElse(null, { null }) })
operator fun set(keys: Collection<TKey>, value: TValue) { 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) operator fun invoke(value: TValue, vararg keys: TKey) = set(keys.asList(), value)
protected open fun onKeySet(key: TKey) {} 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 FamilyProperty<TValue: Any>() : SpecializedProperty<Family, TValue>()
open class PrimitiveProperty<TValue: Any>() : SpecializedProperty<PrimitiveType, 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 defaultFamilies = Family.defaultFamilies
val defaultPrimitives = PrimitiveType.defaultPrimitives val defaultPrimitives = PrimitiveType.defaultPrimitives
@@ -102,8 +107,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
val buildFamilyPrimitives = FamilyProperty<Set<PrimitiveType>>() val buildFamilyPrimitives = FamilyProperty<Set<PrimitiveType>>()
val customSignature = FamilyProperty<String>() val customSignature = FamilyProperty<String>()
val deprecate = FamilyProperty<String>() val deprecate = DeprecationProperty()
val deprecateReplacement = FamilyProperty<String>()
val doc = FamilyProperty<String>() val doc = FamilyProperty<String>()
val platformName = PrimitiveProperty<String>() val platformName = PrimitiveProperty<String>()
val inline = FamilyProperty<Boolean>() val inline = FamilyProperty<Boolean>()
@@ -358,8 +362,12 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
} }
deprecate[f]?.let { deprecated -> deprecate[f]?.let { deprecated ->
val replacement = deprecateReplacement[f]?.let { ", ReplaceWith(\"$it\")" } ?: "" val args = listOfNotNull(
builder.append("@Deprecated(\"$deprecated\"$replacement)\n") "\"${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) { 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()}") builder.append(".${(customSignature[f] ?: signature).renderType()}: ${returnType.renderType()}")
if (keyword == "fun") builder.append(" {") 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() val indent: Int = body.takeWhile { it == ' ' }.length()
builder.append('\n') builder.append('\n')
@@ -449,9 +449,7 @@ fun generators(): List<GenericFunction> {
typeParam("V") typeParam("V")
returns("List<V>") returns("List<V>")
inline(true) inline(true)
deprecate("Use zip() with transform instead.") deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(other, transform)"))
deprecateReplacement("zip(other, transform)")
body { """return ${deprecateReplacement.default}""" }
} }
templates add f("zip(other: Iterable<R>, transform: (T, R) -> V)") { templates add f("zip(other: Iterable<R>, transform: (T, R) -> V)") {
@@ -496,9 +494,7 @@ fun generators(): List<GenericFunction> {
typeParam("V") typeParam("V")
returns("List<V>") returns("List<V>")
inline(true) inline(true)
deprecate("Use zip() with transform instead.") deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(array, transform)"))
deprecateReplacement("zip(array, transform)")
body { """return ${deprecateReplacement.default}""" }
} }
templates add f("zip(array: Array<out R>, transform: (T, R) -> V)") { templates add f("zip(array: Array<out R>, transform: (T, R) -> V)") {
@@ -542,9 +538,7 @@ fun generators(): List<GenericFunction> {
typeParam("V") typeParam("V")
returns("List<V>") returns("List<V>")
inline(true) inline(true)
deprecate("Use zip() with transform instead.") deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(array, transform)"))
deprecateReplacement("zip(array, transform)")
body { """return ${deprecateReplacement.default}""" }
} }
templates add f("zip(array: SELF, transform: (T, T) -> V)") { templates add f("zip(array: SELF, transform: (T, T) -> V)") {
@@ -574,9 +568,7 @@ fun generators(): List<GenericFunction> {
typeParam("R") typeParam("R")
typeParam("V") typeParam("V")
returns("Sequence<V>") returns("Sequence<V>")
deprecate("Use zip() with transform instead.") deprecate(Deprecation("Use zip() with transform instead.", replaceWith = "zip(sequence, transform)"))
deprecateReplacement("zip(sequence, transform)")
body { """return ${deprecateReplacement.default}""" }
} }
templates add f("zip(sequence: Sequence<R>, transform: (T, R) -> V)") { templates add f("zip(sequence: Sequence<R>, transform: (T, R) -> V)") {
@@ -75,8 +75,7 @@ fun mapping(): List<GenericFunction> {
inline(true) inline(true)
exclude(Strings, ArraysOfPrimitives) 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." } 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.") deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().map {} instead.", replaceWith = "filterNotNull().map(transform)"))
deprecateReplacement("filterNotNull().map(transform)")
typeParam("T : Any") typeParam("T : Any")
typeParam("R") typeParam("R")
returns("List<R>") returns("List<R>")
@@ -153,8 +152,8 @@ fun mapping(): List<GenericFunction> {
to the given [destination]. to the given [destination].
""" """
} }
deprecate("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().mapTo(destination) {} instead.") deprecate(Deprecation("This function will change its semantics soon to map&filter rather than filter&map. Use filterNotNull().mapTo(destination) {} instead.",
deprecateReplacement("filterNotNull().mapTo(destination, transform)") replaceWith = "filterNotNull().mapTo(destination, transform)"))
typeParam("T : Any") typeParam("T : Any")
typeParam("R") typeParam("R")
typeParam("C : MutableCollection<in R>") typeParam("C : MutableCollection<in R>")
@@ -98,9 +98,7 @@ fun snapshots(): List<GenericFunction> {
include(Strings) include(Strings)
typeParam("K") typeParam("K")
returns("Map<K, T>") returns("Map<K, T>")
deprecate("Use toMapBy instead.") deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)"))
deprecateReplacement("toMapBy(selector)")
body("return ${deprecateReplacement.default}")
} }
templates add f("toMapBy(selector: (T) -> K)") { templates add f("toMapBy(selector: (T) -> K)") {