Hey guys! Does anyone know how to make Kotlin comp...
# server
l
Hey guys! Does anyone know how to make Kotlin compiler understand that a method return type is nullable? There is a class
org.springframework.transaction.support.AbstractPlatformTransactionManager
which method
doGetTransaction
is actually nullable, but Kotlin compiler doesn't understand that. In the first picture - a method itself and in the second one - method which is used after
doGetTransaction
with nullable param. I'm 100% sure
doGetTransaction
return type should be treated as nullable.
Any way to make this compile?
I think this happens due to
org.springframework.lang.NonNullApi
m
If Pivotal have annotated it with NonNullApi, and not explicitly marked the method as returning Null, then either the method has been annotated incorrectly OR it truly can't return a null. If it's annotation based, you could turn off jsr305 compiler option (the one that enables null annotation processing). Also, based on the documentation you posted, the interface assumes something will be returned. So, the compiler is 'doing the right thing'.
b
As an aside, if you're confident that the return type could be nullable, you should raise a PR against the open source repository adding this annotation (
@Nullable
on the method in question). It won't immediately fix your problem, but it would fix it in the future for yourself and others.
m
The Spring Docs are generally very accurate, and make no mention of returning a null. This tends to indicate the method is defined, and interpreted correctly by the Kotlin compiler. i.e. the return should not be null.
b
I also agree with that sentiment, I just didn't dig into it very far. I know for a fact that Pivotal went through a huge effort to make spring boot 2.0.0+ compatible with Kotlin, including adding all the nullability annotations.
m
Pedantic point: Pivotal made it more idiomatic when working with Kotlin. It's always been Kotlin compatible. I've had occasions where saying something was made compatible makes people assume it wasn't compatible/usable with Kotlin prior to this version, hence my extra caution/warning.
👍 2
👆 1
s
I will have a look today
Juergen in on vacation so you will have to wait for the definitive answer, but don't think
doGetTransaction
return value should be
null
, based on existing implementation the contract seems to be throwing
TransactionException
in case of
null
transaction. For
suspend()
that's different
null
transaction means "suspend active synchronizations" as stated in the Javadoc.