Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7075

Safe Navigation operator does not perform null check with @CompileStatic on interface type not declaring getter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.3.6
    • 2.3.7
    • Static compilation
    • 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()
      

      Attachments

        Activity

          People

            melix C├ędric Champeau
            gillius Jason Winnebeck
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: