Reputation: 4368
Here is the content of my groovy file:
def KEY = "a"
Properties myProp = new Properties()
myProp[KEY] = "b"
assert(myProp[KEY] == myProp.getProperty(KEY))
Properties results = new Properties(myProp)
assert(results[KEY] == results.getProperty(KEY))
I expected both asserts to pass but only the first assert passes and the second assert fails.
Any explanation to this is greatly appreciated. Thanks!
Upvotes: 1
Views: 203
Reputation: 160321
So, when the docs say "creates an empty property list", that's what it does:
println(results)
>>> [:]
Check out what getProperty
does:
Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.
Leading to the conclusion that the []
(getAt
) does not search the default property list.
We can pursue this to see how Groovy implements getAt
:
public static <K,V> V getAt(Map<K,V> self, K key) {
return self.get(key);
}
So it's calling the underlying Hashtable
's get
method, which knows nothing about the default property list--defaults are part of Properties
, not Hashtable
:
println(results.getProperty(KEY))
>>> b
println(results.getAt("a"))
>>> null
println(results.get("a"))
>>> null
Is this "correct" behavior? Likely not--maybe a Properties.getAt
would be in order.
Upvotes: 1