Monday, December 24, 2012

Groovy Tips

Here are some productivity tips for Groovy development. Provided you have GROOVY_HOME/bin added to your path you can execute following commands:

  • running Groovy shell
groovish

Type 'help' or '\h' for help.
-----------------------------------------------------------------------------------------------------------
groovy:000>
you can e.g. type: 
   
Sting.methods.each{println it.name }

  • running graphic console

groovyConsole


  • running uncompiled Grovy on web server
1. Add groovy.jar from $GROOVY_HOME/embedded to server classpath (WEB-INF/lib)


2. Add groovy.servlet.GroovyServlet to WEB-INF/web.xml.
3. Place your Groovy scripts wherever you'd normally place your JSP files.
4. Create hyperlinks to your Groovy scripts


  • automatic imports
Groovy automatically imports:import packages:
java.lang.*
java.util.*
java.net.*
java.io.*
java.math.BigInteger
java.math.BigDecimal
groovy.lang.*
groovy.util.*


  • method pointers
In Groovy you can do the following:
def shoppingList = []
def add = shoppingList.&add
def remove = shoppingList.&remove
add "Milk"
add "Bread"
add "Beer"
remove "Beer"
add "Apple Juice"
print shoppingList


  • operators
The following are operators and corresponding methods:
OperatorMethod
a == b
   or
a != b
a.equals(b)
a+ba.plus(b)
a-ba.minus(b)
a*b a.multiply(b)
a/b a.div(b)
a%ba.mod(b)
a++
   or
++a 
a.next()
a--
   or
--a 
a.previous()
a&ba.and(b)
a|b a.or(b)
a[b]a.getAt(b)
a[b] = ca.putAt(b,c)
a << b a.leftShift(b)
a >> ba.rightShift(b)
a < b
   or
a > b
   or
a <= b
   or
a >= b
a.compareTo(b)






No comments:

Post a Comment