Groovy (programming language)

Groovy (programming language)

Infobox programming language
name = Groovy

paradigm = object-oriented, scripting
year = 2003
designer = JCP
developer = Guillaume Laforge (Project Manager and JSR-241 Spec Lead)
latest_release_version = 1.5.7
latest_release_date = release date|2008|10|09
latest_test_version = 1.6-beta-2
latest_test_date = release date|2008|10|09
typing = dynamic, strong, duck
implementations =
dialects =
influenced_by = Python, Ruby, Perl, Smalltalk, Java
influenced =
operating_system = Cross platform (JVM)
license = Apache License V2.0
website = http://groovy.codehaus.org

Groovy is an object-oriented programming language for the Java Platform as an alternative to the Java programming language. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform.

Groovy uses a Java-like curly bracket syntax which is dynamically compiled to Java Virtual Machine bytecodes and which works seamlessly with other Java code and libraries. The Groovy compiler can be used to generate standard Java bytecode to be used by any Java project. Most Java code is valid Groovy syntax and can be used dynamically as a scripting language.

Groovy is currently undergoing standardization via the Java Community Process under [http://www.jcp.org/en/jsr/detail?id=241 JSR 241] . Groovy 1.0 was released on January 2,2007.

Features

Groovy has a number of features not found in standard Java:

* Dynamic typing
* Closures
* Operator overloading

Syntax comparison

The following presents a side-by-side comparison ofJava with Groovy:

Standard Java (Java 5+) for (String item : new String [] {"Rod", "Carlos", "Chris"}) { if (item.length() <= 4) { System.out.println(item);

Groovy ["Rod", "Carlos", "Chris"] .findAll{it.size() <= 4}.each{println it}

Mark-up language support

One noteworthy feature of Groovy is its native support for various markup languages such as XML and HTML, accomplished via an inline 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. For example:

the following Groovy code ...

import groovy.xml.MarkupBuilder def writer = new StringWriter() def myXMLDoc = new MarkupBuilder(writer) myXMLDoc.workbook { worksheet(caption:"Employees") { row(fname:"John", lname:"McDoe") row(fname:"Nancy", lname:"Davolio") } worksheet(caption:"Products") { row(name:"Veeblefeetzer", id:"sku34510") row(name:"Prune Unit Zappa", id:"sku3a550") } } println writer.toString()

... produces the XML result:

For the sake of comparison, Java code to produce the equivalent XML is shown below. Note that, unlike the Groovy example, each XML element and attribute is created with an explicit method call.

equivalent Java code:

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;import org.w3c.dom.Element;

public class XMLFun {

public static void main(String [] args) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", 2); Transformer trans = factory.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes");

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element workbook = doc.createElement("workbook"); doc.appendChild(workbook); Element worksheet = doc.createElement("worksheet"); worksheet.setAttribute("caption", "Employees"); workbook.appendChild(worksheet); Element row = doc.createElement("row"); row.setAttribute("fname", "John"); row.setAttribute("lname", "McDoe"); worksheet.appendChild(row); row = doc.createElement("row"); row.setAttribute("fname", "Nancy"); row.setAttribute("lname", "Davolio"); worksheet.appendChild(row);

worksheet = doc.createElement("worksheet"); worksheet.setAttribute("caption", "Products"); workbook.appendChild(worksheet); row = doc.createElement("row"); row.setAttribute("name", "Veeblefeetzer"); row.setAttribute("id", "sku34510"); worksheet.appendChild(row); row = doc.createElement("row"); row.setAttribute("name", "Prune Unit Zappa"); row.setAttribute("id", "sku3a550"); worksheet.appendChild(row);

StringWriter writer = new StringWriter(); StreamResult out = new StreamResult(writer); DOMSource dsource = new DOMSource(doc); trans.transform(dsource, out); System.out.println(writer.toString());

History

James Strachan first talked about the development of Groovy in his [http://radio.weblogs.com/0112098/2003/08/29.html blog] in August 2003. Several versions were released between 2004 and 2006. After the JCP standardization process began, the version numbering was changed and a version called "1.0" was released on Tuesday, January 2, 2007.After various betas and release candidates numbered 1.1, on December 7 2007 Groovy 1.1 Final was released and immediately rebranded as Groovy 1.5 as a reflection of the great improvement made.

Integrated development environment

Many IDEs support Groovy.


= Eclipse =

* Methods are completed, if you specify the type of variables.
* Debugger
* Update site is http://dist.codehaus.org/groovy/distributions/update/

IntelliJ IDEA

There are at least two plugins, but currently developed is the one released by JetBrains, Jet Groovy Plugin [http://www.jetbrains.com/idea/features/groovy_grails.html] .
* Code completion
** You can complete Java code and Groovy code mutually. Not only you can complete Java classes from Groovy code, but also you can complete Groovy class from Java code in real time. You can mix Java and Groovy code in a project.
* Jump to the definition by Ctrl with mouse click.
** As same as completion, you can move between Java code and Groovy code.
* Debugger
* Quick-fix for coding errors.
* Completions for GroovyDoc.
* Refactoring such as extracting method.
* Support for Grails and GSP.

NetBeans

Starting from version 6.5, NetBeans supports Groovy and Grails.

See also

* Comparison of programming languages
* Jython
* BeanShell
* Grails (framework)
* Pnuts
* ZK Framework

References

*
*
*

External links

* [http://groovy.codehaus.org/ Official site]
* [http://www.jcp.org/en/jsr/detail?id=241 JSR 241]
* [http://www-128.ibm.com/developerworks/java/library/j-alj08034.html An introduction to Groovy]
* [http://dmoz.org/Computers/Programming/Languages/Java/Extensions/Groovy/ Open Directory: Java: Extensions: Groovy]
* [http://groovy.codehaus.org/Groovy+for+the+Office Groovy for the Office]
* [http://groovy.dzone.com/ Groovy Zone - DZone Groovy news aggregator]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Java (programming language) — infobox programming language name = Java paradigm = Object oriented, structured, imperative year = 1995 designer = Sun Microsystems latest release version = Java Standard Edition 6 (1.6.0) latest release date = latest test version = latest test… …   Wikipedia

  • Python (programming language) — infobox programming language name = Python paradigm = multi paradigm: object oriented, imperative, functional year = 1991 designer = Guido van Rossum developer = Python Software Foundation latest release version = 2.6 latest release date =… …   Wikipedia

  • Dynamic programming language — This article is about a class of programming languages, for the method for reducing the runtime of algorithms, see Dynamic programming. Dynamic programming language is a term used broadly in computer science to describe a class of high level… …   Wikipedia

  • Curl (programming language) — Curl Paradigm(s) multi paradigm: object oriented, markup Appeared in 1998 Designed by Steve Ward, MIT Developer …   Wikipedia

  • Mirah (programming language) — Mirah Paradigm(s) object oriented, imperative Appeared in 2008 Typing discipline static, with dynamic features, strong, inferred Influenced by Ruby, Java, Boo …   Wikipedia

  • Einstein (programming language) — Introduction Einstein is an open source forth generation programming language (4GL) written on top of the Deesel 3GL programming language. It is a flow based programming language supporting message based constructs such… …   Wikipedia

  • Neko (programming language) — Neko Paradigm(s) object oriented, structured, classless, scripting Appeared in 2005 Developer Nicolas Cannasse …   Wikipedia

  • Boo (programming language) — Infobox programming language name = Boo paradigm = Object oriented year = 2003 designer = Rodrigo B. De Oliveira developer = Rodrigo B. De Oliveira latest release version = 0.8.2 latest release date = 2008 05 20 typing = static, strong, duck… …   Wikipedia

  • Shakespeare Programming Language — Pour les articles homonymes, voir Shakespeare (homonymie). Le Shakespeare Programming Language ou SPL est un langage de programmation créé par Karl Hasselström et Jon Åslund en février 2001 dont le code source ressemble à une pièce de théâtre. Il …   Wikipédia en Français

  • Groovy — Появился в: 2003 Автор(ы): Джеймс Стрэчен Релиз: 2.0.5 (4 октября 2012 года) Типизация данных …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”