The Artima Developer Community
Sponsored Link

Java Community News
New Meta-Programming APIs in Groovy 1.1

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

New Meta-Programming APIs in Groovy 1.1 Posted: Sep 27, 2007 2:09 PM
Reply to this message Reply
Summary
Groovy's meta-object capabilities received several updates in version 1.1, including a special ExpandoMetaClass that allows dynamic addition of methods, constructors and properties to a Groovy class. Graeme Rocher describes some of those new meta-programming features in a recent blog post.
Advertisement

One of the key attractions of dynamic languages is that they easily lend themselves to meta-programming techniques—the ability of a program to dynamically modify its own behavior or the behavior of some other code. Such features are exploited by frameworks built on dynamic languages, such as Ruby on Rails and Grails with dynamic finder methods: Given a database entity, the Rails framework, as well as Grails, creates a corresponding class with finder methods that match to the entity's properties, such as find_by_X where X is a property name.

Such dynamic programming techniques have always been possible in Groovy, but developers lacked an elegant interface to Groovy's meta-object protocol, points out Graeme Rocher in a recent blog post, The new meta-programming APIs in Groovy 1.1 beta 3. Rocher notes that with Groovy 1.1, the language received several new features that make meta-programming, such as adding methods dynamic to a class, much easier:

Groovy has always had the underlying Meta Object Protocol (MOP) that allowed the same behaviour as languages such as Ruby and Small Talk, however the API onto these has, up until now, not been as elegant as it could have been. With Groovy 1.1 beta 3 there is a lot more at your finger tips...

  • Runtime evaluation with respondsTo & hasProperty... It is now a lot easier to inspect the runtime thanks to the addition of hasProperty and respondsTo for meta classes so you can do:
    class Foo {
        String name = "Wilma"
        def sayHello() { println "hello $name" }
        def sayHello(String message) { println "hello $message" }
    }
    def f = new Foo()
    
    if(f.metaClass.respondsTo(f,'sayHello') {
        f.sayHello()
    }
    
  • Missing method/property interception with methodMissing & propertyMissing... As of Groovy 1.1 beta 3, Groovy now supports "method missing" and "property missing", which are only called when method dispatch fails (ie just before a MissingMethodException would be thrown anyway.)...
    Foo.metaClass.methodMissing = { String name, args ->
        Foo.metaClass."$name" = { Object[] varArgs ->
            "$name : ${varArgs.inspect()}" 
        }
        delegate."$name"(args)
    }
    
    def f = new Foo()
    f.sayHello('Fred')
    assert f.notARealMethod("boo") 
                == 'notARealMethod : [["boo"]]'
    assert f.notARealMethod("boo", "hoo") 
                == 'notARealMethod : [["boo", "hoo"]]'
    

Many of these features in Groovy 1.1 are facilitated via a special MetaClass, ExpandoMetaClass, that allows you to dynamically add methods, constructors, properties and static methods using a neat closure syntax.

What do you think Groovy 1.1's new meta-programming features?

Topic: Quaere Project Moves to Codehaus, Releases Updates Previous Topic   Next Topic Topic: Gavin King on Being Explicit

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use