I'm trying to call Java method org.springframework.data.mongodb.core.query.Update#set
from Kotlin code. This method is declared as
public Update set(String key, Object value)
There is no additional annotations or comments about parameters nullability. In fact, it just puts value
into HashMap
, so effectively value
might be null
.
However, Kotlin compiler treats value
type as Any
(instead of expected Any!
), and fails when passing null
with
Null can not be a value of a non-null type Any
Passing nullable value with !!
patently causes KotlinNullPointerException
, casting as Any
throws TypeCastException
.
What is the logic behind such nullability inference? Passing null
parameter with reflection actually works well, but is there more natural solution?
This is caused by the annotation in the package's package-info.java
:
@org.springframework.lang.NonNullApi
package org.springframework.data.mongodb.core.query;
which is