Metaprogramming
Using Categories and Mixins
        Matthew Taylor
      G2One SpringSource
Metaprogramming

• Manipulating class structures at runtime
• Lisp, Python, Ruby, JavaScript, PHP, Perl, Smalltalk, Groovy
Using the MetaClass
             in Groovy
String.metaClass.getRandomChar = {
    def chars = delegate.chars
    chars[new...
Categories

• Another method of metaprogramming
• Good for functionality that:
 • spans classes
 • should be reverted afte...
Category Example:
         RandomCategory

• Add rand() method to Integer
• Add rand() method to List
• Add rand(count) me...
Mixins

• Permanent, application-wide class changes
• Amplifications or utilities added to classes
• Simpler to use than ca...
Mixin Example:
        String Utilities Mixin

•Add isAlpha() to String

• Add isAlphaNumeric() to String
Advanced Mixin Example:
      Groovy Measures
            9.meters + 34.centimeters

                          9.034

• Us...
methodMissing
Dynamic Finders in Grails
class GORM {
  def dynamicMethods = [...] // an array of dynamic methods that use ...
Contact


• matt.taylor@springsource.com
• http://weblog.dangertree.net
• http://www.twitter.com/rhyolight
Upcoming SlideShare
Loading in …5
×

Groovy Metaprogramming With Categories And Mixins.Key

4,428 views
4,230 views

Published on

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
4,428
On SlideShare
0
From Embeds
0
Number of Embeds
355
Actions
Shares
0
Downloads
53
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Groovy Metaprogramming With Categories And Mixins.Key

  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

×