How to get TypeName with correct nullable

Viewed 254

I try to call this.asType().asTypeName().isNullable isNullable - does not work correct.

I found a solution, but it does not look well:

fun Element.getTypeNameWithRealNullable(): TypeName {
    val annotation = this.getAnnotationMirror(Nullable::class)
    return annotation?.let {
        this.asType().asTypeName().copy(nullable = true)
    } ?: this.asType().asTypeName().copy(nullable = false)
}

fun <T : Annotation> Element.getAnnotationMirror(type: KClass<T>): AnnotationMirror? {
    return this.getAnnotation(type.java.name)
}

Do kotlinpoet has better way for get correct isNullable?

0 Answers
Related