{"id":119,"date":"2006-06-11T23:29:19","date_gmt":"2006-06-12T04:29:19","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/06\/11\/common-groovy-errors\/"},"modified":"2006-06-11T23:29:19","modified_gmt":"2006-06-12T04:29:19","slug":"common-groovy-errors","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/06\/11\/common-groovy-errors\/","title":{"rendered":"Common Groovy Errors"},"content":{"rendered":"<p>At my work we are using Groovy extensively and often mix Java syntax in Groovy.  I want to post some common Groovy language errors that I think everybody in my team has committed at one point when we forget that Groovy is not Java.  The <b>first error<\/b> that I will mention is that use of array initializers.  In Java you can construct an array using an initializer like this:<\/p>\n<pre>\nString[] array = new String[]{\"one\", \"two\", \"three\"}\n<\/pre>\n<p>In Groovy, when we attempted to use this construct we would get a nasty and hard to understand compiler exception.  Groovy is an expressive language and you can construct an initialized list in the following fashion:<\/p>\n<pre>\ndef list = [\"one\", \"two\", \"three\"]   \/\/ ArrayList\n<\/pre>\n<p>The list variable is an instance of ArrayList.  If you require an array you need to cast the list such as:<\/p>\n<pre>\ndef arr = (String[])list   \/\/ String array\n<\/pre>\n<p>Now, this leads me to the <b>second compiler error<\/b> that I have encountered in Groovy.  In Groovy I keep writing for loops the Java way:<\/p>\n<pre>\nfor(int i; i < list.size(); i++) {\n   System.out.println(list.get(i));\n}\n<\/pre>\n<p>From my understanding Groovy only supports the for\/in loop construct.  If you need to iterate over a list using an index you could do the following:<\/p>\n<pre>\nfor(i in 0 .. list.size()-1) {\n   println list.get(i)\n}\n<\/pre>\n<p>If you don&#8217;t need the index you can loop over an list in by using the each method provided by the Groovy Development Kit (GDK):<\/p>\n<pre>\nlist.each {\n   println it\n}\n<\/pre>\n<p>Since I mention how to construct an initialized list in Groovy, let me also cover maps.  In Groovy you can construct a map by using the following construct:<\/p>\n<pre>\ndef map = [name:'Juixe', date:new Date()]\n<\/pre>\n<p>Since map is an instance of HashMap you can retrieve values using the get method but Groovy provides a different mechanism.  In Groovy you can access a value from a map like this:<\/p>\n<pre>\nprintln map['name']   \/\/ returns Juixe\n<\/pre>\n<p>In the examples above I have mentioned how to create lists and maps with data.  If you need to create a list or map without data you can use one of the following statements:<\/p>\n<pre>\ndef emptyList = []\ndef emptyMap = [:]\n<\/pre>\n<p>A <b>third error<\/b> that I have commited is that I keep thinking of the each closures as for loops.  I have received compilation erros because I try to continue or break out of a closure loop.  Closures are code block so you can break out of them using the return keyword.  I continue to the next element in a list do something like the following:<\/p>\n<pre>\n[1, 2, 3, 4, 5].each {\n  if(it == 3)\n\treturn\n  println it\n}\n<\/pre>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/groovy\" rel=\"tag\">groovy<\/a>, <a href=\"http:\/\/technorati.com\/tag\/gdk\" rel=\"tag\"> gdk<\/a>, <a href=\"http:\/\/technorati.com\/tag\/java\" rel=\"tag\"> java<\/a>, <a href=\"http:\/\/technorati.com\/tag\/scripting\" rel=\"tag\"> scripting<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>At my work we are using Groovy extensively and often mix Java syntax in Groovy. I want to post some common Groovy language errors that I think everybody in my team has committed at one point when we forget that Groovy is not Java. The first error that I will mention is that use of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1V","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/119"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}