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
<h2>Where it all starts</h2> <p>Say you have a Groovy code like</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 'hi' </pre></td></tr></table> <p>As explained in <a class="confluence-link" href="/display/GROOVY/Groovy+Backstage" data-linked-resource-id="19168" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Backstage" data-base-url="http://docs.codehaus.org">Groovy Backstage</a>, there is bytecode generated to achieve the desired behaviour of printing to stdout.</p> <p>The easiest way of looking at the generated bytecode is to groovyc your Groovy source to a class file and process it with a Java Decompiler (e.g. JAD). See also: <a class="confluence-link" href="/display/GROOVY/From+source+code+to+bytecode" data-linked-resource-id="22517" data-linked-resource-type="page" data-linked-resource-default-alias="From source code to bytecode" data-base-url="http://docs.codehaus.org">From source code to bytecode</a></p> <p>The resulting code looks as follows (only the relevant snippet):</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>... Object aobj[] = { "hi" }; ScriptBytecodeAdapter.invokeMethod(this, "println", ((Object) (aobj))); ... </pre></td></tr></table> <h2>Invocation scheme</h2> <p>There is a delegation scheme like</p> <ul class="alternate"> <li><code>ScriptBytecodeAdapter.invokeMethod(...)</code> (static method) <ul class="alternate"> <li><code>InvokerHelper.invokeMethod(...)</code> (static method) <ul class="alternate"> <li><code>Invoker.invokeMethod(...)</code> (instance method called on InvokerHelper's single instance)</li> </ul> </li> </ul> </li> </ul> <h2>MetaClass and MetaClassRegistry</h2> <p>Invoker calls <code>invokeMethod(...)</code> on the <strong>MetaClass</strong> of our class (with exceptions, see below). It finds this MetaClass by looking it up in the <strong>MetaClassRegistry</strong>. The Invoker holds a single instance of this registry.</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> When working with the MetaClassRegistry, <code>InvokerHelper.getInstance().getMetaRegistry()</code> is the only instance, you should ever use.</p></td></tr></table> <p>Exceptions (when <code>MetaClass.invokeMethod(...)</code> is not used):</p> <ul class="alternate"> <li>for Closures, <code>Closure.invoke(...)</code> is used</li> <li>for GroovyObjects <em>obj</em> of type <code>GroovyInterceptable</code>, <code>obj.invokeMethod(methodName,asArray(arguments))</code> is called</li> <li>for any other <code>GroovyObject</code> <em>obj</em> when method invokation through its MetaClass fails, <code>obj.invokeMethod(methodName,asArray(arguments))</code> is called</li> </ul> <p><code>MetaClass.invokeMethod(...)</code> finally cares for the invokation, either by reflection or by dynamic bytecode generation. Dynamic bytecode generation is supposed to be faster. For a class <code>MyClass</code> it generates <code>gjdk.groovy.lang.MyClass_GroovyReflector</code> with an <code>invoke</code> method.</p> <p><em>Does MyClass_GroovyReflector contain methods according to MyClass.groovy that can be called directly</em> <img class="emoticon emoticon-question" data-emoticon-name="question" border="0" src="/s/en_GB/3278/15/_/images/icons/emoticons/help_16.png" alt="(question)" title="(question)" /></p> <p>The cool thing about MetaClass is that you can dynamically add or remove methods to it. One can even replace the whole MetaClass in the MetaClassRegistry. See ProxyMetaClass for an example.</p> <p>back to <a class="confluence-link" href="/display/GROOVY/Groovy+Backstage" data-linked-resource-id="19168" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Backstage" data-base-url="http://docs.codehaus.org">Groovy Backstage</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