You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Pavel Lobodinský (Jira)" <ji...@apache.org> on 2024/04/12 12:06:00 UTC

[jira] [Comment Edited] (GROOVY-11144) MissingPropertyEx when accessing private static field in closure of same class

    [ https://issues.apache.org/jira/browse/GROOVY-11144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17836576#comment-17836576 ] 

Pavel Lobodinský edited comment on GROOVY-11144 at 4/12/24 12:05 PM:
---------------------------------------------------------------------

I'm also facing lots of issues with Spock tests that only support Groovy 4. Even the simple code below does not work in plain Groovy 4.x.
{code:groovy}
class StaticPropertyIssueTest {
    static def STR = "test"

    static void main(String[] args) {
        def dataClass = new DataClass(value: "test")

        dataClass.with {
            assert value == STR
        }
    }
}

class DataClass {
    String value
    String get(String str) { "Hello" }
} {code}


was (Author: JIRAUSER304862):
I'm also facing lots of issues with Spock tests that only support Groovy 4. Even the simple code below does not work in plain Groovy.
{code:groovy}
class StaticPropertyIssueTest {
    static def STR = "test"

    static void main(String[] args) {
        def dataClass = new DataClass(value: "test")

        dataClass.with {
            assert value == STR
        }
    }
}

class DataClass {
    String value
    String get(String str) { "Hello" }
} {code}

> MissingPropertyEx when accessing private static field in closure of same class
> ------------------------------------------------------------------------------
>
>                 Key: GROOVY-11144
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11144
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 4.0.13
>         Environment: Windows 10, Groovy 4.0.13, Java 17.0.8
>            Reporter: Andreas Turban
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 5.0.0-alpha-1
>
>         Attachments: Test.groovy
>
>
> When accessing a private static final field from a Closure within the same class Groovy 4 throws a MissingPropertyException. The code works in Groovy 2.5.22 & 3.0.18.
> The real instance of the object needs to be a subtype of the class containing the constant.
> See attached code to reproduce.
> *Expected behavior:*
> The code runs without error
> *Actual behavior:*
> Groovy 4 throws:
> {{groovy.lang.MissingPropertyException: No such property: field for class: FirstEvent}}
> {{    at BaseEvent$_init_closure1.doCall(Test.groovy:7)}}
> {{    at BaseEvent.init(Test.groovy:7)}}
> {{    at Test.run(Test.groovy:15)}}
> *Notes:*
> Interestingly if you remove the static keyword from {color:#0033b3}private static final {color}{color:#000000}String {color}{color:#871094}field {color}= {color:#067d17}"name"{color}
> it fails in all three versions 2.5.22, 3.0.18 & 4.0.13 with a {{{}MissingPropertyException{}}}.
> I would expect that the code should also work for these cases.
>  
> The problem came up in a discussion about a Spock issue [#1544|https://github.com/spockframework/spock/issues/1544].



--
This message was sent by Atlassian Jira
(v8.20.10#820010)