Eric Milles created GROOVY-11480:
------------------------------------

             Summary: Cast exception when using qualified this in lambda
                 Key: GROOVY-11480
                 URL: https://issues.apache.org/jira/browse/GROOVY-11480
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation
            Reporter: Eric Milles
            Assignee: Eric Milles


Consider the following:
{code:groovy}
@groovy.transform.CompileStatic
class Test1 {
    class Test2 {
        def m() {
            Optional.empty().orElseGet(() -> Test1.this.n)
        }
    }
    private n = 123
}

assert new Test1.Test2(new Test1()).m() == 123
{code}

The expression "Test1.this.n" is not detected as an instance member access.  
Thus the thisObject reference for the lambda class is set to {{Class<Test2>}} 
and this results in a cast exception at runtime.

{code}
java.lang.ClassCastException: class java.lang.Class cannot be cast to class 
Test1$Test2 (java.lang.Class is in module java.base of loader 'bootstrap'; 
Test1$Test2 is in unnamed module of loader 
groovy.lang.GroovyClassLoader$InnerLoader @7ec68821)
        at Test1$Test2$_m_lambda1.doCall(TestScript85.groovy:5)
        at java.base/java.util.Optional.orElseGet(Optional.java:364)
        at Test1$Test2.m(TestScript85.groovy:5)
{code}



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

Reply via email to