Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Apache Groovy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Features== Most valid Java files are also valid Groovy files. Although the two languages are similar, Groovy code can be more compact, because it does not need all the elements that Java needs.<ref name=konig32>König 2007, pg. 32</ref> This makes it possible for Java programmers to learn Groovy gradually by starting with familiar Java syntax before acquiring more Groovy [[programming idiom]]s.<ref>{{cite web |url=http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers |title=Groovy style and language feature guidelines for Java developers |publisher=Groovy.codehaus.org |access-date=2015-01-22 |archive-url=https://web.archive.org/web/20150117214709/http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers |archive-date=2015-01-17 |url-status=dead }}</ref> Groovy features not available in Java include both static and [[Type system#DYNAMIC|dynamic]] typing (with the keyword <code>def</code>), [[operator overloading]], native syntax for lists and [[associative array]]s (maps), native support for [[regular expression]]s, polymorphic iteration, [[string interpolation]], added helper methods, and the [[safe navigation operator]] <code>?.</code> to check automatically for [[null pointer]]s (for example, <code>variable?.method()</code>, or <code>variable?.field</code>).<ref>{{cite web |url=http://groovy.codehaus.org/Differences+from+Java |title=Groovy – Differences from Java |publisher=Groovy.codehaus.org |access-date=2013-08-12 |url-status=dead |archive-url=https://web.archive.org/web/20090317025737/http://groovy.codehaus.org/Differences+from+Java |archive-date=2009-03-17 }}</ref> Since version 2, Groovy also supports modularity (shipping only the <code>jar</code>s that the project uses, thus reducing the size of Groovy's library), type checking, static compilation, Project Coin syntax enhancements, [[Java syntax#Code blocks|multicatch blocks]] and ongoing performance enhancements using the <code>invokedynamic</code> instruction introduced in [[Java version history#Java SE 7|Java 7]].<ref>{{cite web |url=http://www.infoq.com/articles/new-groovy-20 |title=What's new in Groovy 2.0? |date=28 Jun 2012}}</ref> Groovy natively supports [[markup language]]s such as [[XML]] and [[HTML]] by using an inline [[Document Object Model]] (DOM) syntax. This feature enables the definition and manipulation of many types of heterogeneous data assets with a uniform and concise syntax and programming methodology.{{Citation needed|date=November 2008}} Unlike Java, a Groovy source code file can be executed as an (uncompiled) [[scripting language|script]], if it contains code outside any class definition, if it is a class with a ''main'' method, or if it is a ''Runnable'' or ''GroovyTestCase''. A Groovy script is fully parsed, compiled, and generated before executing (similar to Python and Ruby). This occurs under the hood, and the compiled version is not saved as an artifact of the process.<ref name=konig37_8>König 2007, pp. 37-8</ref> ===GroovyBeans, properties=== ''GroovyBeans'' are Groovy's version of [[JavaBeans]]. Groovy implicitly generates getters and setters. In the following code, <code>setColor(String color)</code> and <code>getColor()</code> are implicitly generated. The last two lines, which appear to access color directly, are actually calling the implicitly generated methods.<ref name=konig38_9>König 2007, pp. 38-9</ref> <syntaxhighlight lang="groovy"> class AGroovyBean { String color } def myGroovyBean = new AGroovyBean() myGroovyBean.setColor('baby blue') assert myGroovyBean.getColor() == 'baby blue' myGroovyBean.color = 'pewter' assert myGroovyBean.color == 'pewter' </syntaxhighlight> Groovy offers simple, consistent syntax for handling ''lists'' and ''maps'', reminiscent of Java's ''array'' syntax.<ref name=konig41_3>König 2007, pp. 41-3</ref> <syntaxhighlight lang="groovy"> def movieList = ['Dersu Uzala', 'Ran', 'Seven Samurai'] // Looks like an array, but is a list assert movieList[2] == 'Seven Samurai' movieList[3] = 'Casablanca' // Adds an element to the list assert movieList.size() == 4 def monthMap = [ 'January' : 31, 'February' : 28, 'March' : 31 ] // Declares a map assert monthMap['March'] == 31 // Accesses an entry monthMap['April'] = 30 // Adds an entry to the map assert monthMap.size() == 4 </syntaxhighlight> ===Prototype extension=== Groovy offers support for [[Prototype-based programming|prototype extension]] through <code>ExpandoMetaClass</code>, Extension Modules (only in Groovy 2), Objective-C-like [[Objective-C#Categories|Categories]] and <code>DelegatingMetaClass</code>.<ref>{{cite web |url=http://groovy.codehaus.org/JN3525-MetaClasses |title=JN3525-MetaClasses |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121001122409/http://groovy.codehaus.org/JN3525-MetaClasses |archive-date=2012-10-01 |url-status=dead }}</ref> <code>ExpandoMetaClass</code> offers a [[domain-specific language]] (DSL) to express the changes in the class easily, similar to [[Ruby (programming language)|Ruby's]] open class concept: <syntaxhighlight lang="groovy"> Number.metaClass { sqrt = { Math.sqrt(delegate) } } assert 9.sqrt() == 3 assert 4.sqrt() == 2 </syntaxhighlight> Groovy's changes in code through prototyping are not visible in Java, since each attribute/method invocation in Groovy goes through the metaclass registry. The changed code can only be accessed from Java by going to the metaclass registry. Groovy also allows overriding methods as <code>getProperty()</code>, <code>propertyMissing()</code> among others, enabling the developer to intercept calls to an object and specify an action for them, in a simplified [[Aspect-oriented programming|aspect-oriented]] way. The following code enables the class <code>java.lang.String</code> to respond to the <code>hex</code> property: <syntaxhighlight lang="groovy"> enum Color { BLACK('#000000'), WHITE('#FFFFFF'), RED('#FF0000'), BLUE('#0000FF') String hex Color(String hex) { this.hex = hex } } String.metaClass.getProperty = { String property -> def stringColor = delegate if (property == 'hex') { Color.values().find { it.name().equalsIgnoreCase stringColor }?.hex } } assert "WHITE".hex == "#FFFFFF" assert "BLUE".hex == "#0000FF" assert "BLACK".hex == "#000000" assert "GREEN".hex == null </syntaxhighlight> The Grails framework uses metaprogramming extensively to enable [[Grails (framework)#Persistence|GORM]] dynamic finders, like <code>User.findByName('Josh')</code> and others.<ref>{{cite web |url=http://www.slideshare.net/zenMonkey/metaprogramming-techniques-in-groovy-and-grails |title=Metaprogramming Techniques in Groovy and Grails |date=11 Jun 2009}}</ref> ===Dot and parentheses=== Groovy's syntax permits omitting parentheses and dots in some situations. The following groovy code <syntaxhighlight lang="groovy"> take(coffee).with(sugar, milk).and(liquor) </syntaxhighlight> can be written as <syntaxhighlight lang="groovy"> take coffee with sugar, milk and liquor </syntaxhighlight> enabling the development of [[domain-specific language]]s (DSLs) that look like plain English. ===Functional programming=== Although Groovy is mostly an object-oriented language, it also offers [[functional programming]] features. ====Closures==== According to Groovy's documentation: "Closures in Groovy work similar to a 'method pointer', enabling code to be written and run in a later point in time".<ref>{{cite web |url=http://groovy.codehaus.org/Closures |title=Groovy - Closures |url-status=dead |archive-url=https://web.archive.org/web/20120522213410/http://groovy.codehaus.org/Closures |archive-date=2012-05-22 }}</ref> Groovy's closures support free variables, i.e. variables that have not been explicitly passed as a parameter to it, but exist in its declaration context, [[partial application]] (that it terms '[[currying]]'<ref name=":0" />), delegation, implicit, typed and untyped parameters. When working on Collections of a determined type, the closure passed to an operation on the collection can be inferred: <syntaxhighlight lang="groovy"> list = [1, 2, 3, 4, 5, 6, 7, 8, 9] /* * Non-zero numbers are coerced to true, so when it % 2 == 0 (even), it is false. * The type of the implicit "it" parameter can be inferred as an Integer by the IDE. * It could also be written as: * list.findAll { Integer i -> i % 2 } * list.findAll { i -> i % 2 } */ def odds = list.findAll { it % 2 } assert odds == [1, 3, 5, 7, 9] </syntaxhighlight> A group of expressions can be written in a closure block without reference to an implementation and the responding object can be assigned at a later point using delegation: <syntaxhighlight lang="groovy"> // This block of code contains expressions without reference to an implementation def operations = { declare 5 sum 4 divide 3 print } </syntaxhighlight> <syntaxhighlight lang="groovy"> /* * This class will handle the operations that can be used in the closure above. Another class * could be declared having the same methods, but using, for example, webservice operations * in the calculations. */ class Expression { BigDecimal value /* * Though an Integer is passed as a parameter, it is coerced into a BigDecimal, as was * defined. If the class had a 'declare(Integer value)' method, it would be used instead. */ def declare(BigDecimal value) { this.value = value } def sum(BigDecimal valueToAdd) { this.value += valueToAdd } def divide(BigDecimal divisor) { this.value /= divisor } def propertyMissing(String property) { if (property == "print") println value } } </syntaxhighlight> <syntaxhighlight lang="groovy"> // Here is defined who is going to respond the expressions in the block of code above. operations.delegate = new Expression() operations() </syntaxhighlight> ====Curry==== Usually called ''[[partial application]]'',<ref name=":0">[http://programmers.stackexchange.com/questions/152868/does-groovy-call-partial-application-currying "Does groovy call partial application 'currying']", 10 Aug 2013</ref> this Groovy feature allows closures' parameters to be set to a default parameter in any of their arguments, creating a new closure with the bound value. Supplying one argument to the <code>curry()</code> method will fix argument one. Supplying N arguments will fix arguments 1 .. N. <syntaxhighlight lang="groovy"> def joinTwoWordsWithSymbol = { symbol, first, second -> first + symbol + second } assert joinTwoWordsWithSymbol('#', 'Hello', 'World') == 'Hello#World' def concatWords = joinTwoWordsWithSymbol.curry(' ') assert concatWords('Hello', 'World') == 'Hello World' def prependHello = concatWords.curry('Hello') //def prependHello = joinTwoWordsWithSymbol.curry(' ', 'Hello') assert prependHello('World') == 'Hello World' </syntaxhighlight> Curry can also be used in the reverse direction (fixing the last N arguments) using <code>rcurry()</code>. <syntaxhighlight lang="groovy"> def power = { BigDecimal value, BigDecimal power -> value**power } def square = power.rcurry(2) def cube = power.rcurry(3) assert power(2, 2) == 4 assert square(4) == 16 assert cube(3) == 27 </syntaxhighlight> Groovy also supports [[lazy evaluation]],<ref>{{cite web |url=http://groovy.codehaus.org/Lazy+transformation |title=Groovy - Lazy Transformation |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121008091312/http://groovy.codehaus.org/Lazy+transformation |archive-date=2012-10-08 |url-status=dead }}</ref><ref>{{cite web|url=http://ndpar.blogspot.com.br/2011/02/lazy-lists-in-groovy.html|title=Side Notes: Lazy lists in Groovy|date=3 Feb 2011}}</ref> [[Fold (higher-order function)|reduce/fold]],<ref>{{cite web |url=http://bendoerr.me/posts/2011-06-20-groovy-inject.html |title=Groovy's Fold |date=20 Jun 2011 |access-date=12 February 2015 |archive-url=https://web.archive.org/web/20150213033355/http://bendoerr.me/posts/2011-06-20-groovy-inject.html |archive-date=13 February 2015 |url-status=dead }}</ref> [[Data structure|infinite structure]]s and [[immutability]],<ref>{{cite web |url=http://www.slideshare.net/arturoherrero/functional-programming-with-groovy |title=Functional Programming with Groovy |date=5 Nov 2011}}</ref> among others.<ref>{{cite web |url=http://groovy.codehaus.org/Functional+Programming+with+Groovy |title=Function programming in Groovy |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121008095622/http://groovy.codehaus.org/Functional+Programming+with+Groovy |archive-date=2012-10-08 |url-status=dead }}</ref> ===JSON and XML processing=== On JavaScript Object Notation ([[JSON]]) and XML processing, Groovy employs the [[Builder pattern]], making the production of the data structure less verbose. For example, the following XML: <syntaxhighlight lang="xml"> <languages> <language year="1995"> <name>Java</name> <paradigm>object oriented</paradigm> <typing>static</typing> </language> <language year="1995"> <name>Ruby</name> <paradigm>functional, object oriented</paradigm> <typing>duck typing, dynamic</typing> </language> <language year="2003"> <name>Groovy</name> <paradigm>functional, object oriented</paradigm> <typing>duck typing, dynamic, static</typing> </language> </languages> </syntaxhighlight> can be generated via the following Groovy code: <syntaxhighlight lang="groovy"> def writer = new StringWriter() def builder = new groovy.xml.MarkupBuilder(writer) builder.languages { language(year: 1995) { name "Java" paradigm "object oriented" typing "static" } language (year: 1995) { name "Ruby" paradigm "functional, object oriented" typing "duck typing, dynamic" } language (year: 2003) { name "Groovy" paradigm "functional, object oriented" typing "duck typing, dynamic, static" } } </syntaxhighlight> and also can be processed in a streaming way through <code>StreamingMarkupBuilder</code>. To change the implementation to JSON, the <code>MarkupBuilder</code> can be swapped to <code>JsonBuilder</code>.<ref>{{cite web |url=http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html |title=JsonBuilder |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121002221510/http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html |archive-date=2012-10-02 |url-status=dead }}</ref> To parse it and search for a functional language, Groovy's <code>findAll</code> method can serve: <syntaxhighlight lang="groovy"> def languages = new XmlSlurper().parseText writer.toString() // Here is employed Groovy's regex syntax for a matcher (=~) that will be coerced to a // boolean value: either true, if the value contains our string, or false otherwise. def functional = languages.language.findAll { it.paradigm =~ "functional" } assert functional.collect { it.name } == ["Groovy", "Ruby"] </syntaxhighlight> ===String interpolation=== In Groovy, strings can be interpolated with variables and expressions by using GStrings:<ref>{{cite web |url=http://rajakannappan.blogspot.com.br/2009/12/groovy-strings-different-ways-of.html |title=Groovy Strings - Different ways of creating them |date=26 Dec 2009}}</ref> <syntaxhighlight lang="groovy"> BigDecimal account = 10.0 def text = "The account shows currently a balance of $account" assert text == "The account shows currently a balance of 10.0" </syntaxhighlight> GStrings containing variables and expressions must be declared using double quotes. A complex expression must be enclosed in curly brackets. This prevents parts of it from being interpreted as belonging to the surrounding string instead of to the expression: <syntaxhighlight lang="groovy"> BigDecimal minus = 4.0 text = "The account shows currently a balance of ${account - minus}" assert text == "The account shows currently a balance of 6.0" // Without the brackets to isolate the expression, this would result: text = "The account shows currently a balance of $account - minus" assert text == "The account shows currently a balance of 10.0 - minus" </syntaxhighlight> Expression evaluation can be deferred by employing arrow syntax: <syntaxhighlight lang="groovy"> BigDecimal tax = 0.15 text = "The account shows currently a balance of ${->account - account*tax}" tax = 0.10 // The tax value was changed AFTER declaration of the GString. The expression // variables are bound only when the expression must actually be evaluated: assert text == "The account shows currently a balance of 9.000" </syntaxhighlight> ===Abstract syntax tree transformation=== According to Groovy's own documentation, "When the Groovy compiler compiles Groovy scripts and classes, at some point in the process, the source code will end up being represented in memory in the form of a Concrete Syntax Tree, then transformed into an Abstract Syntax Tree. The purpose of AST Transformations is to let developers hook into the compilation process to be able to modify the AST before it is turned into bytecode that will be run by the JVM. AST Transformations provides Groovy with improved compile-time metaprogramming capabilities allowing powerful flexibility at the language level, without a runtime performance penalty."<ref>{{cite web |url=http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations |title=Compile-time Metaprogramming - AST Transformations |access-date=2012-10-07 |archive-url=https://web.archive.org/web/20121014094900/http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations |archive-date=2012-10-14 |url-status=dead }}</ref> Examples of ASTs in Groovy are: * Category and [[Mixin]] transformation * Immutable AST Macro * Newify transformation * Singleton transformation among others. The testing framework [[Spock (testing framework)|Spock]] uses AST transformations to allow the programmer to write tests in a syntax not supported by Groovy, but the relevant code is then manipulated in the AST to valid code.<ref>{{cite journal |last1=King |first1=Paul |title=A History of the Groovy Programming Language |journal=Proc. ACM Program. Lang. |date=2020 |volume=4 |pages=53 |doi=10.1145/3386326|doi-access=free }}</ref> An example of such a test is: <syntaxhighlight lang="groovy"> def "maximum of #a and #b is #c" () { expect: Math.max (a, b) == c where: a | b || c 3 | 5 || 5 7 | 0 || 7 0 | 0 || 0 } </syntaxhighlight> ===Traits=== According to Groovy's documentation, "[[Trait (computer programming)|Traits]] are a structural construct of the language that allows: composition of behaviors, runtime implementation of interfaces, behavior overriding, and compatibility with static type checking/compilation." Traits can be seen as [[interface (object-oriented programming)|interface]]s carrying both default implementations and state. A trait is defined using the trait keyword: <syntaxhighlight lang="groovy"> trait FlyingAbility { /* declaration of a trait */ String fly() { "I'm flying!" } /* declaration of a method inside a trait */ } </syntaxhighlight> Then, it can be used like a normal interface using the keyword <code>implements</code>: <syntaxhighlight lang="groovy"> class Bird implements FlyingAbility {} /* Adds the trait FlyingAbility to the Bird class capabilities */ def bird = new Bird() /* instantiate a new Bird */ assert bird.fly() == "I'm flying!" /* the Bird class automatically gets the behavior of the FlyingAbility trait */ </syntaxhighlight> Traits allow a wide range of abilities, from simple composition to testing.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)