You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Keegan Witt (JIRA)" <ji...@apache.org> on 2015/08/25 22:09:46 UTC

[jira] [Updated] (GROOVY-7558) Error when referencing private member variables from within a closure

     [ https://issues.apache.org/jira/browse/GROOVY-7558?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Keegan Witt updated GROOVY-7558:
--------------------------------
    Description: 
Originally posted on SO here: http://stackoverflow.com/questions/32212260/in-groovy-is-it-legal-to-reference-private-member-variables-from-within-a-closur

The code below fails with {{java.lang.ClassCastException: Test$_doStuff_closure1 cannot be cast to Test}}.  Interestingly, if I remove the {{@CompileStatic}} annotation or make the member variable non-private it works as expected.

{code:java}
class Test {
    private String str = "hi"
    @groovy.transform.CompileStatic
    public void doStuff() {
        Closure c = {
            println str
        }
        c()        
    }
}

new Test().doStuff()
{code}

It also works when {{@CompileStatic}} is moved up to the class level

{code:java}
@groovy.transform.CompileStatic
class Test {
    private String str = "hi"
    public void doStuff() {
        Closure c = {
            println str
        }
        c()        
    }
}

new Test().doStuff()
{code}

  was:
Originally posted on SO here: http://stackoverflow.com/questions/32212260/in-groovy-is-it-legal-to-reference-private-member-variables-from-within-a-closur

The code below fails with `java.lang.ClassCastException: Test$_doStuff_closure1 cannot be cast to Test`.

Interestingly, if I remove the @CompileStatic annotation or make the member variable non-private it works as expected.

// ---------- Begin Code ---------- \\
import groovy.transform.CompileStatic

class Echo {
    public void text(String txt) {
        println txt
    }
}

class Test {
    private Echo echo = new Echo()
    @CompileStatic
    public void doStuff() {
        Closure c = {
            echo.text('hi')
        }
        c()        
    }
}

new Test().doStuff()


> Error when referencing private member variables from within a closure
> ---------------------------------------------------------------------
>
>                 Key: GROOVY-7558
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7558
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.4.4
>         Environment: Linux x86-64 / java 1.8.0_60
>            Reporter: Jon Keys
>            Assignee: Cédric Champeau
>
> Originally posted on SO here: http://stackoverflow.com/questions/32212260/in-groovy-is-it-legal-to-reference-private-member-variables-from-within-a-closur
> The code below fails with {{java.lang.ClassCastException: Test$_doStuff_closure1 cannot be cast to Test}}.  Interestingly, if I remove the {{@CompileStatic}} annotation or make the member variable non-private it works as expected.
> {code:java}
> class Test {
>     private String str = "hi"
>     @groovy.transform.CompileStatic
>     public void doStuff() {
>         Closure c = {
>             println str
>         }
>         c()        
>     }
> }
> new Test().doStuff()
> {code}
> It also works when {{@CompileStatic}} is moved up to the class level
> {code:java}
> @groovy.transform.CompileStatic
> class Test {
>     private String str = "hi"
>     public void doStuff() {
>         Closure c = {
>             println str
>         }
>         c()        
>     }
> }
> new Test().doStuff()
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)