Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>Overriding invokeMethod, getProperty and setProperty</p></td></tr></table></p> <h1>ExpandoMetaClass - Overriding invokeMethod, getProperty and setProperty</h1> <p>It is also possible to override the methods invokeMethod, getProperty and setPropety on ExpandoMetaClass thus allowing even more dynamic behaviour.</p> <h3>Overriding invokeMethod</h3> <p>As an example of overring invokeMethod, take this simple example:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Stuff { def invokeMe() { "foo" } } Stuff.metaClass.invokeMethod = { String name, args -> def metaMethod = Stuff.metaClass.getMetaMethod(name, args) def result if(metaMethod) result = metaMethod.invoke(delegate,args) else { result = "bar" } result } def stf = new Stuff() assert "foo" == stf.invokeMe() assert "bar" == stf.doStuff() </pre></td></tr></table> <p>So what is happening here? Well firstly we've overriden invokeMethod by assigning it an appropriate closure, but in addition we first look-up a <a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html">MetaMethod</a> with the line:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def metaMethod = delegate.class.metaClass.getMetaMethod(name) </pre></td></tr></table> <p>A <a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html">MetaMethod</a> in Groovy is a method that is known to exist on the MetaClass whether added at runtime or whatever, thus we check if there is an existing <a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html">MetaMethod</a> and if there isn't we simply return "bar", hence the behaviour of the assert statements is correct.</p> <h3>Overriding getProperty and setProperty</h3> <p>Again overriding getProperty and setProperty is similar to the above:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class Person { String name = "Fred" } Person.metaClass.getProperty = { String name -> def metaProperty = Person.metaClass.getMetaProperty(name) def result if(metaProperty) result = metaProperty.getProperty(delegate) else { result = "Flintstone" } result } def p = new Person() assert "Fred" == p.name assert "Flintstone" == p.other </pre></td></tr></table> <p>The important thing to note here is that instead of a MetaMethod we look-up a <a href="http://groovy.codehaus.org/api/groovy/lang/MetaProperty.html">MetaProperty</a> instance if that exists we call the getProperty method of the <a href="http://groovy.codehaus.org/api/groovy/lang/MetaProperty.html">MetaProperty</a> passing the delegate (ie the instance of the class).</p> <p>The only different with setProperty is you need the value in the method signature and to call setProperty on the MetaProperty:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Person.metaClass.setProperty = { String name, value -> ... if(metaProperty) metaProperty.setProperty(delegate, value) ... } </pre></td></tr></table> <h3>Useful References</h3> <ul> <li><a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html">MetaMethod</a></li> <li><a href="http://groovy.codehaus.org/api/groovy/lang/MetaProperty.html">MetaProperty</a></li> </ul>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced