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

Can't access private static constant from closure, when using @CompileStatic

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.8, 2.1.3
    • 2.1.7, 2.2.0-beta-2
    • Static compilation
    • None
    • JDK1.7

    Description

      Can not access private static constant from colosure with @CompileStatic
      Code:

      @groovy.transform.CompileStatic
      class A{
        private static final int BASE = 10
        
        void test() {
            def c = { 
                    println BASE
            }
            
            c()
        }  
      }
      
      new A().test()
      

      Output(Without @CompileStatic)

      10
      

      Output(With @CompileStatic)

      1 compilation error:
      
      Access to A#BASE is forbidden at line: -1, column: -1
      

      But this code can work(wrap another closure)

      @groovy.transform.CompileStatic
      class A{
        private static final int BASE = 10
        
        void test() {
            def c = { 
                def c1 = {
                    println BASE
                }
                c1()
            }
            
            c()
        }  
      }
      
      new A().test()
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            lusxiao Eddie Lu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: