Top Banner
29.02.2008 Meta-programming in Groovy Lars Blumberg Christoph Hartmann Arvid Heise
25

Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Feb 21, 2018

Download

Documents

dinhmien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

29.02.2008

Meta-programming in Groovy

Lars Blumberg

Christoph Hartmann

Arvid Heise

Page 2: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

2Meta-programming in Groovy

James Strachan wrote

The Groovy Story

Page 3: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 4: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

4Meta-programming in Groovy

My class is your class

Groovy

Java

Java Runtime Environment

Adopted from Gina, p. 5

Page 5: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 6: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

6Meta-programming in Groovy

Groovy programming concepts

Beauty through brevity

Page 7: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

7Meta-programming in Groovy

Expose the Magic

Page 8: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Running Example

class Dog {

String name = 'dog'

void bark() {

System.out.println "$name: woof"

}

String toString() {

name

}

}

8Meta-programming in Groovy

Page 9: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Metaclasses in Groovy

9Meta-programming in Groovy

Page 10: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Creating Objects

10Meta-programming in Groovy

static void main(args) {new Dog()

}

ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])

Page 11: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Getting Metaclass for Classes

11Meta-programming in Groovy

static void main(args) {new Dog()

}

ScriptBytecodeAdapter.invokeNewN( DogExample.class, Dog.class, new Object[0])

Page 12: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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')

Page 13: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Method Invocation

13Meta-programming in Groovy

static void main(args) {dog = new Dog()dog.bark()

}

ScriptBytecodeAdapter. invokeMethodN( DogExample.class, dog, "bark", new Object[0])

Page 14: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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])

Page 15: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 16: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 17: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 18: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Evaluating Expressions

18Meta-programming in Groovy

static void main(args) {shell = new GroovyShell()shell.evaluate("1+1")

}

Page 19: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Become Magician

19Meta-programming in Groovy

Page 20: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Keep It Simple

20Meta-programming in Groovy

Class

XML

Application

TableHibernate

Page 21: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Keep It Simple

21Meta-programming in Groovy

Class

Application

TableEJB

Page 22: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

Keep It Simple

22Meta-programming in Groovy

Application

TableGroovy

Page 23: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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

Page 24: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

We love you …

24Meta-programming in Groovy

Page 25: Meta-programming in Groovy - · PDF fileMeta-programming in Groovy 3 Groovy is an agile dynamic language for the Java Platform with many features that are inspired by languages like

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