Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 978
Kirie Avatar asked Nov 23 '15 19:11

Kirie


1 Answers

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()

like image 150
tim_yates Avatar answered Sep 25 '22 14:09

tim_yates