Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-11480

Cast exception when using qualified this in lambda

    XMLWordPrintableJSON

Details

    Description

      Consider the following:

      @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
      

      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.

      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)
      

      Attachments

        Activity

          People

            emilles Eric Milles
            emilles Eric Milles
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: