Setting values of all properties of a class
suggest changeAs an example, we want to set all string properties of a sample class:
class TestClass {
val readOnlyProperty: String
get() = "Read only!"
var readWriteString = "asd"
var readWriteInt = 23
var readWriteBackedStringProperty: String = ""
get() = field + '5'
set(value) { field = value + '5' }
var readWriteBackedIntProperty: Int = 0
get() = field + 1
set(value) { field = value - 1 }
var delegatedProperty: Int by TestDelegate()
private var privateProperty = "This should be private"
private class TestDelegate {
private var backingField = 3
operator fun getValue(thisRef: Any?, prop: KProperty<*>): Int {
return backingField
}
operator fun setValue(thisRef: Any?, prop: KProperty<*>, value: Int) {
backingField += value
}
}
}
Getting mutable properties builds on getting all properties, then filtering them by type. We also need to check visibility, because reading private properties throws a runtime exception.
val instance = TestClass()
TestClass::class.memberProperties
.filter { it.visibility == KVisibility.PUBLIC }
.filterIsInstance<KMutableProperty<*>>()
.forEach { prop ->
System.out.println("${prop.name} -> ${prop.get(instance)}")
}
To set all String properties to "Our Value" we can additionally filter by the return type. Since Kotlin is based on Java VM, Type Erasure is in effect, and thus Properties returning generic types such as List<String> will be the same as List<Any>. Sadly reflection is not a golden bullet and there is no sensible way to avoid this, so you need to watch out in your use-cases.
val instance = TestClass()
TestClass::class.memberProperties
.filter { it.visibility == KVisibility.PUBLIC }
// We only want strings
.filter{ it.returnType.isSubtypeOf(String::class.starProjectedType) }
.filterIsInstance<KMutableProperty<*>>()
.forEach { prop ->
// Instead of printing the property we set it to some value
prop.setter.call(instance, "Our Value")
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents