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
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>Adding methods to an instance</p></td></tr></table></p> <h1>Adding Methods to an Instance</h1> <p>Normally when you add a <code>MetaMethod</code>, it is added for <em>all instances</em> of that class. However, you can dynamically add methods to individual instances by giving that instance its own <code>MetaClass</code>.</p> <h2>For Groovy version 1.6 and higher - any Groovy or Java object</h2> <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 str = "hello" str.metaClass.test = { "test" } assert "test" == str.test() </pre></td></tr></table> <h2>For Groovy prior to version 1.6 - <code>GroovyObjects</code> only!</h2> <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 test = "test" def gstr = "hello $test" // this is a GString, which implements GroovyObject def emc = new ExpandoMetaClass( gstr.class, false ) emc.test = { println "test" } emc.initialize() gstr.metaClass = emc gstr.test() // prints "test" </pre></td></tr></table> <p>Note that you <strong>cannot</strong> do 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> gstr.metaClass = new ExpandoMetaClass( gstr.class ) gstr.metaClass.test = { println "test" } </pre></td></tr></table> <p>because you <em>must</em> call <code>emc.initialize()</code> before making any method calls on the instance. But you can't add MetaMethods after calling <code>initialize()</code>! This is bit of a catch 22 because the ExpandoMetaClass is intercepting methods to itself. The solution is (as shown in the first example) to simply add the MetaMethods before assigning the new MetaClass to your instance.</p> <p>The other option is to set the set <code>emc.allowChangesAfterInit = true</code>. This will allow you to add additional methods on the MetaClass after it is in use.</p> <table class="wysiwyg-macro" data-macro-name="note" data-macro-parameters="title=Note" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vdGU6dGl0bGU9Tm90ZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Be sure to use the proper constructor, <code>new ExpandoMetaClass(MyClass,</code><code><strong>false</strong></code><code>)</code>. The <code>false</code> parameter keeps the MetaClass from being inserted into the Registry. Otherwise your new MetaClass will be used for all instances of <code>MyClass</code>, not just the instance it is assigned to.</p></td></tr></table> <table class="wysiwyg-macro" data-macro-name="note" data-macro-parameters="title=Compatibility" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vdGU6dGl0bGU9Q29tcGF0aWJpbGl0eX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Only works in Groovy 1.1-beta-3 and above. Use the Proxy class in older versions to achieve a per-instance behaviour change.</p></td></tr></table> <h2>If your Instance is not a GroovyObject</h2> <p>If your instance is a plain Java type, it will not implement <code>GroovyObject</code>, and consequently, will not have a <code>metaClass</code> property. In this case you must wrap your instance in a <a href="http://groovy.codehaus.org/api/groovy/util/Proxy.html">groovy.util.Proxy</a>:</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> ExpandoMetaClass emc = new ExpandoMetaClass( Object, false ) emc.boo = { "Surprise!" } emc.initialize() def obj = new groovy.util.Proxy().wrap( new Object() ) obj.setMetaClass( emc ) assert obj.boo() == "Surprise!" </pre></td></tr></table> <p>Note that this example is calling the <code>setMetaClass(..)</code> method rather than using the property notation in the previous example. This is because <code>Proxy</code> intercepts <em>method calls</em> only, not property access.</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