groovy - rose-hulman.edu filegetting started with groovy generally a .java file can be renamed to...

11

Click here to load reader

Upload: phungkhanh

Post on 12-Feb-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Groovy

David Pick, Mark Jenne, Patrick Nowicki

Page 2: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

A little about Groovy

 Dynamically compiled to  Java Virtual Machine byte    code  Object-Oriented Closures Operator Overloading Lists!

http://i136.photobucket.com/albums/q172/IMANDEA/GroovyBaby.jpg

Q1

Page 3: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Getting Started With Groovy

Generally a .java file can be renamed to .groovyGroovy has lists and maps, no more importing ArrayList :)Closures, Java should have them, but it doesn't, so now Groovy does

 Closure coding example

http://i128.photobucket.com/albums/p177/nandysewton/groovy.gif

Q2

Page 4: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Differences from Java == means equals on all types

  alternate for-loop syntax        for (i in 0..len-1)        for (i in 0..<len)    semicolons are optional

  Nested classes not currently supported

  throws clause not checked by compiler

  dynamic and static typing is supported

Q3

Page 5: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Some Groovy Features

multiple assignment and one-line variable swapping  

named parameter passing 

passing closures into methods            def closure = { param -> param + 1 } def answer = [1, 2].collect(closure) assert answer == [2, 3]

safe navigation def streetName = user?.address?.street

Page 6: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

GUI Programming

GUI design can directly use Java Swing elements or the Groovy SwingBuilder

 SwingBuilder simplifies the syntax of Swing elements

Builders - handle the busywork of Swing for youCode is much more readable and maintainable

 "actionPerformed" attributes can be set on elements

these act as closures 

Reference elements by ID, like JavaScript

Q4

Page 7: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

SwingBuilder Exampleimport groovy.swing.SwingBuilderimport java.awt.BorderLayout as BLdef swing = new SwingBuilder()count = 0

def frame = swing.frame(title:'Frame', size:[300,300]){ borderLayout() label(id:'clickCount', text:"Click the button!", constraints: BL.NORTH) button(text:'Click Me', actionPerformed:{count++; clickCount.text = "Clicked ${count} time(s).";}, constraints:BL.SOUTH)}

frame.show()

Elements can be given an id instead of complete definitions actionPerformed attribute acts like a closure

Page 8: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Project Demonstration

Page 9: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Projects in Groovy

Good IdeasQuick developmentGUI based Java applications

 Bad Ideas 

Speed dependent applicationsPure functioning code

  

Groovy Project Home - http://groovy.codehaus.org/

http://groovy.codehaus.org

Q5, Q6

Page 10: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Key Challenges

Lack of documentation 

Non-comprehensive sample code

 Googling for "Groovy"

 Trying to write Groovy and not Java

http://i209.photobucket.com/albums/bb281/EVO_8_MR/mini_me.gif

Page 11: Groovy - rose-hulman.edu fileGetting Started With Groovy Generally a .java file can be renamed to .groovy Groovy has lists and maps, no more importing ArrayList :) Closures, Java should

Questions, Comments...Concerns?

http://www.deadline.com/wp-content/uploads/2008/07/dr_evil.jpg