...
...
Note |
---|
You can Skip this tutorial if you already know Groovy. You can refer to this page as a high-level reference to Groovy concepts and go ahead with the tutorials to start writing Groovy scripts in JMWE. |
...
. |
...
Table of Content Zone | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||
SyntaxComments in GroovyGroovy supports single and multi-line comments. Single line comments start with // and can be found at any position in the line. Multi-line comments start with /* and */
KeywordsWhile writing your scripts make sure you do not use the existing keywords of the Groovy language like break, package etc to name your variables or constants. See here for the list of keywords. String ConcatenationThe Groovy strings can be concatenated with a + operator.
You can use the backslash character \ to escape the quotes
SemicolonsSemicolons are used to separate statements, but they are optional in Groovy as long as you write one statement per line. If you use multiple statements on the same line you can either use a semicolon or a newline to separate the statements. Code Block | | ||||||||||||||||||||||||||||
|
This section of the document covers the syntax, keywords, operators, etc in the Groovy language.
Table of Content Zone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Program structureThis section of the document gives an intorduction on the program structure in Groovy. You can refer to the official documentation of the Groovy language or any Java tutorial for complete information. PackagesA package is a group of similar types of classes, interfaces and sub-packages. It is used to categorize the classes and interfaces so that they can be easily maintained. See here to know more about pacakges. Definfing a package:
To refer to some class ImportsIn order to refer to any class you need a qualified reference to its package. Groovy follows Java’s notion of allowing For example
Default imports: Groovy automatically imports the packages ObjectsIn Groovy everything is an object that has methods and properties.
MethodsA method is a set of code which is referred to by name and can be invoked with parentheses Accessing a method You can access a method using the "." dot syntax as shown in the below example, where employee is the object that has accessors or methods to get Employee data
Direct access operator You can use the direct access operator to call the property directly removing the paranthesis () and instead of using the getter.
Script classA script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a
this will be compiled as if it was the following:
VariablesVariables are typically used to store information which your Groovy script needs to do its job on. See here for more information on Variables and their types. Variable declaration You can declare a variable either by specifiying its type or without specifying using def. The latter can be done using
AnnotationsAnnotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM. In Groovy, an annotation can be used to alter the semantics of the language. It is especially true of AST transformations which will generate code based on annotations. SimpleData types in GroovyGroovy offers a wide variety of built-in data types. Following is a list of data types which are defined in Groovy Booleans
Define a boolean object:
Groovy has special rules to coerce non-boolean objects to a boolean value, referred as Truthy and Falsy. Groovy decides an expression as
Any other value is considered Falsy. Eg: An empty string. CollectionsThis section lists some of the collections in Groovy. See here for detailed information. ListsGroovy uses a comma-separated list of values, surrounded by square brackets, to denote lists. Groovy lists are plain JDK Initialising a list: You can initialise a list delimiting the values by commas and surrounded by square brackets. You can also create lists containing values of heterogeneous types:
Accessing a list:
SetA collection that contains no duplicate elements. Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. All these permit null values too. HashSet: A HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface. HashSet stores the elements by using a mechanism called hashing. HashSet contains unique elements only. LinkedHashSet: LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface. Initialising a Set/HashSet:
MapsGroovy features maps. Maps associate keys to values, separating keys and values with colons, and each key/value pairs with commas, and the whole keys and values surrounded by square brackets. Initialising a Map: You need pass quoted strings if your key string isn’t a valid identifier Eg: "block-no". You must surround the variable or expression with parentheses, when you need to pass variable values as keys in your map definitions. Eg:
Accessing the elements of a Map:
ArraysGroovy reuses the list notation for arrays, but to make such literals arrays, you need to explicitly define the type of the array through coercion or type declaration. You can also create multi-dimensional arrays
Initialising an Array :
Accessing the elements of an array:
OperatorsGroovy supports all the typical operators, such as arithmetic operators, assignment operators, logical operators, relational operators and conditional operator. See below examples using some of the operators.
There a few other notable operators that are specific only to the Groovy language; one of them is the Safe Navigation Operator. Safe navigation operatorThe Safe Navigation operator is used to avoid a NullPointerException. When you have a reference to an object you might need to verify that it is not Normally you would have to:
Instead, you can write it as:
Groovy Control structures Groovy supports the usual if-else, "nested" if then else if, while loop, exception handling syntax.
Switch statement - The switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement in Groovy can handle any kind of switch value and different kinds of matching can be performed.
In Groovy it is backward compatible with Java code, one difference though is that the Groovy switch statement can handle any kind of switch value and different kinds of matching can be performed. for loop - for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop. In Groovy the for loop is much simpler and works with any kind of array, collection, Map, etc.
Optional omitting of syntaxGroovy allows you to leave out some elements of syntax like package prefix, parentheses, and semicolon which brings the code to the bare minimum. |
...