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

Builders behave strange when closures throw MissingMethodException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 2.3.7, 2.4.0-rc-2, 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4
    • None
    • None
    • None

    Description

      I noticed some strange behavior when closures inside a builder throw exceptions:

      import groovy.transform.TypeChecked
      
      @TypeChecked
      class TestBuilder extends BuilderSupport {
      
        Object createNode(Object name, Map map){
      
          println "Create node $name: $map"
          new Node(null, name, map)
        }
      
        Object createNode(Object name){
          println "Create node $name"
          new Node(null, name)
        }
      
        Object createNode(Object name, Object value){
          println "Create node $name, value $value"
          new Node(null, name, value)
        }
      
        Object createNode(Object name, Map map, Object value){
          println "Create node $name: $map, value $value"
          new Node(null, name, map, value)
        }
      
        void setParent(Object parent, Object child){
          println "set parent of $child to $parent"
          ((Node)parent).append( (Node) child)
        }
      }
      
      def builder = new TestBuilder()
      
      // First case
      // expected: MissingMethodException tring to call String.foo()
      // actual: MissingMethodException tring to call b(Closure)
      
      try {
        builder.a {
          b {
            error('xy'.foo())
          }
        }
      }catch (e){
        assert e instanceof MissingMethodException
        assert e.method == "foo"
      }
      
      // Second case
      // expected: one call to buidler.createNode('b')
      // actual: two calls to buidler.createNode('b')
      builder.with {
        a {
          b {
            error("xy".foo())
          }
        }
      }
      

      Attachments

        1. groovy-7156.zip
          50 kB
          Jochen Kemnade

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jkemnade Jochen Kemnade
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: