seltzerl (3) [Avatar] Offline
#1
Listing 9.2 has this assertion:
"assert obj.properties.keySet() == new HashSet(keys)"

This assertion fails because 'third' is not in the set of properties keys, since it's defined with the "public" modifier. If "public" is changed to "def", the assertion succeeds, since third is then a property. This is explained on page 176.

Please let me know if I'm correct here or if I'm overlooking something. BTW, I'm using 1.1-rc-2.

Thanks.

Larry
Mittie (397) [Avatar] Offline
#2
Re: Listing 9.2
Hi,

the book covers Groovy 1.0 in which the property reflection was as shown in listing 9.2. If in Groovy 1.1-rc2 the public field is not recognized as a property, I would consider this a regression error. Please post it to user@groovy.codehaus.org.

thanks for posting
Dierk
seltzerl (3) [Avatar] Offline
#3
Re: Listing 9.2
Dierk,

Ok, I'll post it as you've requested, but I'd like to point out that the book says on page 176 (section 7.1.1)

"When no visibility modifier is attached to [a] field declaration, a property is generated for the respective name."

This implies that the specification of "public" in Listing 9.2 would indeed make "third" not a property. It thus seems like the book is inconsistent and is mixing different releases.

Thanks.

Larry
seltzerl (3) [Avatar] Offline
#4
Re: Listing 9.2
Dierk,

The assertion ( "assert obj.properties.keySet() == new HashSet(keys)" ) fails when listing 9.2 is run in either 1.0 or 1.1-rc2. Here are the (partial) outputs:

-----
1.0:

Exception thrown: java.lang.AssertionError: Expression: (obj.properties.keySet() == new java.util.HashSet(keys))

java.lang.AssertionError: Expression: (obj.properties.keySet() == new java.util.HashSet(keys))

-----
1.1-rc-2:

Exception thrown: java.lang.AssertionError: Expression: (obj.properties.keySet() == new java.util.HashSet(keys))

java.lang.AssertionError: Expression: (obj.properties.keySet() == new java.util.HashSet(keys))
at Script0.run(Script0:11)

-----

The behavior is consistent, so I don't think it's a regression error.

After instantiating the MyClass, "println obj.properties"
results in this ("third" is NOT present in the list):

1.1-rc2:
["metaClass":groovy.lang.MetaClassImpl@139a0dc[class MyClass], "class":class MyClass, "second":2, "first":1]

1.0:
["metaClass":groovy.lang.MetaClassImpl@1382d35[class MyClass], "class":class MyClass, "second":2, "first":1]

I think this should be added to the current list of errata.

BTW, I like the book a lot!

Thanks.

Larry
Mittie (397) [Avatar] Offline
#5
Re: Listing 9.2
Hi,

I have no problem with opening an erratum for that. What puzzles me is that I'm dead sure that the assertion worked before the book went to press. I need a bit more time for investigation.

Thanks for posting and the time you invested into testing all the variants!
Much appreciated
Dierk

Message was edited by:
Mittie