Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile...
Transcript of Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile...
3Meta-programming in Groovy
“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
4Meta-programming in Groovy
My class is your class
Groovy
Java
Java Runtime Environment
Adopted from Gina, p. 5
5Meta-programming in Groovy
Precompiled vs. direct mode
Adopted from Gina, p. 48
Code.groovy
Code.groovy
groovyc
Java class loader Groovy class loader
Code.class
Loaded class Loaded class
Running Example
class Dog {
String name = 'dog'
void bark() {
System.out.println "$name: woof"
}
String toString() {
name
}
}
8Meta-programming in Groovy
Creating Objects
10Meta-programming in Groovy
static void main(args) {new Dog()
}
ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])
Getting Metaclass for Classes
11Meta-programming in Groovy
static void main(args) {new Dog()
}
ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])
Example for Custom Metaclass
12Meta-programming in Groovy
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')
Method Invocation
13Meta-programming in Groovy
static void main(args) {dog = new Dog()dog.bark()
}
ScriptBytecodeAdapter. invokeMethodN( DogExample.class, dog, "bark", new Object[0])
Intercepting Method Calls
14Meta-programming in Groovy
static void main(args) {dog = new Dog()dog.bark()
}
ScriptBytecodeAdapter. invokeMethodN( DogExample.class, dog, "bark", new Object[0])
Interception in GroovyInterceptable
15Meta-programming in Groovy
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
Interception using Interceptor
16Meta-programming in Groovy
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()
dog.bark()
}
Neighbor intercepted barking
Interception with MetaClass
17Meta-programming in Groovy
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()
Waldi is about to bark
Waldi: woof
Waldi is about to speak
Waldi does not speak
Evaluating Expressions
18Meta-programming in Groovy
static void main(args) {shell = new GroovyShell()shell.evaluate("1+1")
}
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
23Meta-programming in Groovy
References
• [Gina]: Dierk Koenig: Groovy in Action
• Codehaus Documentation
http://groovy.codehaus.org/Documentation
• Practically Groovy
http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=practically+groovy
• Groovy Source Code and Mailing List
25Meta-programming in Groovy