====== Groovy == ===== Getting Help == * Indexes: [[groovy>Documentation|Groovy Documentation Home]] - [[ibm>views/java/libraryview.jsp?search_by=practically+groovy|IBM Practically Groovy]] * Tutorials: * APIs: [[groovy>gapi/|Groovy API]] FIXME add description - [[groovy>groovy-jdk]] Groovy methods added to standard Java SE classes ===== Install == [[groovy>Download]] the binary and unpack it to ''/usr/local'' and make a symlink: ''/usr/local# ln -s groovy-1.6.5 groovy'' [[groovy>Installing+Groovy|Add]] groovy bin directory to your invironment variables: # /etc/profile.d/jdk.sh ... export GROOVY_HOME=/usr/local/groovy export PATH="$PATH:$GROOVY_HOME/bin" ===== Tools == [[groovy>Groovy+Console|groovyConsole]] is a GUI app for evaluating Groovy scripts and [[groovy>Groovy+Shell|groovysh]] is an interactiove CLI app for evaluating Groovy expressions. [[groovy>Groovy+CLI|groovy]] ist the interpreter for Groovy scripts (see below) and ''groovyc'' is the compiler which compiles Groovy scripts to java classes (see below) ===== Script Files == ==== Shebang == #!/usr/bin/env groovy println "Hallo Sandy!" ==== Command Line Arguments == // file: test.groovy println args[0]+ ' '+ args[1]+ ' '+ args[2]+ ' '+ args.length Invoking the script with ''test.groovy one two three'' prints ''one two three 3''. See also [[groovy>Groovy+CLI]] ==== Stdin stdout stderr == FIXME ''System.in.text'' ==== Behind the Scenes == A script automatically creates a class which has the same name as the filename of the script: #!/usr/bin/env groovy // Filname: groovytest println this.class // prints: class groovytest println getClass() // prints: class groovytest println this.getClass() // prints: class groovytest The first version is a [[javaref>api/java/lang/Class.html|class literal]] and seems to be a shortcut for calling getClass(). ''println'' seems to call implizitely ''toString()'' on any object. (This is the same as ''System.out.println()'' in standard java.): println this.class // prints: class groovytest println this.class.toString() // prints: class groovytest The parent class of the autogenerated script class is ''[[groovy>gapi/groovy/lang/Script.html|groovy.lang.Script]]'': println this.class.getSuperclass() // prints: groovy.lang.Script ''println()'' is a method of the autogenerated script class (inherited from ''groovy.lang.Script''). It can be called without ''this.'' and braces. This seems to be added automatically, because the following call prints: ''...Exception: No signature of method: groovytest.println() is applicable for argument types: (java.lang.Integer, java.lang.Integer)...'' #!/usr/bin/env groovy // Filename: groovytest println 0,0 ===== Compile Groovy Scripts == Groovy scripts can be [[groovy>Compiling+Groovy|compiled]] to java class files with ''groovyc''. In order to run those classes with the normal ''java'' command, you either have to [[groovy>Running|include]] ''/usr/local/groovy/embeddable/goovy-all-1.6.5.jar'' to the ''CLASSPATH'' variable or better :?:((to be tested!)) make a symlink in the [[javatut>ext/basics/install.html|Java Installed Extensions]] directory: cd /usr/lib/java/jre/lib/ext ln -s /usr/local/groovy/embeddable/goovy-all-1.6.5.jar Note that this would have to be done on every system which wants to run a compiled script. Therefore it is better to create a jar file wich already includes the necessary depcendencies. This is described [[groovy>WrappingGroovyScript|here]]. ===== Java Web Start == FIXME Add Groovy specific infos about Java Web Start here. See also [[java#java_web_start|generic Java Web Start]] ===== Apache Pivot == FIXME Add Groovy specific infos about Pivot here. http://www.ibm.com/developerworks/xml/tutorials/x-pivottut/ See also generic [[Apache Pivot]] ==== Java Web Start == FIXME Add Groovy / Pivot specific infos about Java Web Start here. See also [[Apache Pivot#java_web_start|general pivot specific things about Java Web Start]] ===== Dynamic Typing == ''def'' is a replacement for a type name e.g. in method signatures. It is used to indicate that you don't care about the type. You can omit ''def'' in global variables of a script: def seven=7; println "seven.class: "+ seven.class // -> class java.lang.Integer int eight=8; println "eight.class: "+ eight.class // -> class java.lang.Integer nine=9; println "nine.class: " + nine.class // -> class java.lang.Integer seven="seven"; println "seven.class: "+ seven.class // -> class java.lang.String //eight="eight"; println "eight.class: "+ eight.class // GroovyCastException!!! nine="nine"; println "nine.class: " + nine.class // -> class java.lang.String ===== Collections: Lists, Ranges and Maps == ==== Lists == #!/usr/bin/env groovy myList = [1976, 1969] // create a list myList << 2010 // append something to the end println myList // => [1976, 1969, 2010] println myList[2] // => 2010 println myList[3] // => null println myList.size // => 3 Iterate over the list by calling the ''each()'' method on the list and pass in a closure: myList.each{ print "$it " } // => 1976 1969 2010 myList.eachWithIndex{ v, k -> print "$k:$v " } // => 0:1976 1:1969 2:2010 Each list [[groovy>Collections|creates]] an implementation of [[javaref>api/java/util/List.html|java.util.List]]. Hence the complete API of List is available: println myList.class // => class java.util.ArrayList println myList.get(0) // => 1976 println myList.size() // => 2 More on lists: [[groovy>JN1015-Collections|usage examples]], [[groovy>groovy-jdk/java/util/List.html|added methods to the standard List interface]], [[ibm>java/library/j-pg04149.html|Reaching for each]] ==== Maps == myMap = ["Sandy":1976, "Becki":1969] println myMap.Sandy // => 1976 println myMap.get("Sandy") // => 1976 println myMap.class // => null (default getter is overwritten !!!) println myMap.getClass() // => class java.util.LinkedHashMap myMap.Junior="2010?" // Add a new value myMap.each{ print "$it " } // => Sandy=1976 Becki=1969 Junior=2010? myMap.each{ print "$it.key=$it.value "} // => Sandy=1976 Becki=1969 Junior=2010? myMap.each{ k, v -> print "$k=$v " } // => Sandy=1976 Becki=1969 Junior=2010? More on maps: [[ibm>java/library/j-pg04149.html|Reaching for each]], [[groovy>JN1035-Maps|Maps]] ===== Unsorted == * Interfaces can be [[groovy>Groovy+way+to+implement+interfaces|implemted using closures and maps]] * ''.sort()'' is a method added to [[groovy>groovy-jdk/java/util/Collection.html|Collection]] by Groovy * Everything can be [[ibm>java/library/j-pg04149.html|traversed]] using ''each'' * Getters and setters are [[ibm>java/library/j-pg02179.html#N10203|autogenerated]] * ''def'' is used for dynamically typed variables. See [[groovy>Scoping+and+the+Semantics+of+"def"]] -> What is this "def" I heard of? ===== Open Questions / To do == * Grails: http://grails.org/doc/latest/guide/17.%20Deployment.html http://grails.org/Deployment * [[http://www.ibm.com/developerworks/java/library/j-pg03155/|GroovyServer Pages]] * [[http://docs.codehaus.org/display/GROOVY/Extended+Guide+to+Method+Signatures|Named arguments]] are possible. Are they useful? * Final classes can be [[ibm>java/library/j-pg06239.html|extended]]