You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@freemarker.apache.org by Albert Kam <mo...@gmail.com> on 2016/05/26 08:55:16 UTC

HashAdapter keySet() is null ?

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)