Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

cdac@parag.gajbhiye@groovy metaprogrammning

1,130 views

Published on

  • Be the first to comment

  • Be the first to like this

cdac@parag.gajbhiye@groovy metaprogrammning

  1. 1. Meta-programming in Groovy Lars Blumberg Christoph Hartmann Arvid Heise 29.02.2008
  2. 2. James Strachan wrote The Groovy StoryMeta-programming in Groovy 2
  3. 3. “Groovy is an agile dynamic language for the Java Platform with many features that are inspired bylanguages 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. 4. My class is your class Groovy Java Java Runtime Environment Adopted from Gina, p. 5Meta-programming in Groovy 4
  5. 5. Precompiled vs. direct modeCode.groovy groovyc Code.class Code.groovy Java class loader Groovy class loaderLoaded class Loaded class Adopted from Gina, p. 48 Meta-programming in Groovy 5
  6. 6. Groovy programming concepts Beauty through brevity Meta-programming in Groovy 6
  7. 7. Expose the MagicMeta-programming in Groovy 7
  8. 8. Running Exampleclass Dog { String name = dog void bark() { System.out.println "$name: woof" } String toString() { name }} Meta-programming in Groovy 8
  9. 9. Metaclasses in GroovyMeta-programming in Groovy 9
  10. 10. Creating Objectsstatic void main(args) { ScriptBytecodeAdapter.invokeNewN( new Dog() DogExample.class, Dog.class,} new Object[0]) Meta-programming in Groovy 10
  11. 11. Getting Metaclass for Classesstatic void main(args) { ScriptBytecodeAdapter.invokeNewN( new Dog() DogExample.class, Dog.class,} new Object[0]) Meta-programming in Groovy 11
  12. 12. Example for Custom Metaclassclass WaldiMeta extends MetaClassImpl { WaldiMeta() { super(GroovySystem.getMetaClassRegistry(), Dog.class) initialize() }}// Instance-based MetaClasswaldi = new Dog(name: Waldi)waldi.metaClass = new WaldiMeta()// Class-based MetaClassGroovySystem.getMetaClassRegistry().setMetaClass(Dog.class, new WaldiMeta())waldi = new Dog(name: Waldi) Meta-programming in Groovy 12
  13. 13. Method Invocationstatic void main(args) { dog = new Dog() ScriptBytecodeAdapter. invokeMethodN( dog.bark() DogExample.class, dog, "bark",} new Object[0]) Meta-programming in Groovy 13
  14. 14. Intercepting Method Callsstatic void main(args) { dog = new Dog() ScriptBytecodeAdapter. invokeMethodN( dog.bark() DogExample.class, dog, "bark",} new Object[0]) Meta-programming in Groovy 14
  15. 15. Interception in GroovyInterceptableclass 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. 16. Interception using Interceptorclass 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. 17. Interception with MetaClassclass 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 barkdog.metaClass = new BrunoMeta() Waldi: woofdog.bark() Waldi is about to speakdog.speak() Waldi does not speak Meta-programming in Groovy 17
  18. 18. Evaluating Expressions static void main(args) { shell = new GroovyShell() shell.evaluate("1+1") }Meta-programming in Groovy 18
  19. 19. Become MagicianMeta-programming in Groovy 19
  20. 20. Keep It Simple XMLClass Hibernate Table Application Meta-programming in Groovy 20
  21. 21. Keep It SimpleClass EJB Table Application Meta-programming in Groovy 21
  22. 22. Keep It Simple Groovy Table ApplicationMeta-programming in Groovy 22
  23. 23. 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
  24. 24. We love you …Meta-programming in Groovy 24
  25. 25. 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

×