freemarker-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Albert Kam <moonblade.w...@gmail.com>
Subject HashAdapter keySet() is null ?
Date Thu, 26 May 2016 08:55:16 GMT
Hi,

I'm experimenting on passing hash or a seq of hashes to the java code.
I tried doing keySet() from the hash or list of hashes passed as
HashAdapter type, and found out that it results in null.

I had to 'wrap' the hashAdapter into another map through guava to get
a normal map with the correct keySet() behavior.

I wonder if it's the normal behavior ?

Example of ftl:

    <@testParameters />

    <#macro testParameters>
        ${proc.debug('hello world')}
        ${proc.debugMap({
            'msg': 'hello world'
        })}
        ${proc.debugMaps(
            [
                { 'msg': 'hello world' },
                { 'msg': 'hello world2' },
                { 'msg': 'hello world3' }
            ]
        )}
    </#macro>

And the code of proc in the model / context:

    public static class DataProcessor {
        public void debug(String str) {
            System.out.println(str);
        }

        public void debugMap(Map<String, Object> map) {
            System.out.println(JsonUtil.prettyString(map));
            System.out.println(JsonUtil.prettyString(map.keySet())); // null
            System.out.println(JsonUtil.prettyString(Maps.newHashMap(map).keySet()));
// ok
        }

        public void debugMaps(List<Map<String, Object>> maps) {
            System.out.println(JsonUtil.prettyString(maps));
            System.out.println(JsonUtil.prettyString(maps.get(0).keySet()));
// null
            System.out.println(JsonUtil.prettyString(Maps.newHashMap(maps.get(0)).keySet()));
// ok
        }
    }

Thank you

-- 
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

Mime
View raw message