Setya | 4 Sep 12:18

[groovy-user] Throwing exception when closure parameter is not given


Hi all,

I'm attaching dynamic method to an Expando here:

def Expando exp = new Expando();
exp.bind
{ fieldName ->

   if (!fieldName || fieldName.trim() == '')
       throw new Exception('Field name is required.');
}

The above snippet works well if I call the method with syntax "bind
'fieldname'" or "bind()", but if I call it using only "bind" no exception
was thrown.

Any ideas ?

Setya

--

-- 
View this message in context: http://www.nabble.com/Throwing-exception-when-closure-parameter-is-not-given-tp19307480p19307480.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

(Continue reading)

Jochen Theodorou | 4 Sep 13:49
Gravatar

Re: [groovy-user] Throwing exception when closure parameter is not given

Setya schrieb:
> Hi all,
> 
> I'm attaching dynamic method to an Expando here:
> 
> def Expando exp = new Expando();
> exp.bind
> { fieldName ->
> 
>    if (!fieldName || fieldName.trim() == '')
>        throw new Exception('Field name is required.');
> }
> 
> The above snippet works well if I call the method with syntax "bind
> 'fieldname'" or "bind()", but if I call it using only "bind" no exception
> was thrown.
> 
> Any ideas ?

exp.bind will return the closure itself. Remember, that Expando is based 
on properties.... you can add them and change them. And each property 
can work as method as well... but primary they are still properties. If 
you now say I don't want to have properties, I want to have methods, 
then we would have to write a different Expando or subclass Expando... 
which is not difficult to do... I am using code that won't do in 1.0... 
which could easily changed, but to keep it shorter:

> class MyExpando {
> 
>     private Map expandoProperties
(Continue reading)

Setya | 5 Sep 07:04

Re: [groovy-user] Throwing exception when closure parameter is not given


Hi,

> class MyExpando {
> 
>     private Map expandoProperties
>     
>     MyExpando() {
>       expandoProperties = new HashMap()
>     }
> 
>     MyExpando(Map expandoProperties) {
>         this.expandoProperties = expandoProperties
>     }
> 
>     def propertyMissing(String name) {
>         def result = expandoProperties[name]
>         if (result instanceof Closure) return result()
>         return result
>     }
> 
>     void propertyMissing(String name, newValue) {
>         expandoProperties[name] = newValue
>     }
> 
>     def methodMissing(String name, args) {
>         def result = expandoProperties[name]
>         if (result instanceof Closure) return result(*args)
>         return result       
>     }
(Continue reading)

Jochen Theodorou | 5 Sep 14:03
Gravatar

Re: [groovy-user] Throwing exception when closure parameter is not given

Setya schrieb:
[...]
> Since I don't want to maintain yet another Groovy file for MyExpando, is it
> possible if I just override existing Expando methods ?

wouldn't that lead to the same... you can of course also make a class 
written in Java out of MyExpando. Besides that you have to extend 
GroovyObject then it would look almost the same.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane