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 invokeMethod & getProperty</h1> <p>Since 1.0, Groovy supports the ability to intercept <em>all</em> method and property access via the <code>invokeMethod</code> and <code>get/setProperty</code> hooks. If you only want to intercept failed method/property access take a look at <a class="confluence-link" href="/display/GROOVY/Using+methodMissing+and+propertyMissing" data-linked-resource-id="9765917" data-linked-resource-type="page" data-linked-resource-default-alias="Using methodMissing and propertyMissing" data-base-url="http://docs.codehaus.org">Using methodMissing and propertyMissing</a>.</p> <h2>Overriding invokeMethod</h2> <p>In any Groovy class you can override <code>invokeMethod</code> which will essentially intercept all method calls (to intercept calls to existing methods, the class additionally has to implement the <code>GroovyInterceptable</code> interface). This makes it possible to construct some quite interesting DSLs and builders.</p> <p>For example a trivial <code>XmlBuilder</code> could be written as follows (note Groovy ships with much richer XML APIs and this just serves as an 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 XmlBuilder { def out XmlBuilder(out) { this.out = out } def invokeMethod(String name, args) { out << "<$name>" if(args[0] instanceof Closure) { args[0].delegate = this args[0].call() } else { out << args[0].toString() } out << "</$name>" } } def xml = new XmlBuilder(new StringBuffer()) xml.html { head { title "Hello World" } body { p "Welcome!" } } </pre></td></tr></table> <p>Another simple usage of invokeMethod is to provide simple AOP style around advice to existing methods. Here is a simple logging example implemented with invokeMethod:</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 MyClass implements GroovyInterceptable { def invokeMethod(String name, args) { System.out.println ("Beginning $name") def metaMethod = metaClass.getMetaMethod(name, args) def result = metaMethod.invoke(this, args) System.out.println ("Completed $name") return result } } </pre></td></tr></table> <h2>Overriding getProperty and setProperty</h2> <p>You can also override property access using the <code>getProperty</code> and <code>setProperty</code> property access hooks. For example it is possible to write a trival "Expandable" object using this technique:</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 Expandable { def storage = [:] def getProperty(String name) { storage[name] } void setProperty(String name, value) { storage[name] = value } } def e = new Expandable() e.foo = "bar" println e.foo </pre></td></tr></table>
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