Groovy: getClass method on map literal returns null

In Groovy, I often use literal literature quite often in my code, and I was curious what specific implementation of Map was.

After several attempts, 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 get this output:

 [A:B] true null 

This tells me that the map is apparently a HashMap, but the getClass method does not want to tell me this.

So my question is: why does getClass return null, and is there a better way to get execution class information from Groovy?

+5
source share
1 answer

You need to use

 map.getClass() 

Otherwise, it searches for a key named class

Almost a duplicate Why groovy.class returns a different value than .getClass ()

+7
source

Source: https://habr.com/ru/post/1236644/


All Articles