3. Meta-programming in Groovy. “Groovy is an agile dynamic language for the
Java. Platform with many features that are inspired by languages like Python ...
Meta-programming in Groovy Lars Blumberg Christoph Hartmann Arvid Heise
29.02.2008
James Strachan wrote
The Groovy Story
Meta-programming in Groovy
2
“Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like Python, Ruby and Smalltalk, making them available to Java developers using a Java-like syntax.” The Groovy web site
Meta-programming in Groovy
3
My class is your class
Groovy Java Java Runtime Environment
Adopted from Gina, p. 5
Meta-programming in Groovy
4
Precompiled vs. direct mode Code.groovy groovyc
Code.class
Code.groovy
Java class loader Loaded class
Groovy class loader Loaded class
Adopted from Gina, p. 48
Meta-programming in Groovy
5
Groovy programming concepts Beauty through brevity
Meta-programming in Groovy
6
Expose the Magic
Meta-programming in Groovy
7
Running Example
class Dog { String name = 'dog' void bark() { System.out.println "$name: woof" } String toString() { name } }
Meta-programming in Groovy
8
Metaclasses in Groovy
Meta-programming in Groovy
9
Creating Objects static void main(args) { new Dog() }
ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])
Meta-programming in Groovy
10
Getting Metaclass for Classes static void main(args) { new Dog() }
ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])
Meta-programming in Groovy
11
Example for Custom Metaclass class WaldiMeta extends MetaClassImpl { WaldiMeta() { super(GroovySystem.getMetaClassRegistry(), Dog.class) initialize() } } // Instance-based MetaClass waldi = new Dog(name: 'Waldi') waldi.metaClass = new WaldiMeta() // Class-based MetaClass GroovySystem.getMetaClassRegistry().setMetaClass(Dog.class, new WaldiMeta()) waldi = new Dog(name: 'Waldi')
Meta-programming in Groovy
12
Method Invocation static void main(args) { dog = new Dog() dog.bark() }
ScriptBytecodeAdapter. invokeMethodN( DogExample.class, dog, "bark", new Object[0])
Meta-programming in Groovy
13
Intercepting Method Calls static void main(args) { dog = new Dog() dog.bark() }
ScriptBytecodeAdapter. invokeMethodN( DogExample.class, dog, "bark", new Object[0])
Meta-programming in Groovy
14
Interception in GroovyInterceptable
class InterceptingDog extends Dog implements GroovyInterceptable { Object invokeMethod(name, args) { System.out.println "$this is about to $name" metaClass.invokeMethod(this, name, args) } } dog = new InterceptingDog(name: 'Waldi') dog.bark()
Waldi is about to bark Waldi: woof
Meta-programming in Groovy
15
Interception using Interceptor class InterceptingNeighbor implements Interceptor { String action
Object beforeInvoke(object, methodName, arguments) { action = methodName } boolean doInvoke() { if(action != 'bark') return true println "Neighbor intercepted barking" false } } proxy = ProxyMetaClass.getInstance(Dog.class) proxy.interceptor = new InterceptingNeighbor() proxy.use { dog = new Dog() Neighbor intercepted barking dog.bark() }
Meta-programming in Groovy
16
Interception with MetaClass class BrunoMeta extends MetaClassImpl { Object invokeMethod(sender, object, methodName, originalArguments, isCallToSuper, fromInsideClass) { println "$object is about to $methodName" super.invokeMethod(sender, object, methodName, originalArguments, isCallToSuper, fromInsideClass) } Object invokeMissingMethod(instance, methodName, arguments) { println "$instance does not $methodName" } } dog = new Dog(name: 'Waldi') dog.metaClass = new BrunoMeta() dog.bark() dog.speak()
Meta-programming in Groovy
Waldi is about to bark Waldi: woof Waldi is about to speak Waldi does not speak
17
Evaluating Expressions static void main(args) { shell = new GroovyShell() shell.evaluate("1+1") }
Meta-programming in Groovy
18
Become Magician
Meta-programming in Groovy
19
Keep It Simple
XML
Class
Hibernate
Table
Application
Meta-programming in Groovy
20
Keep It Simple
Class
EJB
Table
Application
Meta-programming in Groovy
21
Keep It Simple
Groovy
Table
Application
Meta-programming in Groovy
22
Meta-programming in Groovy • Introspection: fully integrated • GroovyObject: getMetaClass, getProperty • MetaClass: getProperties, getMethods, getMetaMethods
• Intercession: • Interception: • GroovyInterceptable: pretend to have function, error handling • Interceptor: scope-level; useful for AOP, e.g. logging • MetaClass: change or observe behavior on class-level • Expando: dynamic behavior and properties on instance-level • ExpandoMetaClass: most powerful, dynamic on class-level
Meta-programming in Groovy
23
We love you …
Meta-programming in Groovy
24
References
• [Gina]: Dierk Koenig: Groovy in Action • Codehaus Documentation http://groovy.codehaus.org/Documentation • Practically Groovy http://www.ibm.com/developerworks/views/java/li braryview.jsp?search_by=practically+groovy • Groovy Source Code and Mailing List
Meta-programming in Groovy
25