groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Kemnade (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-7156) Builders behave strange when closures throw MissingMethodException
Date Wed, 22 Jul 2015 14:17:05 GMT

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

Jochen Kemnade commented on GROOVY-7156:
----------------------------------------

I think I've got a fix: Inside {{groovy.util.BuilderSupport.doInvokeMethod(String, Object,
Object)}}, wrap the {{closure.call();}} in a try-catch block and re-throw the exception wrapped
in a {{GroovyRuntimeException}}. I'l see if I can come up with a test and a patch.

> Builders behave strange when closures throw MissingMethodException
> ------------------------------------------------------------------
>
>                 Key: GROOVY-7156
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7156
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.3.7, 2.4.0-rc-2, 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4
>            Reporter: Jochen Kemnade
>         Attachments: groovy-7156.zip
>
>
> I noticed some strange behavior when closures inside a builder throw exceptions:
> {code}
> 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())
>     }
>   }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message