Is there a way to tell Kotlin to convert all insta...
# announcements
m
Is there a way to tell Kotlin to convert all instances of
java.lang.String
to
String?
rather than
String!
within a specified context, so that I get errors or warnings if I try to do (say)
resultset.getString(2).trim()
?
🚫 2
g
"within a specific context", I mean the type system might do it. would using an explicit type
val resultSet: List<String?> = somejavaAPI.getResults()
solve your problem? also, if the java API your getting your
resultset
back from is one you control, then kotlin will respect JSR 305 annotations, so you can download a copy of JSR 305 and use the `Nullable`/`Nonnull` annotations, and the calling kotlin code will stop using the platform type
m
The API is JDBC, so I don't really control it. I guess eventually there will be a new version that has JSR 305 annotations...
a
Kotlin 1.3.40 with its support of external annotations in IDEA might do what you want: https://blog.jetbrains.com/kotlin/2019/06/kotlin-1-3-40-released/. It's not mentioned in the blog post, but Intellij IDEA can infer such annotations automatically for Java libraries, for example the JDK. This will only work in the IDE though.