Metaprogramming With The Groovy Runtime - Part 1 of 2

486 views

Published on

Speaker: Jeff Scott Brown
The dynamic runtime nature of Groovy is one of the things that sets it apart from standard Java and makes it a fantastic language for building dynamic applications for the Java Platform. The metaprogramming capabilities offered by the language provide everything that an application development team needs to build systems that are far more capable than their all Java counterparts. This Part 1 of 2 will cover the runtime metaprogramming capabilities of Groovy. The session will dive deep into Groovy's Meta Object Protocol (MOP) which implements the incredibly dynamic runtime dispatch mechanism. The session will include a lot of live code demonstrating really powerful runtime features of the language.
This session is focused specifically on Groovy's runtime metaprogramming capabilities. Part 2 of 2 will cover Groovy's compile time metaprogramming capabilities.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
486
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Metaprogramming With The Groovy Runtime - Part 1 of 2

  1. 1. Metaprogramming With The Groovy Runtime – Part 1 of 2 Jeff Scott Brown @jeffscottbrown jbrown@gopivotal.com © 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  2. 2. Metaprogramming ● What Is It? – behavior changes at runtime – capabilities are introduced at runtime – Wikipedia says... “Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves)...”
  3. 3. Metaprogramming And Groovy ● Groovy Lends Itself Well To Metaprogramming – dynamic method dispatch – method/property interception – create methods at runtime – JVM supports very dynamic behavior ● much cannot be easily tapped by Java the language
  4. 4. Expando ● Dynamically Expanding Object def myExpando = new Expando() myExpando.favoriteLanguage = 'Groovy' myExpando.addNumbers = { i, j -> i + j } assert 'Groovy' == myExpando.favoriteLanguage assert 100 == myExpando.addNumbers(60, 40) assert myExpando.foo == null
  5. 5. Runtime Mappings expando.favoriteLanguage = 'Groovy' // maps to... expando.setProperty('favoriteLanguage', 'Groovy') expando.favoriteLanguage // maps to... expando.getProperty('favoriteLanguage') expando.addNumbers(33, 66) // maps to... expando.invokeMethod('addNumbers', [33, 66] as Object[])
  6. 6. Our Own Expando With Those 3 Simple Methods We Can Create Our Own Version Of Expando In 3 Minutes. Let's Do That Now. Live Demo...
  7. 7. Closure Delegates ● ● ● ● Closures May Be Assigned A “Delegate” Closures Relay Method Calls To Their Delegate Useful For Creating DSLs And Builders Same Closure May Be Executed In Different Contexts With Different Delegates
  8. 8. Closure Delegates def myClosure = { append('First Linen') append('Last Linen') } // will fail because there is no 'append' method myClosure() def buffer = new StringBuffer() def myClosure = { append('First Linen') append('Last Linen') } myClosure.delegate = buffer // will append to the StringBuffer myClosure()
  9. 9. Closure Delegates class User { String login String password String email Date age static constraints = { login(length:5..15,blank:false,unique:true) password(length:5..15,blank:false) email(email:true,blank:false) age(min:new Date(),nullable:false) } }
  10. 10. MarkupBuilder def mkp = new groovy.xml.MarkupBuilder() mkp.html { head { title('My MarkupBuilder Test') } body { h2('My Favorite Sites') ul { li('groovy.codehaus.org') li('grails.org') li('javajeff.blogspot.com') } Remember that this is executable code. All of those } tag names are method calls that are being intercepted } by the builder.
  11. 11. MarkupBuilder <html> <head> <title>My MarkupBuilder Test</title> </head> <body> <h2>My Favorite Sites</h2> <ul> <li>groovy.codehaus.org</li> <li>grails.org</li> <li>javajeff.blogspot.com</li> </ul> </body> </html>
  12. 12. ExpandoMetaClass String.metaClass.vowels = { delegate.findAll { it.toLowerCase().matches('[aeiou]') } } def message = 'Groovy Is A Great Language' println message.vowels() // ["o", "o", "I", "A", "e", "a", "a", "u", "a", "e"]
  13. 13. Thank You For Coming Q&A

×