从Kotlin调用不接受null的Java代码 [英] Calling java code that doesn't accept null from kotlin

查看:53
本文介绍了从Kotlin调用不接受null的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDEA社区2017.1.2,JRE 1.8,Kotlin 1.1.2-2

IDEA Community 2017.1.2, JRE 1.8, Kotlin 1.1.2-2

我在libGdx中有Java方法,这些方法没有关于其可为空性的任何注释,例如:

I have Java methods, located in libGdx that don't have any annotations regarding their nullability, e.g.:

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

如我们所见,

参数不能为null.但是,由于没有任何迹象表明它不是null参数,因此Kotlin会很乐意在 RenderableProvider?中传递null.我如何告诉Kotlin在编译时检查是否应该传递 RenderableProvider 而不是 RenderableProvider??

as we can see, argument can't be null. However, since nothing tells that it's not-null argument, Kotlin will happily pass null in RenderableProvider?. How do I tell Kotlin to check during compile-time that I should be passing RenderableProvider and not RenderableProvider??

我已经阅读了外部注释,但是没有指定自定义Kotlin签名",如果我将 renderableProvider 注释为 @NotNull ,则没有任何变化-Kotlin仍然允许null.

I've read about external annotations, however there is no "Specify Custom Kotlin Signature" and if I annotate renderableProvider as @NotNull nothing changes - kotlin still allows null.

我什至尝试用XML中的 javax.annotation.Nonnull 手动替换 org.jetbrains.annotations.NotNull ,但这没什么区别-代码编译并崩溃NPE.

I even tried to replace org.jetbrains.annotations.NotNull with javax.annotation.Nonnull in XML manually, but it makes no difference - code compiles and crashes with NPE.

推荐答案

外部注释为

External annotations are no longer supported. You'll either have to fork libgdx and annotate the methods there or live with this issue, unfortunately.

这篇关于从Kotlin调用不接受null的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆