3. “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
4. My class is your class
Groovy
Java
Java Runtime Environment
Adopted from Gina, p. 5
Meta-programming in Groovy 4
5. Precompiled vs. direct mode
Code.groovy
groovyc
Code.class Code.groovy
Java class loader Groovy class loader
Loaded class Loaded class
Adopted from Gina, p. 48
Meta-programming in Groovy 5
8. Running Example
class Dog {
String name = 'dog'
void bark() {
System.out.println "$name: woof"
}
String toString() {
name
}
}
Meta-programming in Groovy 8
10. Creating Objects
static void main(args) { ScriptBytecodeAdapter.invokeNewN(
new Dog() DogExample.class, Dog.class,
} new Object[0])
Meta-programming in Groovy 10
11. Getting Metaclass for Classes
static void main(args) { ScriptBytecodeAdapter.invokeNewN(
new Dog() DogExample.class, Dog.class,
} new Object[0])
Meta-programming in Groovy 11
12. 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
13. Method Invocation
static void main(args) {
dog = new Dog() ScriptBytecodeAdapter. invokeMethodN(
dog.bark() DogExample.class, dog, "bark",
} new Object[0])
Meta-programming in Groovy 13
14. Intercepting Method Calls
static void main(args) {
dog = new Dog() ScriptBytecodeAdapter. invokeMethodN(
dog.bark() DogExample.class, dog, "bark",
} new Object[0])
Meta-programming in Groovy 14
15. 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
16. 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
17. 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') Waldi is about to bark
dog.metaClass = new BrunoMeta() Waldi: woof
dog.bark() Waldi is about to speak
dog.speak() Waldi does not speak
Meta-programming in Groovy 17
18. Evaluating Expressions
static void main(args) {
shell = new GroovyShell()
shell.evaluate("1+1")
}
Meta-programming in Groovy 18