Dominik Przybysz created GROOVY-8281:
----------------------------------------

             Summary: Final field is null in trait, but it has value
                 Key: GROOVY-8281
                 URL: https://issues.apache.org/jira/browse/GROOVY-8281
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 2.4.12, 2.4.11, 2.4.10, 2.4.9, 2.4.8
            Reporter: Dominik Przybysz


I have a code like this:

{code}
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()
{code}

Everything works as expected with groovy 2.4.7:

{code}
$ sdk u groovy 2.4.7

Using groovy version 2.4.7 in this shell.
$ groovy bug.groovy 
OK: test
{code}

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):

{code}
$ 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)
{code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to