mopping up with groovy
DESCRIPTION
An introduction to using the Meta Object Protocol within GroovyTRANSCRIPT
![Page 2: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/2.jpg)
What is a MOP?
![Page 3: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/3.jpg)
Duck Typing
• POJ (Plain Old Java) – you can interact with an object based on what it is (Class)
• Groovy – you can interact based on which properties and methods are available on the object (MetaClass)
![Page 4: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/4.jpg)
Modifying Class Behaviour at Runtime
• AKA Monkey Patching
• AKA Duck Punching
• see whytheluckystiff's My Complete List Of Substitute Names For The Maneuver We Now Know To Be Monkeypatching
![Page 5: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/5.jpg)
Why Do It?
• it has an amusing name
• it makes you more attractive to the opposite (or same) sex
• it allows you to write more succinct, expressive code
![Page 6: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/6.jpg)
But I Feel Dirty …
• you may need to change how you think about things
• the laws are different in the duck typing universe
• question old attitudes and conventions
![Page 7: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/7.jpg)
How Does Groovy Implement a MOP?
![Page 8: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/8.jpg)
The MetaClass
• every Groovy object has one
• richer introspection and invocation interface than Java Class
• you can set the MetaClass for an object to change the rules for how it can behave
![Page 9: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/9.jpg)
The Final Shape of an Object
Added Methods + Properties+ = MetaClassClass
![Page 10: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/10.jpg)
The ExpandoMetaClass
• core of Groovy metaprogramming
• allows you to customise behaviour on the fly
• when you ask a newly created GroovyObject for its metaClass, this is what you get back
ExpandoMetaClass<<interface>>MetaClassGroovyObject
![Page 11: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/11.jpg)
What Can You Do With It?
• borrow methods from other classes
• add or override constructors
• dynamically create method names
• add methods to interfaces
• add or override instance methods
• add or override properties
• add or override static methods
http://groovy.codehaus.org/ExpandoMetaClass
![Page 12: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/12.jpg)
Adding a Class Method
![Page 13: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/13.jpg)
Adding a Property
![Page 14: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/14.jpg)
Overriding an Instance Method
![Page 15: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/15.jpg)
The Same In Ruby
![Page 16: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/16.jpg)
Invocation/Access Hooks
• invokeMethod
• getProperty
• setProperty
• propertyMissing
• methodMissing
![Page 17: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/17.jpg)
AOP with invokeMethod
![Page 18: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/18.jpg)
Flexible Methods with methodMissing
![Page 19: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/19.jpg)
MOP!!...huh! What is it Good For?
![Page 20: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/20.jpg)
Syntactic Sugar
![Page 21: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/21.jpg)
Mocking/Stubbing Behaviour
![Page 22: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/22.jpg)
DSL - The Original Class
![Page 23: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/23.jpg)
Adding Some MOP Trickery
![Page 24: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/24.jpg)
Icecream Time
![Page 25: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/25.jpg)
Replacing/Simplifying Frameworks and Patterns
• Spring, EasyMock/JMock
• circumvent the rigidity of the Java language
• patterns like Builder
• Dependency Injection
• with Groovy these can be simplified or disappear altogether
![Page 26: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/26.jpg)
Finally
![Page 27: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/27.jpg)
The Pros and Cons
Pros
• flexibility
• extensibility
• power
Cons• sometimes clunky
syntax• slower performance
![Page 28: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/28.jpg)
With Great Power…
• don’t get wowed by the coolness factor
• remember to KISS
• the end result should be more understandable, not less
• keep pushing the boundaries but exercise judgment
![Page 29: Mopping Up With Groovy](https://reader034.vdocuments.mx/reader034/viewer/2022042700/554f45b8b4c90572088b563a/html5/thumbnails/29.jpg)
Further Reading and References
• http://www.gnu.org/software/guile/docs/goops/Metaobjects-and-the-Metaobject-Protocol.html#Metaobjects%20and%20the%20Metaobject%20Protocol
• http://en.wikipedia.org/wiki/Meta-object_protocol• http://www.grails-exchange.com/files/GraemeRocher%20-
%20Keynote-DynamicGroovyMetaMagic.pdf• http://groovy.codehaus.org/ExpandoMetaClass• http://whytheluckystiff.net/articles/
seeingMetaclassesClearly.html• http://groovy.codehaus.org/ExpandoMetaClass• http://hackety.org/2007/08/10/
myCompleteListOfSubstitutePhrasesForTheActWeNowKnowToBeMonkeypatching.html
• Groovy In Action