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
<h1>Using methodMissing & propertyMissing</h1> <p>Since 1.5, Groovy supports the concept of "methodMissing". This differs from <a class="confluence-link unresolved" data-content-title="Using invokeMethod & getProperty" data-linked-resource-default-alias="Using invokeMethod &amp; getProperty" href="#">invokeMethod</a> in that it is only invoked in the case of failed method dispatch.</p> <p>There are a couple of important aspects to this behaviour:</p> <ol> <li>Since method/propertyMissing only occur in the case of failed dispatch, they are expensive to execute</li> <li>Since method/propertyMissing aren't intercepting EVERY method call like <a class="confluence-link unresolved" data-content-title="Using invokeMethod & getProperty" data-linked-resource-default-alias="Using invokeMethod &amp; getProperty" href="#">invokeMethod</a> they can be more efficient with a few meta-programming tricks</li> </ol> <h2>Using methodMissing with dynamic method registration</h2> <p>Typically when using methodMissing the code will react in some way that makes it possible for the next time the same method is called, that it goes through the regular Groovy method dispatch logic.</p> <p>For example consider <a href="http://grails.org/GORM+-+Querying">dynamic finders</a> in GORM. These are implemented in terms of methodMissing. How does it work? The code resembles something like this:</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 GORM { def dynamicMethods = [...] // an array of dynamic methods that use regex def methodMissing(String name, args) { def method = dynamicMethods.find { it.match(name) } if(method) { GORM.metaClass."$name" = { Object[] varArgs -> method.invoke(delegate, name, varArgs) } return method.invoke(delegate,name, args) } else throw new MissingMethodException(name, delegate, args) } } </pre></td></tr></table> <p>Notice how, if we find a method to invoke then we dynamically register a new method on the fly using <a class="confluence-link" href="/display/GROOVY/ExpandoMetaClass" data-linked-resource-id="79517" data-linked-resource-type="page" data-linked-resource-default-alias="ExpandoMetaClass" data-base-url="http://docs.codehaus.org">ExpandoMetaClass</a>. This is so that the next time the same method is called it is more efficient. This way methodMissing doesn't have the overhead of invokeMethod AND is not expensive for the second call</p> <h2>Using propertyMissing</h2> <p>Groovy also supports propertyMissing for dealing with property resolution attempts. For a getter you use a propertyMissing definition that takes a String argument:</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 Foo { def propertyMissing(String name) { name } } def f = new Foo() assertEquals "boo", f.boo </pre></td></tr></table> <p>For a setters you add a second propertyMissing definition that takes a value argument:</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 Foo { def storage = [:] def propertyMissing(String name, value) { storage[name] = value } def propertyMissing(String name) { storage[name] } } def f = new Foo() f.foo = "bar" assertEquals "bar", f.foo </pre></td></tr></table> <p>As with methodMissing you will likely want to dynamically register new properties at runtime to improve the performance of you code.</p> <h2>Static methods and properties</h2> <p>You can add methodMissing and propertyMissing that deals with static methods and properties via <a class="confluence-link" href="/display/GROOVY/ExpandoMetaClass" data-linked-resource-id="79517" data-linked-resource-type="page" data-linked-resource-default-alias="ExpandoMetaClass" data-base-url="http://docs.codehaus.org">ExpandoMetaClass</a></p>
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