1

I'm new to Groovy, but I am trying to write a simple recursive method to parse hierarchical paths into an object graph. Here is what I've tried:

class Hierarchy {
    def root = [:]

    static void processHierarchy(names, parent) {
        println names
        if (names.size() > 0) {
            def childName = names[0]
            def child = parent[childName]
            if (child == null){
                child = new Expando()
                parent[childName]=  child
            }
            processHierarchy(names[1..-1], child)
        }
    }

    Hierarchy () {
        def names = '/some/thing/to/test'.split('/')
        if (names != null && names.size() > 0){
            processHierarcy(names, this.root)
        }
        println this.root
    }
}

new Hierarchy()

But I get the following error:

Caught: groovy.lang.MissingMethodException: No signature of method: Hierarchy.processHierarcy() is applicable for argument types: ([Ljava.lang.String;, java.util.LinkedHashMap) values: [[, some, thing, to, test], [:]]
Possible solutions: processHierarchy(java.lang.Object, java.lang.Object)
groovy.lang.MissingMethodException: No signature of method: Hierarchy.processHierarcy() is applicable for argument types: ([Ljava.lang.String;, java.util.LinkedHashMap) values: [[, some, thing, to, test], [:]]
Possible solutions: processHierarchy(java.lang.Object, java.lang.Object)
    at Hierarchy.<init>(xxx.groovy:48)
    at xxx.run(xxx.groovy:54)

What am I missing here?

4

1 回答 1

1

缺少一个h:)processHierarcy(names, this.root)

另外,修改 if 阻止processHierarchy()if(names.size() > 1)避免IndexOutOfBoundsException异常。

于 2013-10-08T21:33:24.047 回答