Calling java code that does not accept null from kotlin

IDEA Community 2017.1.2, JRE 1.8, Kotlin 1.1.2-2

I have Java methods located in libGdx that don't have any comments regarding their null resistance, for example:

public void render (final RenderableProvider renderableProvider) {
  renderableProvider.getRenderables(renderables, renderablesPool);

      

as we can see, the argument cannot be null. However, since there is no indication that this is a non-empty argument, Kotlin will happily pass null to RenderableProvider?

. How do I tell Kotlin to check at compile time that I should go through RenderableProvider

and not RenderableProvider?

?

I read the external annotations however there is no "Specify kotlin custom signature" and if I annotate RenderableProvider

as @NotNull

nothing changes - kotlin still allows null.

I even tried to replace org.jetbrains.annotations.NotNull

by javax.annotation.Nonnull

in XML manually, but it doesn't matter - code compilation and NPE crashes.

+3


source to share


2 answers


External annotations are no longer supported . You will either have to fork libgdx and comment on methods there or live with this problem unfortunately.



+3


source


You can wrap it in an extension function and then use it for rendering only:

fun RenderClass.renderSafe(renderProvider: RenderableProvider) = 
    this.render(renderProvider)

      



Now you cannot get through null

.

+1


source







All Articles