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

Final field is null in trait, but it has assigned value

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.8, 2.4.9, 2.4.10, 2.4.11, 2.4.12
    • 2.5.1
    • None
    • None

    Description

      I have a code like this:

      trait MyTrait {
      	final String myValue = "test"
      	final Wrapper wrapper = new Wrapper(myValue)
      }
      
      class Wrapper {
      	Wrapper(String value){
      		if(value == null) {
      			throw new Exception("Value is null!!!")	
      		}
      		println "OK: $value"
      	}
      }
      
      class Main implements MyTrait {}
      
      new Main()
      

      Everything works as expected with groovy 2.4.7:

      $ sdk u groovy 2.4.7
      
      Using groovy version 2.4.7 in this shell.
      $ groovy bug.groovy 
      OK: test
      

      but newer versions of groovy do not compile this code (2.4.8, 2.4.9, 2.4.10) or have null to value (2.4.11, 2.4.12):

      $ sdk u groovy 2.4.8
      
      Using groovy version 2.4.8 in this shell.
      $ groovy bug.groovy 
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      ~/bug.groovy: 3: Apparent variable 'myValue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
      You attempted to reference a variable in the binding or an instance variable from a static context.
      You misspelled a classname or statically imported field. Please check the spelling.
      You attempted to use a method 'myValue' but left out brackets in a place not allowed by the grammar.
       @ line 3, column 38.
         	final Wrapper wrapper = new Wrapper(myValue)
                                              ^
      
      1 error
      
      $ sdk u groovy 2.4.9
      
      Using groovy version 2.4.9 in this shell.
      $ groovy bug.groovy 
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      ~/bug.groovy: 3: Apparent variable 'myValue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
      You attempted to reference a variable in the binding or an instance variable from a static context.
      You misspelled a classname or statically imported field. Please check the spelling.
      You attempted to use a method 'myValue' but left out brackets in a place not allowed by the grammar.
       @ line 3, column 38.
         	final Wrapper wrapper = new Wrapper(myValue)
                                              ^
      
      ~/bug.groovy: 3: Apparent variable 'myValue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
      You attempted to reference a variable in the binding or an instance variable from a static context.
      You misspelled a classname or statically imported field. Please check the spelling.
      You attempted to use a method 'myValue' but left out brackets in a place not allowed by the grammar.
       @ line 3, column 38.
         	final Wrapper wrapper = new Wrapper(myValue)
                                              ^
      
      2 errors
      
      $ sdk u groovy 2.4.10
      
      Using groovy version 2.4.10 in this shell.
      $ groovy bug.groovy 
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      ~/bug.groovy: 3: Apparent variable 'myValue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
      You attempted to reference a variable in the binding or an instance variable from a static context.
      You misspelled a classname or statically imported field. Please check the spelling.
      You attempted to use a method 'myValue' but left out brackets in a place not allowed by the grammar.
       @ line 3, column 38.
         	final Wrapper wrapper = new Wrapper(myValue)
                                              ^
      
      ~/bug.groovy: 3: Apparent variable 'myValue' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
      You attempted to reference a variable in the binding or an instance variable from a static context.
      You misspelled a classname or statically imported field. Please check the spelling.
      You attempted to use a method 'myValue' but left out brackets in a place not allowed by the grammar.
       @ line 3, column 38.
         	final Wrapper wrapper = new Wrapper(myValue)
                                              ^
      
      2 errors
      
      $ sdk u groovy 2.4.11
      
      Using groovy version 2.4.11 in this shell.
      $ groovy bug.groovy 
      Caught: java.lang.Exception: Value is null!!!
      java.lang.Exception: Value is null!!!
      	at Wrapper.<init>(bug.groovy:9)
      	at MyTrait$Trait$Helper.$init$(bug.groovy:3)
      	at Main.<init>(bug.groovy)
      	at bug.run(bug.groovy:17)
      $ sdk u groovy 2.4.12
      
      Using groovy version 2.4.12 in this shell.
      $ groovy bug.groovy 
      Caught: java.lang.Exception: Value is null!!!
      java.lang.Exception: Value is null!!!
      	at Wrapper.<init>(bug.groovy:9)
      	at MyTrait$Trait$Helper.$init$(bug.groovy:3)
      	at Main.<init>(bug.groovy)
      	at bug.run(bug.groovy:17)
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              alien11689 Dominik Przybysz
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: