Grails

Static Type Checking in Groovy 2.0

Groovy is a dynamic language. We can't check any typo error, method or property missing error at compile time. To check typo error, method or  property missing at compile time, Groovy 2.0 introduces new features named as Static Type Checking which helps to check errors at compile time. To use static type checker in groovy You have to...

by Mohit Garg
Tag: Groovy
25-Sep-2012

Grails

How to implement AOP Profiling in Grails application

In one of my recent project, i want to profile method execution time. I have used Spring AOP to profile method execution time. It's very easy to implement AOP profiling in grails 1. Suppose we have below mentioned service class, we want to log execution time of method saveDataStudent,saveDataUpdated methods in service class. We need...

by Mohit Garg
Tag: Groovy
25-Sep-2012

Grails

Inject Methods/Properties to be available in all GSP Pages

Hi,   There could be cases where we would need to inject some methods or properties in all GSP pages.   As we know that the GSP pages extends GroovyPage class, hence injecting properties/methods in GroovyPage class can solve out purpose.   Example: Lets add appVersion property and a method to provide logo link...

by Kushal Likhi
Tag: Groovy
25-Sep-2012

Grails

Enhance HttpServletRequest using metaprogramming

Most of the times we use request.getHeader("user-agent") to get the client browser user-agent details inside the Grails Filters or Controllers. User-agent is one of the examples. There are so many such methods e.g. method for checking whether the request was an ajax call, getting IP address of the client machine, checking whether the...

by Bhagwat Kumar
Tag: Groovy
25-Sep-2012

Grails

Dollar-Slashy Strings in Groovy [Ver 1.8+]

Hi,   Groovy took another leap ahead in the methodology of defining Strings by providing us with "dollar-slashy" strings which further minimized the need of escaping any special character as compared to simple "slashy" Strings.   In the Groovy ver:1.8+ you can define Strings as follows: [java] //A dollar-slashy string ...

by Kushal Likhi
Tag: Groovy
25-Sep-2012

Grails

GDSL Awesomeness – Setting a global context

Hi, Sometimes we need to set the context for the GDSL contributor to all the possible files and scripts in the project scope. This is a rare case scenario, but could be handy on some implementations.   This can be done by two techniques: Ignoring all filters passed to the "context" method. Setting the "ctype"...

by Kushal Likhi
Tag: Groovy
25-Sep-2012

Grails

GDSL Awesomeness – Introduction to GDSL in IntelliJ Idea

One of the disadvantage of using meta-programming and DSL is that we don't get auto-completes in Our IDE. Hence IntelliJ provided us with an awesome and intelligent toolkit to handle dynamic properties and methods when writing code in groovy. In IntelliJ IDEA we can write GDSL Files in order to give us the auto-completes(code...

by Kushal Likhi
Tag: Groovy
25-Sep-2012

Grails

GDSL Awesomeness – Defining dynamic properties in a class

We can define dynamic properties in a class as follows:   Suppose we have injected a property named "UTCTimeInMills" in Date class which is of type "long". Now we need to enable syntax hinting for it, We can do it as follows: (See inline comments for description) [java] //First we need to define a context def...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Defining dynamic method in a class

We can define dynamic methods in a class as follows:   Suppose we have injected a method name .clone([boolean] clearTime) in Date which clones the date object and returns the cloned one. This method takes an boolean argument argument too. We can do it as follows: (See inline comments for description) [java] //First we...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Understanding Context And Contributors in Detail

GDSL Scripts are based on two main concepts: contexts and contributors. Hence we will discuss about these in detail here.   In GroovyDSL context is an entity which is supposed to give an answer to the question "Where?" this behavior will be available. Contexts are first-order citizens in GroovyDSL, so they may be stored in local...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Adding Hinting for missingMethod implementation

hi,   Suppose we have used method missing in our code and we want to give syntax-hinting for these methods then we can go as follows:   Suppose we have this class which implements method missing: [java] class Pixel { def Top def Left def Color def methodMissing(String name, args) { //...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Delegating Closure Calls

hi,   Very often we use delegates in our closure calls. It would be great if we could get code-hinting in the closure via the delegates.   Now let us take a very simple scenario, where we made a method "with" which delegates the delegate objet to closure and runs it. [java] class Delegator { static def...

by Kushal Likhi
Tag: Groovy
24-Sep-2012