java.util.LinkedHashMap$Entry vs. java.util.AbstractMap$SimpleImmutableEntry

114 views
Skip to first unread message

Jochen Benke

unread,
Nov 28, 2018, 9:05:54 AM11/28/18
to SnakeYAML
Hello,

if have created a groovy routine to replace values in a YAML document. If I run this as a groovy script or as a Jenkins FreeStyle Project things worked well. If I run this in Jenkins Pipeline Script the leaf element turns from java.util.LinkedHashMap$Entry to java.util.AbstractMap$SimpleImmutableEntry and setValue isn't available anymore. How can configure this behaviour? Do not find anything about this topic same jar sam code.

Thank you very much in advance.

Cheers Jochen

Yaml parser = new Yaml()
def deploymentYaml = parser.load(('file' as File).text)

replaceLeafValueInYAML(deploymentYaml, "Root", null, "namespace", "app-dev")

def replaceLeafValueInYAML(childs, position, nodename, argKey, argValue) {
    if(nodename != null) {
        position = position + "." + nodename
    }

    childs.each {element ->
        def value = null
        def key = null

        if(element instanceof java.util.LinkedHashMap) {
            value = element
        } else if(element instanceof java.lang.String){
            value = element
        } else {
            key = element.getKey();
            value = element.getValue();
        }

        if(value instanceof java.util.LinkedHashMap) {
            replaceLeafValueInYAML(value, position, key, argKey, argValue)
        } else if(value instanceof java.util.ArrayList) {
            replaceLeafValueInYAML(value, position, key + "[]", argKey, argValue)
        } else {
            def leafPosition = position + "." + key

            if(leafPosition.endsWith(argKey)) {
                println element.getClass()
               
                element.setValue(argValue)

                println "New value of key " + leafPosition + "=" + argValue
            }
        }
    }
}


[Pipeline] End of Pipeline
java.lang.UnsupportedOperationException
	at java.util.AbstractMap$SimpleImmutableEntry.setValue(AbstractMap.java:797)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
	at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCa

Andrey Somov

unread,
Nov 28, 2018, 2:21:55 PM11/28/18
to snakeya...@googlegroups.com
Well, it looks like Groovy uses its own collections.

Andrey

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeyaml-cor...@googlegroups.com.
To post to this group, send email to snakeya...@googlegroups.com.
Visit this group at https://groups.google.com/group/snakeyaml-core.
For more options, visit https://groups.google.com/d/optout.

Jochen Benke

unread,
Nov 29, 2018, 3:27:40 AM11/29/18
to SnakeYAML
Hi,  it's bothe groovy. But plain groovy creates an mutable tree and groovy in Pipeline creates an immutable tree. WHY?? Is there any setting in snakeyaml to controll this?

Andrey Somov

unread,
Nov 29, 2018, 3:53:54 AM11/29/18
to snakeya...@googlegroups.com
To control WHAT ?

SnakeYAML is not aware of the Groovy data structures.
SnakeYAML has the ability to explicitly select the collection (which List implementation to choose). I am afraid someone is using it to instantiate Groovy collections.

Andrey

Jochen Benke

unread,
Nov 29, 2018, 3:58:56 AM11/29/18
to SnakeYAML
Sorry, perhaps my english is not so perfect. That what I mean, How I can configure the list implementation in SnakeYAML to avoid using those immutable Entries. This would help. Thank you very much. Cheers.

Andrey Somov

unread,
Nov 29, 2018, 4:11:02 AM11/29/18
to snakeya...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages