You can use toString
method of data classes and parse its output
Sample(name=abc, model=null, color=null, type=xyz, manufacturer=null)
with following code:
val nonNulls = Sample().toString()
.substringAfter('(')
.substringBeforeLast(')')
.split(", ")
.map { with(it.split("=")) { this[0] to this[1] } }
.filter { it.second != "null" }
.map { it.first }
println(nonNulls)
Result:
[name, type]
The obvious restrictions:
- Works only for
data
classes
- Ignores properties that have string value "null"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…