Jazzing up Plain Old Java
Total Page:16
File Type:pdf, Size:1020Kb
Groooooovy Babe: Jazzing Up Plain Old Java http://viva.sourceforge.net/talk/jug-mar-2004/slides.html Groooooovy Babe: Jazzing Up Plain Old Java Scripting Power for Java - Do More With Less (Lines of Code) Gerald Bauer (Chairman, CEO, CFO and CTO of Me, Myself & I, Inc.) Java User Group (JUG) Austria Talk, March 2004 Table of Contents Groooooovy Babe - Jazzing Up Plain Old Java Who is this guy? Agenda - The Road Ahead What is Groovy? Why Groovy? What's wrong with Python (Jython), Ruby (JRuby) or Smalltalk (Bistro)? One Language Can't Do It All: Beyond Hairballs and Spaghetti Code Scripting vs. Systems (Hard-Core) Programming / Groovy vs. Java First Impression - Servus Groovy Example Second Impression - Higher-Level Functions, Loops and Data Types Third Impression - Groovy Beans vs Java Beans The Groovy Founding Fathers More Groovy Heros Groovy Goodies Missing In Java Groovy Lists: Built-In Syntax for Lists Groovy Maps: Built-In Syntax for Maps More Groovy List and Map Examples Groovy Loops: Higher-Level Loops Using Closures What is a Closure (Code Block)? Closures In Action: Groovy Collections vs. Plain Old Java Collections Higher-Level Loops and Functions For Maps And Lists Groovy Adds New Methods To Core Java Classes Groovy Template Strings: Expressions In Strings Groovy Strings: Here-Doc Strings And More Groovy Path Expression Language Built-in Syntax For Regular Expressions (Regex) Groovy Markup (XML) Syntax Scripting Ant Using Groovy Markup Building Swing UIs Using Groovy Markup Building Eclipse UIs Using Groovy Markup Groovy SQL Groovy Java Interop: Static Typing Optional Embedding Groovy in Your App Compiling Groovy Scripts Using groovyc Compiling Groovy Scripts Using Ant or Maven More Groovy Goodies in the Pipeline Alternative Scripting Languages for the Java Runtime Viva! Scripting Language of the Year 2003 Award Scripting Languages Comparison Chart Groovy Links, Links, Links Ruby Links, Links, Links Bonus Slide: Python (Jython) Links, Links, Links Scripting: Higher Level Programming for the 21st Century That's it - The Future Just Happened Groooooovy Babe - Jazzing Up Plain Old Java Who is this guy? Gerald Bauer independent Java, XML and Web consultant and open-source advocate open-sourced Luxor (Java XML UI Language (XUL) Toolkit) , Rachel (Resource Loading Toolkit for Web Start/JNLP), Apollo (Test Skeletion for Web Start/JNLP) and more (now all Apache 2.0-licensed) maintains Lopica Web Start Encyclopedia including the Unofficial Java Web Start/JNLP FAQ publishes The Java Republic (Free Open Source Core Java News) and The Richmond Post (XML UI News) 1 of 19 2/28/08 9:43 PM Groooooovy Babe: Jazzing Up Plain Old Java http://viva.sourceforge.net/talk/jug-mar-2004/slides.html started the XUL News Wire and Web Start News Wire founded the Open XUL Alliance to offer a forum to work on XML UI Language (XUL) interop and help create a rich internet for everyone Agenda - The Road Ahead Why Groovy? One Language Can't Do It All Groovy Panorama - Servus Groovy The Heroes Behind Groovy Groovy Goodies Missing in Java Embedding Groovy, Compiling Groovy Alternative Scripting Languages for Java Groovy and Ruby Books and Links What is Groovy? Groovy is a dynamic object-oriented scripting language that combines the best from Smalltalk, Python and Ruby in an all-in-one package using a Java-like syntax. Groovy is 100 % Java and compiles scripts straight to Java bytecode that run on any Java Virtual Machine. Groovy offers seamless and smoth Java intergration: from Groovy you can access all Java libraries, you can build applets or Java beans, you can derive from Java classes in Groovy and vice versa. Why Groovy? What's wrong with Python (Jython), Ruby (JRuby) or Smalltalk (Bistro)? Why yet another scripting language? Groovy builds on (reuses) the Java standard class library; Python, Ruby or Smalltalk include their own batteries (that is, standard libraries) Groovy uses a Java-like syntax; easy to switch from Java to Groovy or from Groovy to Java Groovy compiles straight to standard Java bytecode; you can use Groovy (groovyc) as an alternative compiler to javac One Language Can't Do It All: Beyond Hairballs and Spaghetti Code Scripting on the Rise. The Death of General Purpose Languages and Monolithic Applications. Prefer the single-purpose languages below to general-purpose languages such as Java, C# or Shark. XHTML / rich (styled) text XUL (XML UI Language) / rich UIs such as menus, toolbars, forms, datagrids, trees, splitters, and so on SVG (Scalable Vector Graphics) / rich 2D graphics such as charts, maps, logos, and so on CSS (Cascading Stylesheets) / Visual Styling for XHTML, XUL and SVG Groovy / Scripting XPath / XML Tree Node Addressing SQL / Data Queries, Updates, Inserts, Deletes, etc. String Tables / Internationalization Velocity, XSL-T / Template Scripting vs. Systems (Hard-Core) Programming / Groovy vs. Java Groovy does not replace Java. Groovy complements Java and doesn't compete head-on. Groovy is a scripting language. Java is a hard-core programming language (systems language). No compilation (Fast Build-Cycle Turnaround) Dynamic Typing (No Need to Declare Variables For Use) Easy Syntax (Higher Level Datatypes, Functions and Loops, Semicolons and Return Optional, and more) Embedabble (Scripting Power for Your Apps) Interactive (Create,View, Change Objects At Run-Time) 2 of 19 2/28/08 9:43 PM Groooooovy Babe: Jazzing Up Plain Old Java http://viva.sourceforge.net/talk/jug-mar-2004/slides.html 50 % less code 2 to 3 times higher productivity (that is, less development time) First Impression - Servus Groovy Example Java public class ServusGroovy { public static void main( String args[] ) { System.out.println( "Servus Groovy" ); } } Groovy print 'Servus Groovy' Second Impression - Higher-Level Functions, Loops and Data Types Java import java.util.*; public class HelloWorld { public static void main( String args[] ) { List country = new ArrayList(); country.add( "Canada" ); country.add( "Austria" ); country.add( "Brazil" ); Collections.sort( country ); for( Iterator it = country.iterator(); it.hasNext() ) System.out.println( "Hello " + it.next() ); } } Groovy country = [ 'Canada', 'Austria', 'Brazil' ] country.sort country.each { println "Hello ${it}" } Ruby country = [ 'Canada', 'Austria', 'Brazil' ] country.sort country.each { |country| puts "Hello #{country}" } Third Impression - Groovy Beans vs Java Beans Java public class Country { private String name; private String capital; 3 of 19 2/28/08 9:43 PM Groooooovy Babe: Jazzing Up Plain Old Java http://viva.sourceforge.net/talk/jug-mar-2004/slides.html public String getName() { return name; } public String getCapital() { return capital; } public String setName( String name ) { this.name = name; } public String setCapital( String capital ) { this.capital = capital; } public static void main( String args[] ) { Country austria = new Country(); austria.setName( "Austria" ); austria.setCapital( "Vienna" ); Country canada = new Country(); canada.setName( "Canada" ); canada.setCapital( "Ottawa" ); List world = new ArrayList(); world.add( austria ); world.add( canada ); for( Iterator it = world.iterator(); it.hasNext() ) { Country country = it.next(); System.out.println( "The capital of " + country.getName() + " is " + country.getCapital() + "." ); } } } Groovy class Country { String name String capital } world = [new Country(name:'Austria', capital:'Vienna'), new Country(name:'Canada', capital:'Ottawa')] world.each { country | println "The capital of ${country.name} is ${country.capital}." } Ruby class Country def initialize( name, capital ) @name = name @capital = capital end attr_reader( :name, :capital ) end world = [ Country.new( 'Austria', 'Vienna' ), Country.new( 'Canada', 'Ottawa' )] world.each { | country | puts "The capital of #{country.name} is #{country.capital}." } The Groovy Founding Fathers Who is James Strachan? Java hacker extraordinaire from London, UK (England) Works as Partner for the Core Developers Network Guiness book of world records candidate for starting the most open source projects Founder or Co-Founder: Groovy, Geronimo, Jelly, dom4j, Jaxen, saxpath, Commons Messenger, Commons Betwixt, Commons CLI Committer: Maven, Taglibs, Commons Collections, Commons Beanutils, Commons Logging, Commons Digester Who is Bob McWhirter? Java hacker extraordinaire from Atlanta, Georgia (USA) 4 of 19 2/28/08 9:43 PM Groooooovy Babe: Jazzing Up Plain Old Java http://viva.sourceforge.net/talk/jug-mar-2004/slides.html Self-Employed, The Werken Company Codehaus.org founder Founder or Co-Founder: Groovy, dom4j, Jaxen, saxpath, Werkz, Drools, ClassWorlds More Groovy Heros Committers Joe Walnes, Chris Stevenson, Jamie McCrindle, Matt Foemmel, Sam Pullara, Kasper Nielsen, Travis Kay, Guillaume Laforge, Zohar Melamed, John Wilson Contributors Jeremy Rayner, Joern Eyrich, Robert Kuzelj, Rod Cope, Yuri Schimke, James Birchfield, Robert Fuller, Sergey Udovenko Source: http://groovy.codehaus.org/team-list.html Groovy Goodies Missing In Java built-in syntax for lists, maps, regex and ranges (e.g (1..1000)) (=higher level data types) built-in syntax for markup (XML) higher-level functions and loops (=closures) built-in Velocity-style/XPath-style expression for Java bean acess (e.g. ${movie.director.name}) many new helper methods added to core Java classes (e.g. any, each, findAll, print and more) new keywords (e.g. property, using) operator overloading (e.g. [1,2,3]+[3,4,5], map['one']) everything is an object (no need for boxing and unboxing) and more Groovy Lists: Built-In Syntax for Lists