For more blog posts from JavaOne, see the table of contents. Can specify a resolver as first parameter if want to use an alternate one. Not worth it though because then as much work as setting up a project. My take: I like that he showed code that he wrote and pointed out idioms.
|Published (Last):||16 March 2008|
|PDF File Size:||8.99 Mb|
|ePub File Size:||11.51 Mb|
|Price:||Free* [*Free Regsitration Required]|
Shows you best practices and idiomatic solutions to help you achieve solutions that fully leverage the power of the Groovy language. This reference card provides exactly the kind of information you are likely to look up when programming Groovy.
The groovy command comes with -h and --help options to show all options and required arguments. Typical usages are:. From Java, you can call Groovy code in the following ways.
Note that you need to have the groovy-all. This enables you to use your Groovy code as if it was written in Java. Use class groovy. Eval for evaluating simple code that is captured in a Java String: int Eval. Use groovy. GroovyShell for more flexibility in the Binding and optional pre-parsing:. Chapter 11 of Groovy in Action has more details about integration options. Here is an overview:.
When a. Scripts differ from classes in that they have a Binding that serves as a container for undeclared references that are not allowed in classes. Static types can be used like in Java and will be obeyed at runtime. Dynamic typing is used by replacing the type declaration with the def keyword. Formal parameters to method and closure declarations can even omit the def. Properties are declared as fields with the default visibility modifier , no matter what type is used.
Actively look for opportunities to implement operator methods in your own Groovy class. This often leads to more expressive code. See also Ranges. Coercion rules for math operations are explained in Groovy in Action, chapter 3.
Some examples to remember are:. Placeholders in GStrings are dereferenced at declaration time but their text representation is queried at GString -pString conversion time. Ranges appear inclusively like They are often enclosed in parentheses since the range operator has low precedence. Integer ranges are often used for selecting sublists.
Range boundaries can be of any type that defines previous , next and implements Comparable. Notable examples are String and Date. Sublist assignments can make a list grow or shrink and lists can contain varying data types. Maps are like lists that have an arbitrary type of key instead of integer.
Therefore, the syntax is very much aligned. Maps can be accessed in a conventional square-bracket syntax or as if the key was a property of the map. Closures capture a piece of logic and the enclosing scope. They are first-class objects and can receive messages, can be returned from method calls, stored in fields, and used as arguments to a method call.
Use as in. Every object is iterable in Groovy,even if it was implemented in Java. See Groovy in Action, chapter 9 on what strategy Groovy applies to make this happen. Implement the iterator method that returns an Iterator object to give your own Groovy class meaningful iterable behavior with the above methods. Transform with closure returning the replacement and filter with closure returning boolean. Decide to use the parser for state-based processing or the slurper for flow-based processing. The parse methods of parser and slurper return different objects Node vs.
GPathResult but you can apply the following methods on both:. When a query contains wildcards, it is wise to use a PreparedStatement. Groovy SQL does this automatically when you supply either the list of values in an extra list or when the statement is a GString.
So each method below has three variants:. Group of methods assigned at runtime to arbitrary classes that fulfill a common purpose. Applies to one thread. Scope is limited to a closure. Additionally, implement the interface GroovyInterceptable to intercept also calls to available methods. Instead of implementing the above methods, they can also be added to the MetaClass of any arbitrary class or object to achieve the same effect. Over a million developers have joined DZone.
Refcard Groovy. A Rapid-Development JVM Language Shows you best practices and idiomatic solutions to help you achieve solutions that fully leverage the power of the Groovy language. Published: Oct. Written by. Table of Contents. Section 1. Section 2. Typical usages are: Execute file MyScript. PI" Print p for each line of input echo PI" Inline edit i file data. Section 3.
From Groovy, you can call any Java code like you would do from Java. It's identical. Eval Use class groovy. Section 4. Properties Properties are declared as fields with the default visibility modifier , no matter what type is used. Properties are referred to like println obj. Section 5. Section 6. Numbers All Groovy numbers are objects, not primitive types. Literal declarations are: Type Example literals java. Integer 15, 0xffff java. Long L, l java. Float 1. Double 1. BigInteger g, G java.
BigDecimal 1. Section 7. Ranges Ranges appear inclusively like Lists Lists look like arrays but are of type java. List plus new methods. Entry objects into account. To do the same with method calls, use the spread-dot operator. Closures Closures capture a piece of logic and the enclosing scope.
Section 8. Methods for java. Object Get object info println obj. Section 9. GPathResult but you can apply the following methods on both: result. MarkupBuilder b. Section Connecting to the DB Getting a new Sql instance directly. Sql source Submitting Queries When a query contains wildcards, it is wise to use a PreparedStatement. Categories Group of methods assigned at runtime to arbitrary classes that fulfill a common purpose.
A bit easier to handle are the variants Object methodMissing String name, Object args Object propertyMissing String name, Object args that are called like the name suggests.
Like This Refcard?
Groovy Refcard Available - Download Now
Index of /refcards/refcards/java
Groovy Reference Card