MyArchiBook

Archive for February 2014

New DelegatingScript Base Class – Groovy 2.2 feature

leave a comment »

Script base class of Compiler Configuration can be customized.
Delegating Script Base Class helps to delegate the calls in a script to a class that has DSL methods/functions & variables.

import org.codehaus.groovy.control.CompilerConfiguration

def scriptInfo='''
        model="Nexus 5"
        getModel()
        '''

class MobileDealer{
    String model
    void getModel(){
        println "Mobile : $model"
    }
}

def compilerConfig=new CompilerConfiguration()
compilerConfig.scriptBaseClass=DelegatingScript.class.name
def shell=new GroovyShell(compilerConfig)
def script=shell.parse(scriptInfo)
MobileDealer m=new MobileDealer()
script.setDelegate(m)
script.run()
  • Line 1 – import package of Compiler Configuration class
  • Line 3-6 has the script where the delegate is set and is run finally.
  • Line 8-13 has the class declaration of the delegate class MobileDealer. The methods and variables in this class can be reused commonly in scripts.
  • Line 15new instance of compiler configuration is created
  • Line 16DelegatingScript is set as the base class for that particular compilerConfiguration instance
  • Line 17-new groovyShell instance is created with the present compiler Configuration
  • Line 18-The above “scriptInfo” is parsed to DelegatingScript type
  • Line 20-MobileDealer class is set as delegate in the script.
  • Line 21-the script is run.

REFERENCE:
http://jira.codehaus.org/browse/GROOVY-6076

Advertisements

Written by thangaveluaishwarya

February 14, 2014 at 12:32 PM

Groovy-2.X tidbits

leave a comment »

Did you know …?

1.We can know the last element of an array by using -1 as the index
Eg:
def a=[1,2,3,4,5,6,7]
assert a[-1]==7

2.Groovy is much like an English speaking language
Eg :   for loop
Earlier in Java,

for(int i=1;i<=10;i++){
 System.out.println("Java");
}

*NOTE: Now that Java 8 has introduced Instream for loop with range.

In Groovy,

10.times{
 println 'Groovy'
}

3.Default access specifiers

  • for Class -> public
  • for Method -> public
  • for variables -> private

4.Groovy generates 2 constructors implicitly
(1) a public constructor
(2) a public constructor where its class variables can be passed as parameters.

5.Values in the constructor can be assgined via Implicit coersion and Explicit coersion too
Eg:

class Season{
def type
def month
}

//Normal way
def s1=new Season(type:"winter",month:"December")

//Implicit coersion - pre-defining the type of variable.
Season s3=[type:"autumn",month:"September"]

//Explicit coersion using 'as'
def s2=[type:"summer",month:"May"] as Season

6.Underscores can be used in number literals for better understanding
Eg:

long creditCardNumber=1234_2345_3456_4567L

Advancements in languages is happening day by day.
Groovy beta version has come up with advanced features like Delegating scripts, TypeChecking Extensions in Script, project coin(present in Java7) and etc.,However Java 8 is also coming up with much advanced features.

We should bear in mind that language should be only the means to make things simpler.
It’s wise to choose the right one based on our needs.

Written by thangaveluaishwarya

February 9, 2014 at 2:46 PM