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

Paul King commented on GROOVY-9695:
-----------------------------------

The following passes with the fix in GROOVY-9759 (using {{@CompileStatic}}):
{code}
import java.util.regex.Pattern
import org.junit.Test
import groovy.transform.CompileStatic

class Example {
    @Test
    void testBaseClass() {
        assert new Base().check("FooTest") == 'FooTest'
    }

    @Test
    void testChildClass() {
        assert new Child().check("FooSpec") == 'FooSpec'
    }

    @Test(expected = IllegalArgumentException)
    void testChildClassFail() {
        new Child().check("Dummy")
    }
}

class Base {
    private static final List<Pattern> PATTERNS = [~/.*Test/, ~/.*Spec/]

    @CompileStatic
    def check(String str) {
        Map<String, String> failures = [:]

        def result = PATTERNS.findResult { Pattern pattern ->
            if (pattern.matcher(str).matches()) {
                return str
            }
            failures["${PATTERNS.indexOf(pattern)}".toString()] = 
pattern.pattern()
            null
        }
        if (!result) {
            throw new IllegalArgumentException("Matched no pattern: " +
                failures.collect{k, v -> "$k: $v" }.join(", "))
        }

        return result
    }
}

class Child extends Base { }
{code}
JUnit 4 Runner, Tests: 3, Failures: 0, Time: 4


> Regression for accessing private static constants in closures from Parent 
> Classes
> ---------------------------------------------------------------------------------
>
>                 Key: GROOVY-9695
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9695
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Leonard Brünings
>            Priority: Major
>
> The following code worked in 2.5.12, in 2.5.13 {{testChildClass}} fails with 
> {{groovy.lang.MissingPropertyException: No such property: PATTERNS for class: 
> Child}}
> [~emilles] probably related to the changes mentioned here GROOVY-9665
> {code:groovy}
> class Example {
>     @Test
>     void testBaseClass() {
>         new Base().check("FooSpec")
>     }
>     @Test
>     void testChildClass() {
>         new Child().check("FooSpec")
>     }
> }
> class Base {
>     private static final List<Pattern> PATTERNS = [~/.*Test/, ~/.*Spec/]
>     def check(String str) {
>         List failures = []
>         def result = PATTERNS.findResult { Pattern pattern ->
>             if (pattern.matcher(str).matches()) {
>                 return str
>             } else {
>                 failures.add("Pattern ${PATTERNS.indexOf(pattern)} did not 
> match")
>             }
>         }
>         if (!result) {
>             throw new IllegalArgumentException("Did Match no pattern: 
> "+failures.join(", "))
>         }
>         return result
>     }
> }
> class Child extends Base {
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to