Your SlideShare is downloading. ×
0
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Groovy Metaprogramming With Categories And Mixins.Key
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Groovy Metaprogramming With Categories And Mixins.Key

3,903

Published on

Presentation given at the Lamba Lounge (lambdalounge.org) meeting on Dec 4 in St. Louis about Groovy Metaprogramming.

Presentation given at the Lamba Lounge (lambdalounge.org) meeting on Dec 4 in St. Louis about Groovy Metaprogramming.

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

No Downloads
Views
Total Views
3,903
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
53
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Metaprogramming Using Categories and Mixins Matthew Taylor G2One SpringSource
  • 2. Metaprogramming • Manipulating class structures at runtime • Lisp, Python, Ruby, JavaScript, PHP, Perl, Smalltalk, Groovy
  • 3. Using the MetaClass in Groovy String.metaClass.getRandomChar = { def chars = delegate.chars chars[new Random().nextInt(chars.size())] } quot;abcdquot;.randomChar
  • 4. Categories • Another method of metaprogramming • Good for functionality that: • spans classes • should be reverted after execution
  • 5. Category Example: RandomCategory • Add rand() method to Integer • Add rand() method to List • Add rand(count) method to List
  • 6. Mixins • Permanent, application-wide class changes • Amplifications or utilities added to classes • Simpler to use than categories (no ‘use’ block is necessary) • Can be mixed into classes or objects
  • 7. Mixin Example: String Utilities Mixin •Add isAlpha() to String • Add isAlphaNumeric() to String
  • 8. Advanced Mixin Example: Groovy Measures 9.meters + 34.centimeters 9.034 • Uses JScience for unit conversion • Test -- Source • by Jeff Brown
  • 9. methodMissing Dynamic Finders in Grails class GORM { def dynamicMethods = [...] // an array of dynamic methods that use regex def methodMissing(String name, args) { def method = dynamicMethods.find { it.match(name) } if(method) { GORM.metaClass.quot;$namequot; = { Object[] varArgs -> method.invoke(delegate, name, varArgs) } return method.invoke(delegate,name, args) } else throw new MissingMethodException(name, delegate, args) } }
  • 10. Contact • matt.taylor@springsource.com • http://weblog.dangertree.net • http://www.twitter.com/rhyolight

×