Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nullable properties through reflection

Tags:

kotlin

Is there a way to list all properties of an object that are allowed to return nulls?

val cls = javaClass<T>().kotlin

for(property in cls.properties) {
    if(property.accessible) {
        //Is it nullable?

    }
}
like image 275
atok Avatar asked Aug 10 '15 13:08

atok


1 Answers

The API you're looking for was introduced in latest Kotlin builds (0.13.213+). You can now take the type of a property and find out if it was marked as nullable in the source code:

val property = ...
if (property.returnType.isMarkedNullable) {
    ...
}
like image 116
Alexander Udalov Avatar answered Oct 08 '22 21:10

Alexander Udalov