Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.4.8, 2.4.9, 2.4.10, 2.4.11, 2.4.12
-
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
- is duplicated by
-
GROOVY-7515 static initializers on trait fields are discarded
- Closed
- links to