introduction a groovy
DESCRIPTION
Groovy nos entrega una nueva generación de lenguajes scripting que permite el desarrollo de aplicaciones en menos tiempo y de manera mucho mas legibles. Además la con la metaprogramacion de Groovy , permite extender las funcionalidades de la API de Java en Runtime.TRANSCRIPT
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.01
Introducción a Groovy Un lenguaje dinámico para la JVM
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.02
Agenda
• Que es Groovy• Caracteristicas Lenguaje Groovy• Sintaxis en Groovy • Comunidad Groovy• Laboratorios (1-13)
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.03
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinámicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.04
Agenda
• Que es Groovy• Caracteristicas Lenguaje Groovy• Sintaxis en Groovy • Comunidad Groovy• Laboratorios (1-12)
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.05
Que es Groovy?• Groovy es un agil y dinámico lenguaje para la Java
Virtual Machine • Construido pensando en Java pero adicionando las
poderosas caracteristicas de lenguajes como Python, Ruby & Smalltalk.
• Entregas atractivas caracteristicas disponibles para los desarrolladores de Java con una pequeña curva de pequeña curva de aprendizaje.aprendizaje.
• Soporta Domain Specific Languages y otras sintaxis compactas que facilitan tanto la lectura como la mantencion del codigo.
• Permite la integracion con todo los Objetos y librerias existentes desarrolladas en Java.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.06
Agenda
• Que es Groovy• Caracteristicas de Groovy• Sintaxis en Groovy • Comunidad Groovy• Laboratorios (1-12)
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.07
Caracteristicas de Groovy
• Closures• Sintaxis nativa para List y Maps• Soporte para GroovyMarkup y GPath • Soporte nativo para expresiones regulares• Soporte para tipificación dinámica y estática. • Soporta embeber expresiones dentro de string• Una gran cantidad de Metodos agregados al JDK. • Simple sintaxis para escribir Beans ( getter y setter )• Soporte para Anotaciones y Genericos
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.08
Agenda
• Que es Groovy• Caracteristicas de Groovy• Sintaxis en Groovy • Comunidad Groovy• Laboratorios (1-12)
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.09
Sintaxis en Groovy
• La keyword para la definición de variables es def
• Los punto y coma al final de las sentencias son opcionales println x def y = 5; def x = y + 7
• Las declaraciones puede ir en varias lineas
• Los comentarios para una linea con el caracter // ....
• Los comentarios para varias lineas son con el caracter /* ... */
• La sentencia return es opcional
• Los parentesis son opcionales
• Soporta el paso de parametros con nombre en las invocaciones
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.010
Sintaxis en Groovy
• Metodos y Clases en Groovy por defecto son public.• La palabra in es keyword • La palabra as es keyword• Se realizan import por default
* java.io.* * java.lang.* * java.math.BigDecimal * java.math.BigInteger * java.net.* * java.util.* * groovy.lang.* * groovy.util.*
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.011
Agenda
• Que es Groovy• Caracteristicas de Groovy• Sintaxis en Groovy • Recursos• Laboratorios (1-12)
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.012
Recursos
• Groovy Language, guides, examples http://groovy.codehaus.org
• Groovy Eclipse Plugin http://groovy.codehaus.org/Eclipse+Plugin
• Groovy Related News http://aboutgroovy.com http://groovyblogs.org http://groovy.dzone.com
• Andres Almiray’s Groovy Commiter http://jroller.com/aalmiray
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.013
Laboratorios Un lenguaje dinámico para la JVM
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.014
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.015
Lab 01 – Instalación de Groovy ( Linux y Windows)
• Download Groovy 1.5.6 stable
• http://groovy.codehaus.org/Download
• Linux Ubuntu/Debian http://dist.codehaus.org/groovy/distributions/installers/deb/groovy-1.5.6.deb
Install
# sudo dpkg –install groovy-1.5.6.deb
# groovy -version
Groovy Version: 1.5.6 JVM: 1.5.0_14-b03
• Windows http://dist.codehaus.org/groovy/distributions/installers/windows/nsis/groovy-1.5.6-install
er.exe
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.016
Lab 01 – Instalación de Groovy ( Linux y Windows)
• GroovyConsole ( Consola Gráfica de Groovy )print “hola” Ctrl + R
• groovysh ( Consola de Groovy )print “hola” Enter
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.017
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.018
Lab 02 – Eclipse y Groovy Plug-in
• Eclipse 3.3.1 Help >> Software Update >> Find and Install
* Search for new feature for install
• Plug-in site http://dist.codehaus.org/groovy/distributions/update/
• Detalles http://docs.codehaus.org/download/attachments/2744/Groovy_Installation_Eclipse_Plugin.pdf?version=1
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.019
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.020
Labs 03 – Hola Mundoclass HelloWorld {
String name
String greet()
{ return "Hello "+ name }
static void main( args ){
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
println( helloWorld.greet() )
}
}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.021
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.022
Labs 04 – Tipos dinámicos
• Utiliza el operador def , cuando no necesites tipificar la variable a utilizar. ( def es el equivalente al operador var en JavaScript)
• Groovy utilizara el tipo correcto para la variable.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.023
Labs 04 – Tipos dinámicosclass HelloWorld {
def name
def greet()
{ return "Hello "+ name }
static void main( args ){
def helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
println( helloWorld.greet() )
}
}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.024
Labs 04 – Tipos dinámicosclass Calculadora {
def suma(a,b)
{ return a+b }
def resta(a,b)
{ return a-b }
static void main( args ){
def calculadora = new Calculadora()
println calculadora.suma(5,5)
println calculadora.resta(5,5)
}
}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.025
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.026
Labs 05 - GString
• Groovy soporta la capacidad de incrustar variables dentro de String
• Capacidad de definir bloques de String entre los caracteres
“”” …. “””
• Capacidad de invocar APIs dentro de String
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.027
Labs 05 – GStringclass HelloWorld {
def name
def greet()
{ return "Hello ${name}" }
static void main( args ){
def helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
println( helloWorld.greet() )
}
}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.028
Labs 05 – GStringclass Noticia { def titulo def cuerpo
def publicar() { return “”” \t ${titulo} \n \t \t ${cuerpo} \n “”” } static void main( args ){ def noticia = new Noticia() noticia.setTitulo(“Liberacion de Groovy") noticia.setCuerpo(“Se libero la version 1.5.6 ") println( noticia.publicar() ) }}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.029
Labs 05 – GStringclass Noticia { def titulo def cuerpo
def publicar() { return “”” \t ${title} \n \t \t ${cuerpo} \n “”” }
static void main( args ){ def noticia = new Noticia() noticia.setTitulo(“Liberacion de Groovy") noticia.setCuerpo(“Se libero la version 1.5.6 ") println( noticia.publicar() ) }}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.030
Labs 05 – GStringclass Noticia { def titulo def cuerpo
def publicar() { return “”” \t ${titulo} \n \t \t ${cuerpo} \n “”” } static void main( args ){ def noticia = new Noticia() noticia.setTitulo(“Liberacion de Groovy") noticia.setCuerpo(“Se libero la version 1.5.6 ") println “${noticia.publicar()}” }}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.031
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.032
Labs 06 – POGOsclass Noticia { def titulo def cuerpo
static void main( args ){ def noticia = new Noticia() noticia.setTitulo(“Liberacion de Groovy”) noticia.setCuerpo(“Se libero la version 1.5.6 “) println “${noticia.getTitulo()}” println “${noticia.getCuerpo()}”
}}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.033
Labs 06 – POGOsclass Noticia { def titulo def cuerpo
static void main( args ){ def noticia = new Noticia() noticia.titulo = “Liberacion de Groovy” noticia.cuerpo = “Se libero la version 1.5.6 “ println “${noticia.titulo}” println “${noticia.cuerpo}”
}}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.034
Labs 06 – POGOsclass Noticia { def titulo def cuerpo
static void main( args ){ def noticia = new Noticia(titulo: “Liberacion de Groovy”, cuerpo: “Se libero la version 1.5.6“) println “${noticia.titulo}” println “${noticia.cuerpo}”
}}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.035
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.036
Labs 07 - Closures
• Bloque de código anónimo definido entre llaves, el cual toma argumentos y retorna valor.
• Si se omite el nombre del argumento , este recibe el nombre de it
• Los Closures siempre generan un return ,la cual sera por defecto la ultima sentencia evaluada.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.037
Labs 06 – Closures
class Noticia {
def titulo
def cuerpo
static void main( args ){
def salida = { println it }
def noticia = new Noticia(titulo: “Liberacion de Groovy”,
cuerpo: “Se libero la version 1.5.6“)
salida( “${noticia.titulo}”)
salida( “${noticia.cuerpo}”)
}
}
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.038
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.039
Labs 08 –Operadores
• ?: (elvis) - Una redifinicion del ternary operator
• ?. Safe dereference – Navegando por un objeto sin tener la preocupacion de obtener un NullPointerException.
• <=> (spaceship) – Comparacion de dos valores con un retorno a boolean.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.040
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.041
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.042
Labs 10 – GroovyMarkup y GPath
• Groovy tiene un sorpote para varios tipos de lenguajes de marcas como lo son : XML, HTML, SAX, W3C DOM, Ant tasks.
• GPath define una sintaxis para la navegacion por estructuras de datos.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.043
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramación• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.044
Labs 11 – Metaprogramación
• Escribir programas que escriben o manipulan a otros programas o a si mismos.
• Soporte para agregar metodos y propiedades a un objeto en runtime.
• Soporte para interceptar llamadas a metodos, simulando algo parecido a AOP.
• Groovy extiende clases como String y Integer con nuevos metodos, esto es conocido con el nombre de GDK.
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.045
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.046
Labs 12- GroovyServlet
• Permite el desarrollo de Servlet en formato de script
• Se pueder realizar cambios sin tener que reiniciar el server
• No es necesario parsar por un proceso de compilacion
• Utilies para el desarrollo de prototipos o funcionalidades acotadas
• Se realiza el binding de una serie de objetos para faciliar el uso dentro del scope de Groovy
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.047
Labs 12- GroovyServlet
• request - the HttpServletRequest
• response - the HttpServletResponse
• application - the ServletContext associated with the servlet
• session - the HttpSession associated with the HttpServletRequest
• out - the PrintWriter associated with the ServletRequest
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.048
Labs 12- GroovyServlet<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>GroovyAction</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GroovyAction</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
</web-app>
http://www.jroller.com/afuentes/entry/groovyservlet_ejemplo
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.049
Laboratorios
• Lab 01 – Instalación de Groovy ( Linux and Windows )• Lab 02 - Eclipse y Groovy Plug-ins• Lab 03 - Hola Mundo • Lab 04 - Tipos dinamicos• Lab 05 - GString• Lab 06 - POGOs• Lab 07 - Closures• Lab 08 - Operadores• Lab 09 - List y Map y iteradores • Lab 10 - GroovyMarkup y GPath• Lab 11 - Metaprogramacion• Lab 12 - GroovyServlet • Lab 13 - Gsql
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.050
Labs 13 - GSql
• API que permite todo el manejo de Acceso a Base de Datos en forma facil
• Permite la generacion de select en pocas lineas de codigos
Introduccion a Groovy | © 2008 by «Alex Fuentes»; made available under the GPL v1.051
Gracias!