Kirie
Kirie

Reputation: 93

Groovy: getClass method on map literal returns null

In Groovy I use the map literal notation quite frequently in my code, and was curious as to what concrete implementation of Map it was.

After trying a few things, this script best illustrates my confusion:

def map = ["A":"B"]
println map // I assume this avoids any lazy evaluation of the map
println map instanceof HashMap // I tried some other impls too
println map.class

and receive this output:

[A:B]
true
null

This tells me that the map is apparently a HashMap, but the getClass method doesn't want to tell me that.

So my question is: why is getClass returning null, and is there a more appropriate way to get runtime class info from Groovy?

Upvotes: 7

Views: 1317

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You need to use

map.getClass()

As otherwise it's looking for a key called class

Nearly a duplicate of Why does groovy .class return a different value than .getClass()

Upvotes: 9

Related Questions