@CompileStatic 给出 NullPointerException

@CompileStatic gives NullPointerException

为什么仅使用@CompileStatic 进行注释会使下面的代码给出 NullPointerException?

class GroovyEach {
    static def main(args) {
        List items = null

        items.each {
            println 'hello'
        }

    }
}

下面的代码给出了异常。

import groovy.transform.CompileStatic

@CompileStatic
class GroovyEach {
    static def main(args) {
        List items = null

        items.each {
            println 'hello'
        }

    }
}

堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1372)
    at trial.GroovyEach.main(GroovyEach.groovy:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

这是旧 question 的倒数。静态编译时,items 的类型为 List,未静态编译时,类型为 NullObject,它以空安全方式检索迭代器。这很容易证明。

这个有效

class GroovyEach {
    static void main(String[] args) {
        List items = null
        (org.codehaus.groovy.runtime.NullObject) items
    }
}

失败 [Static type checking] - Inconvertible types: cannot cast java.util.List to org.codehaus.groovy.runtime.NullObject

@groovy.transform.CompileStatic
class GroovyEach {
    static void main(String[] args) {
        List items = null
        (org.codehaus.groovy.runtime.NullObject) items
    }
}