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>Evaluating the MetaClass runtime</h1> <p>Since 1.1, Groovy supports a much richer set of APIs for evaluating the MetaClass runtime. Using these APIs in combination with <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> makes Groovy an extremely powerful language for meta-programming</p> <h2>Finding out methods and properties</h2> <p>To obtain a list of methods ( or <a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html">MetaMethod</a> instances in Groovy speak) for a particular Groovy class use can inspect its MetaClass:</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> println obj.metaClass.methods println obj.metaClass.methods.find { it.name.startsWith("to") } </pre></td></tr></table> <p>The same can be done for properties:</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> println obj.metaClass.properties println obj.metaClass.properties.find { it.name.startsWith("to") } </pre></td></tr></table> <h2>Using respondsTo and hasProperty</h2> <p>Obtaining a list of methods sometimes is a little more than what you want. It is quite common in meta-programming scenarios to want to find out if an object supports a particular method.</p> <p>Since 1.1, you can use respondsTo and hasProperty to achieve 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 Foo { String prop def bar() { "bar" } def bar(String name) { "bar $name" } def add(Integer one, Integer two) { one + two} } def f = new Foo() if(f.metaClass.respondsTo(f, "bar")) { // do stuff } if(f.metaClass.respondsTo(f, "bar", String)) { // do stuff } if(!f.metaClass.respondsTo(f, "bar", Integer)) { // do stuff } if(f.metaClass.respondsTo(f, "add", Integer, Integer)) { // do stuff } if(f.metaClass.hasProperty(f, "prop")) { // do stuff } </pre></td></tr></table> <p>The respondsTo method actually returns a List of MetaMethod instances so you can use it to both query and evaluate the resulting list. </p> <table class="wysiwyg-macro" data-macro-name="note" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vdGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>respondsTo only works for "real" methods and those added 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> and not for cases where you override invokeMethod or methodMissing. It is impossible in these cases to tell if an object responds to a method without actually invoking the method.</p></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