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
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!
{{Short description|Programming language}} {{Infobox programming language | name = Groovy | logo = Groovy-logo.svg | logo caption = Groovy Logo | paradigm = [[Programming paradigm#Multi-paradigm|Multi-paradigm]]: [[Object-oriented programming|object-oriented]], [[Imperative programming|imperative]], [[Functional programming|functional]], [[aspect-oriented programming|aspect-oriented]], [[Scripting language|scripting]] | year = {{Start date and age|2003}} | latest preview version = 4.0.0-beta-1 | latest preview date = {{start date and age|2021|09|06}}<ref name="github-releases">{{cite web|url=https://github.com/apache/groovy/releases|title=Releases - apache/groovy|via=[[GitHub]]|accessdate=2020-04-09}}</ref> | designer = [[James Strachan (programmer)|James Strachan]] | developer = Paul King (PMC Chair)<br />Jochen Theodorou (Tech Lead)<br />Guillaume Laforge<br />Cedric Champeau<br />Daniel Sun<br />Eric Milles | typing = [[Type system#DYNAMIC|Dynamic]], [[Type system#STATIC|static]], [[Strong and weak typing|strong]], [[Duck typing|duck]] | website = {{official URL}} | implementations = [[Gradle]], [[Grails (framework)|Grails]] | influenced by = [[Java (programming language)|Java]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[Smalltalk]] | influenced = [[Kotlin (programming language)|Kotlin]] | platform = [[Java Platform, Standard Edition|Java SE]] | license = [[Apache License 2.0]] | file ext = <code>.groovy, .gvy, .gy, .gsh</code><ref name="Groovy Goodness: Default Groovy Script File Extensions">{{Cite web|url=https://mrhaki.blogspot.com/2011/10/groovy-goodness-default-groovy-script.html|title=Groovy Goodness: Default Groovy Script File Extensions}}</ref> }} '''Apache Groovy''' is a [[Java (programming language)|Java]]-syntax-compatible [[object-oriented programming|object-oriented]] [[programming language]] for the [[Java (software platform)|Java platform]]. It is both a static and [[dynamic programming language|dynamic]] language with features similar to those of [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], and [[Smalltalk]]. It can be used as both a [[programming language]] and a [[scripting language]] for the Java Platform, is compiled to [[Java virtual machine]] (JVM) [[bytecode]], and interoperates seamlessly with other Java code and [[Library (computing)|libraries]]. Groovy uses a [[Curly bracket languages|curly-bracket syntax]] similar to Java's. Groovy supports [[Closure (computer programming)|closures]], multiline strings, and [[String interpolation|expressions embedded in strings]]. Much of Groovy's power lies in its [[Abstract syntax tree|AST]] transformations, triggered through annotations. Groovy 1.0 was released on January 2, 2007, and Groovy 2.0 in July, 2012. Since version 2, Groovy can be [[Type system#STATIC|compiled statically]], offering [[type inference]] and performance near that of Java.<ref>{{cite web|url=http://objectscape.blogspot.com.br/2012/08/groovy-20-performance-compared-to-java.html|title=Groovy 2.0 Performance compared to Java |date=25 Aug 2012}}</ref><ref>{{cite web|url=http://blog.evan-wong.com/2012/07/java-vs-groovy20-vs-scala-simple.html|title=Java vs Groovy2.0 vs Scala Simple Performance Test|date=10 Jul 2012|access-date=7 October 2012|archive-url=https://web.archive.org/web/20121210210126/http://blog.evan-wong.com/2012/07/java-vs-groovy20-vs-scala-simple.html|archive-date=10 December 2012|url-status=dead}}</ref> Groovy 2.4 was the last major release under [[Pivotal Software]]'s sponsorship which ended in March 2015.<ref name="blog.pivotal.io">{{cite web|url=http://blog.pivotal.io/pivotal/news-2/groovy-2-4-and-grails-3-0-to-be-last-major-releases-under-pivotal-sponsorship|title=Groovy 2.4 And Grails 3.0 To Be Last Major Releases Under Pivotal Sponsorship|date=19 Jan 2015}}</ref> Groovy has since changed its governance structure to a Project Management Committee in [[the Apache Software Foundation]].<ref name="Groovy Incubator">{{cite web|url=https://blogs.apache.org/foundation/entry/groovy_submitted_to_become_a|title=Groovy joins Apache Incubator|date=11 Mar 2015}}</ref> ==History== [[James Strachan (programmer)|James Strachan]] first talked about the development of Groovy on his blog in August 2003.<ref>{{cite web|url=http://radio.weblogs.com/0112098/2003/08/29.html|archive-url=https://web.archive.org/web/20030901064404/http://radio.weblogs.com/0112098/2003/08/29.html|url-status=dead|archive-date=1 September 2003|title=Groovy - the birth of a new dynamic language for the Java platform|author=James Strachan|date=29 Aug 2003}}</ref> In March 2004, Groovy was submitted to the JCP as JSR 241<ref name="JSR">{{cite web |url=http://www.jcp.org/en/jsr/detail?id=241 |title=Java Community Process JSR 241}}</ref> and accepted by ballot. Several versions were released between 2004 and 2006. After the [[Java Community Process]] (JCP) standardization effort began, the version numbering changed, and a version called "1.0" was released on January 2, 2007. After various betas and release candidates numbered 1.1, on December 7, 2007, Groovy 1.1 Final was released and immediately renumbered as Groovy 1.5 to reflect the many changes made. In 2007, Groovy won the first prize at JAX 2007 innovation award.<ref>{{cite web|url=http://docs.codehaus.org/display/GROOVY/2007/04/26/Groovy+wins+first+prize+at+JAX+2007+innovation+award|title=Groovy wins first prize at JAX 2007 innovation award|date=2007-04-26|access-date=2012-10-07|archive-url=https://web.archive.org/web/20150513184206/http://docs.codehaus.org/display/GROOVY/2007/04/26/Groovy+wins+first+prize+at+JAX+2007+innovation+award|archive-date=2015-05-13|url-status=dead}}</ref> In 2008, [[Grails (framework)|Grails]], a Groovy [[web framework]], won the second prize at JAX 2008 innovation award.<ref>{{cite web|url=http://jax-award.de/jax_award08/proposal_view_de.php?id=240&show=more|title=They say a lot can happen over a cup of coffee|access-date=2012-10-07|archive-url=https://web.archive.org/web/20110419130810/http://jax-award.de/jax_award08/proposal_view_de.php?id=240|archive-date=2011-04-19|url-status=dead}}</ref> In November 2008, [[SpringSource]] acquired the Groovy and Grails company (G2One).<ref>{{cite web|url=http://www.indicthreads.com/2138/springsource-acquires-groovy-and-grails-company-g2one/|title=SpringSource Acquires Groovy and Grails company (G2One)|date=11 Nov 2008}}</ref> In August 2009 [[VMware]] acquired SpringSource.<ref> {{cite web|url=https://techcrunch.com/2009/08/10/vmware-acquires-springsource/|title=VMWare Acquires SpringSource|date=10 Aug 2009}}</ref> In April 2012, after eight years of inactivity, the Spec Lead changed the status of JSR 241 to dormant.<ref name="JSR" /> Strachan had left the project silently a year before the Groovy 1.0 release in 2007.{{citation needed|date=May 2012}} In Oct 2016, Strachan stated "I still love groovy (jenkins pipelines are so groovy!), java, go, typescript and kotlin".<ref>{{cite web |url=https://twitter.com/jstrachan/status/784333918078169088 |title=Tweet from James Strachan |date=November 24, 2016 |access-date=2016-11-24}}</ref> On July 2, 2012, Groovy 2.0 was released, which, among other new features, added static compiling and [[static type checking]]. When the [[Pivotal Software]] joint venture was spun-off by [[EMC Corporation]] (EMC) and VMware in April 2013, Groovy and Grails formed part of its product portfolio. Pivotal ceased sponsoring Groovy and Grails from April 2015.<ref name="blog.pivotal.io"/> That same month, Groovy changed its governance structure from a Codehaus repository to a Project Management Committee (PMC) in the [[Apache Software Foundation]] via its incubator.<ref name="Groovy Incubator"/> Groovy graduated from Apache's incubator and became a top-level project in November 2015.<ref>{{cite web|title=Announcement on dev mailing list|url=http://mail-archives.apache.org/mod_mbox/groovy-dev/201511.mbox/%3CCAEWfVJ%3DBz-tiTTYHiPc8vY26CopLm3pPy_LADvvFh4vjVs%3Dosw%40mail.gmail.com%3E}}</ref> On February 7, 2020, Groovy 3.0 was released.<ref>{{Cite web |title=Release GROOVY_3_0_0 · apache/groovy |url=https://github.com/apache/groovy/releases/tag/GROOVY_3_0_0 |access-date=2024-03-27 |website=GitHub |language=en}}</ref> Version 4.0 was released on January 25, 2022.<ref>{{Cite web |title=Release GROOVY_4_0_0 · apache/groovy |url=https://github.com/apache/groovy/releases/tag/GROOVY_4_0_0 |access-date=2024-03-27 |website=GitHub |language=en}}</ref> ==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. ==Adoption== Notable examples of Groovy adoption include: * [[Apache OFBiz]], the [[Open-source software|open-source]] [[enterprise resource planning]] (ERP) system, uses Groovy.<ref>{{cite web|title=Groovy DSL for OFBiz business logic|url=https://cwiki.apache.org/confluence/display/OFBIZ/Groovy+DSL+for+OFBiz+business+logic|website=Apache OFBiz Project Open Wiki}}</ref><ref>{{cite web|title=Simple-methods examples using Groovy|url=https://cwiki.apache.org/confluence/display/OFBIZ/Simple-methods+examples+using+Groovy|website=Apache OFBiz Project Open Wiki}}</ref> * [[Eucalyptus (software)|Eucalyptus]], a cloud management system, uses a significant amount of Groovy. * [[Gradle]] is a popular build automation tool using Groovy. * [[LinkedIn]] uses Groovy and Grails for some of their subsystems.<ref>{{Cite web|title = Grails at LinkedIn|url= http://blog.linkedin.com/2008/06/11/grails-at-linkedin/|access-date= 2015-06-02}}</ref> * [[Jenkins (software)|Jenkins]], a platform for [[continuous integration]]. With version 2, Jenkins includes a ''Pipeline'' plugin that allows for build instructions to be written in Groovy.<ref>{{Cite web|url=https://jenkins.io/doc/book/pipeline/overview/|title=Jenkins Pipeline}}</ref> * [[Sky.com]] uses Groovy and Grails to serve massive online media content.<ref>{{Cite web|title = Graeme Rocher's Blog: Sky.com relaunches written in Grails|url= http://graemerocher.blogspot.com.br/2008/10/skycom-relaunches-written-in-grails.html|website= Graeme Rocher's Blog|date= October 2, 2008|access-date= 2015-06-02|first= Graeme|last= Rocher}}</ref> * [[SmartThings]], an open platform for [[smart home]]s and the consumer [[Internet of Things]], uses a security-oriented subset of Groovy<ref>[https://iotsecurity.eecs.umich.edu/img/Paper27_CameraReady_SmartThings_Revised_IEEEGen.pdf Security Analysis of Emerging Smart Home Applications]</ref> * [[SoapUI]] provides Groovy as a language for webservice tests development.<ref>{{Cite web|title = Scripting and the Script Library {{!}} Scripting & Properties|url= http://www.soapui.org/Scripting-Properties/scripting-and-the-script-library.html|website= www.soapui.org|access-date= 2015-06-02}}</ref> * [[Wired.com]] uses Groovy and Grails for the Product Reviews standalone section of the website.<ref>{{Cite web|title = Wired.Com|url= http://www.springsource.org/files/uploads/all/pdf_files/customer/S2_CaseStudy_Wired_USLET_EN.pdf|website= www.springsource.org|access-date= 2015-06-02}}</ref> * [[XWiki]] SAS uses Groovy as scripting language in their collaborative open-source product.<ref>{{Cite web|title = XWiki SAS|url= http://www.springsource.org/files/uploads/all/pdf_files/customer/S2_CaseStudy_XWiki.pdf|website= www.springsource.org|access-date= 2015-06-02}}</ref> ==IDE support== Many [[integrated development environment]]s (IDEs) and [[text editor]]s support Groovy: * [[Android Studio]], IDE used for making [[Android (operating system)|Android]] apps * [[Atom (text editor)|Atom]] editor * [[Eclipse (software)|Eclipse]], through Groovy-Eclipse * [[Emacs]], using the groovy-emacs-mode project's groovy-mode. * [[IntelliJ IDEA]], Community Edition, Grails/Griffon in the Ultimate Edition only * [[JDeveloper]], for use with [[Oracle ADF]] * [[jEdit]], an advanced text editor for the [[Java (software platform)|Java platform]] * [[Kate (text editor)|Kate]], an advanced text editor for [[KDE]] supports Groovy and over 200 other file formats * [[NetBeans]], since version 6.5 * [[Notepad++]], an advanced text editor for [[Microsoft Windows]] * [[Sublime Text]], a cross platform text editor * [[TextMate]] * [[Visual Studio Code]] * [[UltraEdit]], general purpose program editor ==Dialects== There is one alternative implementation of Groovy: * Grooscript converts Groovy code to [[JavaScript]] code.<ref>{{cite web|url=http://grooscript.org/doc.html|title=Grooscript Documentation|date=12 Sep 2016|access-date=4 July 2017|archive-url=https://web.archive.org/web/20170628212516/http://grooscript.org/doc.html|archive-date=28 June 2017|url-status=dead}}</ref> Although Grooscript has some limitations compared to Apache Groovy, it can use domain classes in both the server and the client.<ref>{{cite web|url=https://www.infoq.com/presentations/grooscript|title=Presentation at SpringOne/2GX on Grooscript|date=13 Dec 2015}}</ref> Plugin support for [[Grails (framework)|Grails]] version 3.0 is provided, as well as online code conversions.<ref>{{cite web|url=http://grooscript.org/conversions.html|title=Grooscript online conversions|date=15 May 2017|access-date=4 July 2017|archive-url=https://web.archive.org/web/20170709170154/http://grooscript.org/conversions.html|archive-date=9 July 2017|url-status=dead}}</ref> ==See also== {{Portal|Free and open-source software|Computer programming}} * [[Comparison of programming languages]] * [[Griffon (framework)]] – a desktop framework * [[WebSphere sMash|Project Zero]] * [[Spock (testing framework)]] == References == === Citations === {{Reflist|30em}} === Sources === {{Refbegin}} * {{cite book |first= Dierk |last= König |author2=Paul King |author3=Guillaume Laforge |author4=Hamlet D'Arcy |author5=Cédric Champeau |author6=Erik Pragt |author7=Jon Skeet |title= Groovy in Action, Second Edition |publisher= Manning |year= 2015 |isbn= 978-1-935182-44-3}} * {{cite book |first= Kenneth |last= Barclay |author2= John Savage |title= Groovy Programming: An Introduction for Java Developers |url= http://www.elsevier.com/wps/find/bookdescription.cws_home/709814/description#description |isbn= 978-0-12-372507-3 |access-date= 2007-07-19 |archive-url= https://web.archive.org/web/20100112040339/http://www.elsevier.com/wps/find/bookdescription.cws_home/709814/description#description |archive-date= 2010-01-12 |url-status= dead |year= 2007 |publisher= Morgan Kaufmann Publishers }} * {{cite book |first= Scott |last= Davis |title= Groovy Recipes: Greasing the Wheels of Java |isbn= 978-0-9787392-9-4 |url= https://archive.org/details/isbn_9780978739294 |year= 2008 |publisher= Pragmatic Bookshelf }} * {{cite book |first= Venkat |last= Subramaniam |title= Programming Groovy: Dynamic Productivity for the Java Developer |isbn= 978-1-934356-09-8 |url= https://archive.org/details/programminggroov0000subr |year= 2008 |publisher= Pragmatic Bookshelf }} {{Refend}} ==External links== * {{Official website}} {{Apache Software Foundation}} {{Programming languages}} {{Java (Sun)}} {{Authority control}} [[Category:Articles with example code]] [[Category:Dynamically typed programming languages]] [[Category:Java programming language family]] [[Category:Java specification requests]] [[Category:JVM programming languages]] [[Category:Object-oriented programming languages]] [[Category:Programming languages created in 2003]] [[Category:Scripting languages]] [[Category:Software using the Apache license]] [[Category:Programming languages]] [[Category:Dynamic programming languages]] [[Category:2003 software]] [[Category:Cross-platform free software]] [[Category:Cross-platform software]] [[Category:High-level programming languages]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Apache Software Foundation
(
edit
)
Template:Authority control
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Infobox programming language
(
edit
)
Template:Java (Sun)
(
edit
)
Template:Official website
(
edit
)
Template:Portal
(
edit
)
Template:Programming languages
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)