explode Type T
fun explodeTypeT(signature: String = "_<T>", synthesize: Boolean = false, f: Classes.() -> Unit)(source)
Iterates over all classes given a generic type signature, e.g. Map<_, List<T>>
. The signature parameter describes the generic type to search for. It must contain a T
token, which will be replaced with each declaration during iteration. The _
symbol can be used to match any class.
Type constraints with names (e.g. String, Map) are only applied to direct ancestors of T
. In the signature Pair<Foo, List<Map<Bar, T>>>
, Foo
and Bar
are not directly related T
and will therefore not be evaluated.
This function can greatly reduce the boilerplate associated with manually unpacking type signatures. The following two templates are equivalent:
val a = classes {
methods {
returns {
explodeTypeT("Map<_, List<Pair<T, _>>>", synthesize = true) {
entity(payload)
}
}
}
}
val b = classes {
methods {
returns {
filter(Regex("^.+\\.Map\$"))
typeArgument(1) { // List<Pair<Payload, Int>>
filter(Regex("^.+\\.List\$"))
typeArgument(0) { // Pair<Payload, Int>
filter(Regex("^.+\\.Pair\$"))
typeArgument(0) { // Payload
explodeType(synthesize = true) {
entity(payload)
}
}
}
}
}
}
}
assert(a == b) { "expecting a and b to have the same underlying representation" }
Content copied to clipboard