27 May 2004 17:39
Re: [groovy-dev] patch for Object.eachPropertyName()
On 20 May 2004, at 22:02, John Stump wrote:
> James, another questions:
>
> in MetaClass.setProperty(), there is some code in a catch that
> looks something like this:
>
> try {
> doMethodInvoke(object, metaMethod, arguments);
> }
> catch (GroovyRuntimeException e) {
> // if the value is a List see if we can
> construct the value
> // from a constructor
> if (newValue instanceof List) {
> ...
>
> // if value is an multidimensional array
> if (newValue.getClass().isArray()) {
> ...
>
> I'm not really sure what this code does, but the first case
> seems like some convenience code to create an object by
> implicitly calling a constructor? Like this:
>
> class Bean {
> SomeClass propx
> }
>
> // this is the same as
> // bean.propx = new SomeClass(1, "hi")
> bean.propx = [1, "hi"]
>
> I don't remember this being a documented feature, nor can I see
> any real advantage for it. Am I missing something?
You're gues is exactly right. Its very handy with Swing based stuff,
like when you want to set colours, size, dimensions and the like.
e.g.
frame = new JFrame(size:[10, 20])
rather than
frame = new JFrame(size:new Dimension(10, 20))
> And the 2nd case is convenience for coverting a List to an
> multi-dimensional array?
Yep
> If these are right, are they really needed or are they holdover
> code from a previous design?
Its mostly helper converters so that folks can set properties on beans
without things being too pedantic on whether things are lists or arrays
or little helper types like Dimension, Point, Colour etc.
James
-------
http://radio.weblogs.com/0112098/
RSS Feed