考虑这个例子:
import java.math.BigDecimal
fun main(args: Array<String>) {
val s: String? = null
BigDecimal(s)
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有任何警告(使用 clikotlinc
和 IntelliJ)并在运行时抛出:
Exception in thread "main" java.lang.NullPointerException
at java.math.BigDecimal.<init>(BigDecimal.java:809)
...
Run Code Online (Sandbox Code Playgroud)
我尝试根据文档以不同的方式构建它,如下所示:
kotlinc \
-Xnullability-annotations=@javax.annotation:strict \
-Xnullability-annotations=@org.jetbrains.annotations:strict \
-Xjsr305=strict \
Main.kt -d main.jar
Run Code Online (Sandbox Code Playgroud)
注释似乎在那里(如 IntelliJ 中所示):
文件明确指出:
具有可空性注释的 Java 类型不会表示为平台类型,而是表示为实际的可空或非空 Kotlin 类型。
我在这里缺少什么?
构造函数的参数BigDecimal
被视为平台类型,因为构造函数实际上没有注释。
注释@NotNull
是由 IDE 通过检查源代码即时计算的。如果您按住 ctrl+单击构造函数,您将转到源代码,您将看到没有注释。
归档时间: |
|
查看次数: |
348 次 |
最近记录: |