Jump-starting Groovy and Grails for Java experts Václav Pech Senior Software Developer JetBrains, Inc. www.jetbrains.com About Us · Václav Pech ­ Professional software developer for 9 years ­ Groovy enthusiast for more than a year · JetBrains ­ Makers of award winning productivity tools · IntelliJ IDEA, TeamCity, ReSharper, and more www.jetbrains.com 2 About the presentation · Part 1: Groovy ­ Syntax enhancements ­ Target domains (Scripting, Builders, DSLs) · Part 2: Grails ­ Web applications in an easy way · Questions and answers · CAUTION!: Code will be shown and edited during the demo www.jetbrains.com 3 One language can't do it all ­ ­ ­ ­ ­ ­ ­ ­ ­ SQL XHTML JavaScript XUL (XML UI Language) SVG (Scalable Vector Graphics) CSS (Cascading Stylesheets) XPath Velocity, XSL-T ... ­ Groovy ­ Scripting, DSLs, builders, ... www.jetbrains.com 4 Groovy · Dynamic, object-oriented scripting language ­ Java-like syntax · Including annotations and enums · Modest learning curve for Java developers ­ Compiled into Java bytecode ­ Easy Java integration ­ Dynamic typing (with optional types) www.jetbrains.com 5 Groovy · Open sourced · Since 2004 under JSR 241: The Groovy Programming Language · Currently maintained by G2one · IDE support · Plugins for all major IDEs · JetGroovy plugin for IntelliJ IDEA by JetBrains · Runtime support ­ Spring, Seam, ... www.jetbrains.com 6 Properties www.jetbrains.com 7 Closures www.jetbrains.com 8 Closures ­ implicit parameter www.jetbrains.com 9 Iterations www.jetbrains.com 10 GDK · java.util.Collection ­ each(), find(), join(), min(), max() ... · java.lang.Object ­ any(), every(), print(), invokeMethod(), ... · java.lang.Number ­ plus(), minus(), power(), upto(), times(), ... · ... www.jetbrains.com 11 Operator overloading www.jetbrains.com 12 Strings and GStrings www.jetbrains.com 13 Multi-line Strings and GStrings www.jetbrains.com 14 Operators · Spread · def names = customers*.name · [`Java', *[`Groovy', `Ruby', `Scala', `Jython']] · Safe dereference · customer.shippingAddress?.street · Membership · if (code.language in [`Java', `Groovy']) · Elvis · return user.locale?:defaultLocale www.jetbrains.com 15 Operators · Spaceship (compareTo) · a <=> b · Find and Match - =~, ==~ · methodName =~ `findAll(.*)Employees' · Subscript · map[`key'] == map.key == map.get(`key') · `VeryLongString'[4..-1] · As · new Thread({println `In parallel'} as Runnable) www.jetbrains.com 16 Syntax enhancements · Adds methods to JDK classes (GDK) · Dynamic (duck) typing ­ optional! · Syntax enhancements ­ ­ ­ ­ ­ ­ ­ ­ ­ Properties Closures Named parameters (new Customer(name:'Joe')) Collections and maps Operator overloading GString ("Name: $name") GPath (employees.findAll{it.age>30}*.name) Range data type (e.g. 9..29 or 0..-1) Integration of regular expressions www.jetbrains.com 17 Testing · Test both Java and Groovy · Using Ant or Maven · Integrated JUnit support · assert...(), shouldFail(), ... · Duck typing · Easy mocking ­ Integrated support for stubs and mocks www.jetbrains.com 18 Advanced concepts · Builders ­ For hierarchical data structures · Scripting ­ Adding code to your app at runtime · Easy and powerful DSL support ­ Leverages the meta-programming capabilities www.jetbrains.com 19 Builders · Construct hierarchies www.jetbrains.com 20 Builders - GAnt www.jetbrains.com 21 Builders ­ Spring config www.jetbrains.com 22 Builders ­ domain classes www.jetbrains.com 23 Scripting · Evaluate custom Groovy code at run-time ­ Custom code or tests ­ Implementation of an interface www.jetbrains.com 24 DSL · Limited purpose languages · Targeted to a particular domain · Provide a friendlier API to a framework ­ External · Dedicated grammar and parsers · Own syntax ­ Internal · Embedded in a host language · Syntax must follow the host language grammar www.jetbrains.com 25 DSL ­ Date manipulation www.jetbrains.com 26 DSL ­ Hibernate criteria www.jetbrains.com 27 DSL ­ Account manipulation www.jetbrains.com 28 Meta-programming · Builders · Categories · Alter method invocation · · · · · · Overriding invokeMethod() Overriding methodMissing() Overriding propertyMissing() Adding a method or property to the meta-class Overriding invokeMethod() on the meta-class Adding an Expando meta-class to an instance www.jetbrains.com 29 Dynamic method invocation GroovyObject invokeMethod() getProperty() setProperty() methodMissing() propertyMissing() invokeMethod() MetaClass methods properties www.jetbrains.com 30 Grails · A web application framework · Clear MVC architecture · Open sourced · Aims to make development as efficient and agile as possible www.jetbrains.com 31 Technologies www.jetbrains.com 32 Principles · · · · · CoC - Convention over configuration Configuration by exception DRY ­ Don't Repeat Yourself GAnt build scripts Scaffolding ­ Static ­ Dynamic www.jetbrains.com 33 Plugins · Testing · AJAX · Canoo, Cobertura, Selenium, jsUnit · DWR, GWT, Echo 2, Flex, YUI, ... · XFire, XML-RPC, Ajax2, Java Remoting · Web services ­ ­ ­ ­ ­ ­ Scheduling Security, Encryption JMS Text search Wicket ... www.jetbrains.com 34 Community · G2One Inc. ­ G2One.com ­ Guillaume Laforge (Groovy Project Lead) ­ Graeme Rocher (Grails Project Lead) · http://groovy.codehaus.org/ · http://grails.org/ · Groovy Related News ­ http://aboutgroovy.com ­ http://groovyblogs.org ­ http://groovy.dzone.com www.jetbrains.com 35 Books · Groovy in Action · The Definitive Guide to Grails www.jetbrains.com 36 Summary · Groovy ­ Powerful Java extension ­ Builders, Scripting, DSLs · Grails ­ Web development the easy way ­ Contact me: vaclav@jetbrains.com www.jetbrains.com 37 Questions www.jetbrains.com 38