Reputation: 551
I cannot implement this java interface in kotlin.
import org.springframework.core.convert.converter.Converter
import org.springframework.stereotype.Component
@Component
class FeatureFlagConverter : Converter<String?, FeatureFlags> {
override fun convert(source: String?): FeatureFlags = FeatureFlags(source ?: "")
}
Compiler fails with following:
Class 'FeatureFlagConverter' is not abstract and does not implement abstract member public abstract fun convert(source: String): FeatureFlags? defined in org.springframework.core.convert.converter.Converter
Why? The interface is pure java interface (https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java) so the source
parameter in convert
method might be null, and there is no anotation to hint that it is not null.
It helps to remove the nullabilty around String. But I do not want to do that since it is Java interface and the source
parameter in convert
method might be null. And some java caller might pass null here.
Upvotes: 1
Views: 97
Reputation: 93639
The documentation for the method you linked says:
source the source object to convert, which must be an instance of
S
(nevernull
)
So S
is required to be non-nullable.
The reason Kotlin seems to "know" that the parameter must be non-nullable is likely from nullability annotations. Kotlin supports several nullability annotations for Java. Spring's nullability annotations are meta-annotated using JSR-305, so they are supported.
Although this method doesn't mark source
as @NonNull
, I think the whole package is marked with non-nullability as the default here.
Upvotes: 2