Groovy: Strange things

Does writing some code as a Script or as a class in Script make any difference?

I started out with some practice on Groovy builders as reading alone was not giving me any confidence (and not to mention that groovy documentation is not up-to-date). By this time i was confident that i knew a thing about groovy method invocation.

I started with a custom builder. Then wrote a script to test it. Running the script spits out the error message (Missing method exception). I pondered over this for quite sometime and was not able to figure out what was wrong. Is’t this what a BuilderSupport is supposed to do. I decided to write the script differently, creating an enclosing class. This time the error message was different. Cannot find missing method positive. This error message pointed out exactly what was wrong in the builder that i wrote. I copied pasted a plus sign in my code at line 29 in TreeBuilderWithSupport.

But still no clue on why the script ate the actual exception & there was no reference to BuilderSupport in exception stacktrace. Will try to find this out on groovy forums & a better way to debug groovy code. Update: use dump() or inspect the metaclass.

The builder code does not do much and was intended to print a tree (expanded/collapsed). Groovy’s documentation is turning out to be a disappointment, most of the pages turn out to be out of data or incomplete. package builder;

import java.util.Map;
import groovy.util.BuilderSupport;

class TreeBuilderWithSupport extends BuilderSupport{

	def out = System.out

	protected Object createNode(Object name) { out << name + "\n" return name;}

	protected Object createNode(Object name, Object value) { out << name + "($value)" return name;}

	protected Object createNode(Object name, Map attributes) { out << name attributes.each {  out << "$it.key : $it.value  " } out <<  +"\n" return name;}

	protected Object createNode(Object name, Map attributes, Object value) { out << name  attributes.each {  out << "$it.key $it.value  val=$value" } out << + "\n" return name;}

	protected void setParent(Object parent, Object child) {}

	def missingMethod(String m, args) { out << "missing"}
}
package builder
def tb = new TreeBuilderWithSupport();

tb.tree {  
	level1 (a:1, b:2) {   
		level2{    
			level3 {    
			}   
		}  
	}
}
Caught: groovy.lang.MissingMethodException: No signature of method: builder.TestTreeWithSupportAsScript.level1() is applicable for argument types: (java.util.LinkedHashMap, builder.TestTreeWithSupportAsScript$_run_closure1_closure2) values: [[a:1, b:2], builder.TestTreeWithSupportAsScript$_run_closure1_closure2@147917a]Possible solutions: every(), every(groovy.lang.Closure) at builder.TestTreeWithSupportAsScript$_run_closure1.doCall(TestTreeWithSupportAsScript.groovy:6) at builder.TestTreeWithSupportAsScript$_run_closure1.doCall(TestTreeWithSupportAsScript.groovy) at builder.TestTreeWithSupportAsScript.run(TestTreeWithSupportAsScript.groovy:5)
package builder

import groovy.xml.MarkupBuilder;

class TestTreeWithSupport {

public static void main(String[] args) {  
	def tb = new TreeBuilderWithSupport();

	tb.tree {    
		level1 (a:1, b:2) {     
			level2{      
				level3 {      
				}     
			}    
		}  
	} 
}
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive() is applicable for argument types: () values: []Possible solutions: notify(), size(), tokenize() at builder.TreeBuilderWithSupport.createNode(TreeBuilderWithSupport.groovy:29) at builder.TestTreeWithSupport$_main_closure1.doCall(TestTreeWithSupport.groovy:11) at builder.TestTreeWithSupport$_main_closure1.doCall(TestTreeWithSupport.groovy) at builder.TestTreeWithSupport.main(TestTreeWithSupport.groovy:10)