presentatie - introductie in groovy
DESCRIPTION
Paco van der Linden, werkzaam als Senior ADF Ontwikkelaar bij AMIS, heeft met Groovy een aantal interessante oplossingen ontwikkeld. De kennis en ervaring die hij daarbij met Groovy in combinatie met Java (en ADF) heeft opgedaan, heeft hij op maandag 26 november gedeeld in een kennissessie.TRANSCRIPT
![Page 1: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/1.jpg)
INTRODUCTIE IN GROOVY
AMIS – 26 november 2012
![Page 2: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/2.jpg)
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
OVER JAVA
• Academisch
• Uitgewerkt concept, Super uitgespecificeerd
• Bewezen
• Performant, Platform onafhankelijk
• Robust, Secure (?)
• Antiek
• Modern in 1995
• Vergelijk: C#, Python, Ruby, JavaScript, Smalltalk, Go
• Autistisch
import java.io.*;
import java.math.BigDecimal;
import java.net.*;
import java.util.*;
![Page 3: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/3.jpg)
OVER JAVA – VERGELIJKING
Java
public class Pet {
private PetName name;
private Person owner;
public Pet(PetName name, Person owner) {
this.name = name;
this.owner = owner;
}
public PetName getName() {
return name;
}
public void setName(PetName name) {
this.name = name;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
}
C#
public class Pet
{
public PetName Name { get; set; }
public Person Owner { get; set; }
}
![Page 4: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/4.jpg)
OVER JAVA – VERGELIJKING
Java
Map<String, Integer> map =
new HashMap<String, Integer>();
map.put("een", 1);
map.put("twee", 2);
map.put("drie", 3);
map.put("vier", 4);
map.put("vijf", 5);
map.put("zes", 6);
Python
map = {'een':1, 'twee':2, 'drie':3,
'vier':4, 'vijf':5, 'zes':6}
![Page 5: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/5.jpg)
OVER JAVA – VERGELIJKING
Java
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream("file.txt");
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (br != null)
try { br.close();
} catch (IOException e) { }
if (isr != null)
try { isr.close();
} catch (IOException e) { }
if (fis != null)
try { fis.close();
} catch (IOException e) { }
}
Python
with open('file.txt', 'r') as f:
for line in f:
print line,
![Page 6: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/6.jpg)
OVER GROOVY
• Sinds: 2003
• Door: James Strachan
• Target: Java ontwikkelaars
• Basis: Java
• Plus: “Al het goede” van Ruby, Python en Smalltalk
• Apache Licence
• Uitgebreide community
• Ondersteund door SpringSource (VMWare)
• “The most under-rated language ever”
• “This language was clearly designed by very, very lazy
programmers.”
![Page 7: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/7.jpg)
OVER GROOVY
• Draait in JVM
• Groovy class = Java class
• Perfecte integratie met Java
![Page 8: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/8.jpg)
HELLO WORLD - JAVA
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
![Page 9: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/9.jpg)
HELLO WORLD - GROOVY
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
![Page 10: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/10.jpg)
HELLO WORLD - GROOVY
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
![Page 11: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/11.jpg)
HELLO WORLD - GROOVY
public class HelloWorld {
String name;
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
![Page 12: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/12.jpg)
HELLO WORLD - GROOVY
public class HelloWorld {
String name;
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
![Page 13: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/13.jpg)
HELLO WORLD - GROOVY
class HelloWorld {
String name
def greet() {
return "Hello " + name
}
static main(args) {
def helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
}
}
![Page 14: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/14.jpg)
HELLO WORLD - GROOVY
class HelloWorld {
String name
def greet() {
return "Hello " + name
}
static main(args) {
def helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
}
}
name:"Groovy")
![Page 15: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/15.jpg)
HELLO WORLD - GROOVY
class HelloWorld {
String name
def greet() { "Hello " + name }
static main(args) {
def helloWorld = new HelloWorld(name:"Groovy")
println(helloWorld.greet())
}
}
![Page 16: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/16.jpg)
HELLO WORLD - GROOVY
class HelloWorld {
String name
def greet() { "Hello " + name }
static main(args) {
def helloWorld = new HelloWorld(name:"Groovy")
println(helloWorld.greet())
}
}
![Page 17: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/17.jpg)
HELLO WORLD - GROOVY
class HelloWorld {
def name
def greet() { "Hello $name" }
}
def helloWorld = new HelloWorld(name:"Groovy")
println helloWorld.greet()
![Page 18: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/18.jpg)
HELLO WORLD - GROOVY
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
class HelloWorld {
def name
def greet() { "Hello $name" }
}
def helloWorld = new HelloWorld(name:"Groovy")
println helloWorld.greet()
![Page 19: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/19.jpg)
GROOVY CONSOLE
![Page 20: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/20.jpg)
GROOVY INTEGRATION
• Als Programmeertaal
– Compileert design-time
– Configuratie in Ant build script
![Page 21: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/21.jpg)
GROOVY INTEGRATION - ANT
<?xml version="1.0" encoding="windows-1252" ?>
<project xmlns="antlib:org.apache.tools.ant" name="Project1">
...
<path id="groovy.lib" location="${workspace.dir}/groovy-all-2.0.1.jar"/>
...
<taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc“
classpathref="groovy.lib"/>
...
<target name="compile">
<groovyc srcdir="${src.dir}" destdir="${output.dir}">
<javac source="1.6" target="1.6"/>
</groovyc>
</target>
...
</project>
![Page 22: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/22.jpg)
GROOVY INTEGRATION
• Als Programmeertaal
– Compileert design-time
– Configuratie in Ant build script
• Als Scripttaal
– Compileert run-time
• (Groovy compiler is geschreven in Java)
![Page 23: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/23.jpg)
GROOVY INTEGRATION - SCRIPTING
GroovyShell gs = new GroovyShell();
Object result = gs.evaluate("...");
![Page 24: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/24.jpg)
GROOVY FEATURES
• Native syntax
– Lists
def list = [1, 2, 3, 4]
– Maps:
def map = [nl: 'Nederland', be: 'Belgie']
assert map['nl'] == 'Nederland'
&& map.be == 'Belgie'
– Ranges:
def range = 11..36
– Regular Expressions
assert 'abc' ==~ /.\wc/
![Page 25: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/25.jpg)
GROOVY FEATURES
• Native syntax (vervolg)
– GStrings
"Hello $name, it is now: ${new Date()}"
– Multiline Strings
def string = """Dit is regel 1.
Dit is regel 2."""
– Multiple assignment
def (a, b) = [1, 2]
(a, b) = [b, a]
def (p, q, r, s, t) = 1..5
![Page 26: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/26.jpg)
GROOVY FEATURES
• New Operators
– Safe navigation (?.)
person?.adress?.streetname
in plaats van
person != null ? (person.getAddress() != null ?
person.getAddress().getStreetname() : null) :
null
– Elvis (?:)
username ?: 'Anoniem'
– Spaceship (<=>)
assert 'a' <=> 'b' == -1
![Page 27: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/27.jpg)
GROOVY FEATURES
• Operator overloading
– Uitgebreid toegepast in GDK, voorbeelden:
def list = ['a', 'b', 1, 2, 3]
assert list + list == list * 2
assert list - ['a', 'b'] == [1, 2, 3]
new Date() + 1 // Een date morgen
![Page 28: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/28.jpg)
GROOVY FEATURES
• Smart conversion (voorbeelden)
– to Numbers
def string = '123'
def nr = string as int
assert string.class.name == 'java.lang.String'
assert nr.class.name == 'java.lang.Integer'
– to Booleans (Groovy truth)
assert "" as boolean == false
if (string) { ... }
– to JSON
[a:1, b:2] as Json // {"a":1,"b":2}
– to interfaces
![Page 29: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/29.jpg)
GROOVY FEATURES
• Closures
– (Anonieme) functie
• Kan worden uitgevoerd
• Kan parameters accepteren
• Kan een waarde retourneren
– Object
• Kan in variabele worden opgeslagen
• Kan als parameter worden meegegeven
– Voorbeeld:
new File('file.txt').eachLine({ line ->
println line
})
– Kan variabelen buiten eigen scope gebruiken
it
![Page 30: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/30.jpg)
GROOVY FEATURES
• Groovy SQL
– Zeer mooie interface tegen JDBC
– Gebruikt GStrings voor queries
– Voorbeeld:
def search = 'Ki%'
def emps = sql.rows ( """select *
from employees
where first_name like $search
or last_name like $search""" )
![Page 31: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/31.jpg)
GROOVY FEATURES
• Builders
– ‘Native syntax’ voor het opbouwen van:
• XML
• GUI (Swing)
• Json
• Ant taken
• …
– Op basis van Closures
![Page 32: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/32.jpg)
GROOVY QUIZ
• Geldig?
def l = [ [a:1, b:2], [a:3, b:4] ]
def (m, n) = l
class Bean { def id, name }
new Bean().setName('Pieter')
[id:123, name:'Pieter'] as Bean
geef mij nu een kop koffie, snel
def
![Page 33: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/33.jpg)
GROOVY FEATURES
• Categories
– Tijdelijke DSL (domain specific language)
– Breidt (bestaande) classes uit met extra functies
– Voorbeeld:
use(TimeCategory) {
newDate = (1.month + 1.week).from.now
}
![Page 34: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/34.jpg)
GROOVY FEATURES
• (AST) Transformations
– Verder reduceren boiler plate code
– Enkele patterns worden meegeleverd, o.a.:
• @Singleton
• @Immutable / @Canonical
• @Lazy
• @Delegate (multiple inheritance!)
![Page 35: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/35.jpg)
GROOVY CASES
• Java omgeving
– Als je werkt met: (ook in productie)
• Beans
• XML / HTML / JSON
• Bestanden / IO algemeen
• Database via JDBC
• Hardcore Java classes (minder boilerplate)
• DSL
• Rich clients (ook JavaFX)
– Functioneel programmeren in Java
– Prototyping
– Extreme dynamic classloading
• Shell scripting
![Page 36: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/36.jpg)
GROOVY HANDSON
![Page 37: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/37.jpg)
“ADF SCRIPTENGINE”
• Combinatie van:
– ADF
– Groovy
• met wat nieuwe constructies (“omdat het kan”)
• Features
– Programmaflow in Groovy
– Builder syntax voor tonen schermen in ADF
• Control state is volledig serialiseerbaar (high availability enzo)
– Bindings (à la EL ValueBindings)
– Mooie API (à la Grails) richting Model (bijv. ADF BC,
WebServices, etc...)
![Page 38: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/38.jpg)
“ADF SCRIPTENGINE”
• Voorbeeld script
– Handmatig aanvullen lege elementen in een XML fragment
![Page 39: Presentatie - Introductie in Groovy](https://reader033.vdocuments.pub/reader033/viewer/2022052618/554dbd69b4c905bd488b4a54/html5/thumbnails/39.jpg)
GROOVY TEASER
• Method parameters with defaults
• Method with named parameters
• Creating Groovy API’s
• Runtime meta-programming
• Compile-time meta-programming
• Interceptors
• Advanced OO (Groovy interfaces)
• GUI’s
• …