Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Fixed
-
2.3.6
-
None
Description
The following code produces NullPointerException when compiled static, but operates as expected when running in dynamic groovy:
import groovy.transform.CompileStatic @Grapes( @Grab(group='org.springframework.security', module='spring-security-core', version='3.2.5.RELEASE') ) import org.springframework.security.core.context.SecurityContextHolder @CompileStatic def checkSafeNavi() { println SecurityContextHolder //class org.springframework.security.core.context.SecurityContextHolder println SecurityContextHolder.context //org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication println SecurityContextHolder.context?.authentication //null println SecurityContextHolder.context?.authentication?.name //NullPointerException } checkSafeNavi()
Thanks to Thibault Kruse for producing a minimal reproducible version.
Here is another test case that doesn't use Spring Security or any other dependency. This hopefully should be able to be transformed directly into a unit test for Groovy:
//If Y was a class implementing YParent, it works //If Y had the getValue method directly, it works //If Y redeclares (overrides) getValue, it works interface Y extends YParent { } interface YParent { int getValue() } @groovy.transform.CompileStatic def check() { Y y = null println y?.value } check()