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

STC: closure property precedence

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0.21, 5.0.0-alpha-8, 4.0.21
    • 5.0.0-alpha-9
    • Static Type Checker
    • None

    Description

      Properties within a closure are satisfied first against the closure followed by the resolve strategy. This is implemented by: https://github.com/apache/groovy/blob/0e8718d6bafba61daf962892c9d7ae10832b62e0/src/main/java/groovy/lang/Closure.java#L280

      The type checker supports this here: https://github.com/apache/groovy/blob/0e8718d6bafba61daf962892c9d7ae10832b62e0/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java#L648

      The properties "class" and "metaClass" are missing from the switch. "class" is not possible as a variable. However, "metaClass" is and should refer to the Closure's meta class, not the delegate or owner's meta class. This can be checked by running:

      @groovy.transform.TypeChecked
      void test(object) {
        object.with {
          print metaClass
        }
      }
      test("")
      

      This script should print something like "org.codehaus.groovy.runtime.metaclass.ClosureMetaClass@17d2ed1b[class Sample$_test_closure1]".

      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: