Open hastebrot opened 6 years ago
data class TestBean(
var string: String = "foo",
var integer: Int = 1,
val stringReadonly: String = "bar",
val integerReadonly: Int = 2,
val array: MutableList<String> = mutableListOf(),
val bean: TestBean? = null
)
fun main(vararg args: String) {
properties(TestBean::class).forEach {
val propertyName = it.name
val propertyType = it.returnType
val propertyImpl = it::class.simpleName
println("$propertyName: $propertyType ($propertyImpl)")
}
}
private fun <T: Any> properties(type: KClass<T>): Collection<KProperty1<T, *>>
= type.memberProperties
array: kotlin.collections.MutableList<kotlin.String> (KProperty1Impl)
bean: protokolax.TestBean? (KProperty1Impl)
integer: kotlin.Int (KMutableProperty1Impl)
integerReadonly: kotlin.Int (KProperty1Impl)
string: kotlin.String (KMutableProperty1Impl)
stringReadonly: kotlin.String (KProperty1Impl)
So, reflection of properties is not very nice. Let us use annotations instead and directly tell, what we want to to with the properties.
@Target(AnnotationTarget.PROPERTY)
annotation class Observe(vararg val types: ObserveType)
enum class ObserveType {
CHANGES, SPLICES, PATHS
}
data class Person(
@Observe(CHANGES) var firstName: String? = null,
@Observe(CHANGES) var lastName: String? = null,
@Observe(SPLICES) val foods: List<String> = mutableListOf(),
@Observe(SPLICES, PATHS) val friends: List<Person> = mutableListOf()
)
The registry orchestrates trees of observable objects (property paths) with multiple observable values (bindings).