jastrachan | 14 May 20:13 2004

Re: [groovy-dev] patch for Object.eachPropertyName()

On 14 May 2004, at 18:24, John Stump wrote:
> Hi James,
> I have some early code working:
> 1) I defined a new method in MetaClass called getProperties().
> This returns a List of MetaProperty objects.
> 2) I created a new class in groovy.lang called PropertyValue.
> 3) I defined a new method in DefaultGroovyMethods called
> properties(). This returns a List of PropertyValue objects.

Who-hoo! Looks great!

[snip cools stuff]

> Couple of comments and questions:
> 1) Notice that "metaClass" and "class" show up. Should these be
> filtered out?

I guess not; they show up in Java introspection too.

> 2) Types of properties that are not explicitly declared with a
> type come out as Objects, regardless of what value they hold. Is
> this ok? Seems like the type should reflect the current value.

I think the type of the property. Folks can always do value.class to 
get the real implementation type.

i.e. its the constraint of type on all instances of that property. e.g. 
if you wanted to do (say) tabular formatting of rows of data, you'd 
wanna know the numeric type properties - even though the value might be 

> 3) Notice that privates are not listed. Should they be?


> 4) Currently, I am only dealing with proper bean properties
> here; I'm not yet handling the other ways properties and fields
> can exist. I'm a little fuzzy on all this, and the code
> indicates there are numerous mechanisms for getting and setting
> properties. Do I need to look for get*() methods that have no
> parameters to determine other properties? What about the
> "generic" get stuff? You also mention public fields. How do you
> get these for a POGO? Or is this strictly for Java objects?

The code is still a bit fuzzy in this area too :)

I guess the logic to build the list of properties should be something 
like this...

* find all the public fields & add them as a MetaProperty (we can 
implement a special MetaFieldProperty that uses reflection to get/set 
the field value)

* find all properties via introspection & if they don't clash with 
above add them

* do a quick check of any gettter methods with no args & setter methods 
with 1 arg that the introspector didn't get and if there isn't already 
a MetaProperty for this name of property, add a new one. There are 
various bad beans in the JDK which confuse the introspector, so we need 
this little hack to get around this issue.

Right now we do the latter part lazily on demand if we can't find a 
PropertyDescriptor for a name. Doing this up front when we create a 
MetaClass is much better - then the getProperty() / setProperty() can 
do a fast lookup for the MetaProperty to boost performance a little.