Groovy Meta Programming Slides

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    Groovy Meta Programming Slides - Presentation Transcript

    1. Meta-programming in Groovy Lars Blumberg Christoph Hartmann Arvid Heise 29.02.2008
    2. James Strachan wrote The Groovy Story Meta-programming in Groovy 2
    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.groovy Code.class Java class loader Groovy class loader Loaded class Loaded class Adopted from Gina, p. 48 Meta-programming in Groovy 5
    6. Groovy programming concepts Beauty through brevity Meta-programming in Groovy 6
    7. Expose the Magic Meta-programming in Groovy 7
    8. Running Example class Dog { String name = 'dog' void bark() { System.out.println \"$name: woof\" } String toString() { name } } Meta-programming in Groovy 8
    9. Metaclasses in Groovy Meta-programming in Groovy 9
    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) { ScriptBytecodeAdapter. invokeMethodN( dog = new Dog() DogExample.class, dog, \"bark\", dog.bark() new Object[0]) } Meta-programming in Groovy 13
    14. Intercepting Method Calls static void main(args) { ScriptBytecodeAdapter. invokeMethodN( dog = new Dog() DogExample.class, dog, \"bark\", 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
    19. Become Magician Meta-programming in Groovy 19
    20. Keep It Simple XML Hibernate Class Table Application Meta-programming in Groovy 20
    21. Keep It Simple EJB Class Table Application Meta-programming in Groovy 21
    22. Keep It Simple Groovy Table Application Meta-programming in Groovy 22
    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. We love you … Meta-programming in Groovy 24
    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

    + Minh Chuc HoMinh Chuc Ho, 9 months ago

    custom

    453 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 453
      • 453 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 12
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?