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>Use this page to list use cases for AST-level macros in Groovy.</p> <p>How do you want to use such macros? Show examples of their usage (without showing how they're implemented). Add to the bottom of the page.</p> <h2>Increase code readability</h2> <h4>Rearrange code portions</h4> <p>The end-weight principle for natural languages says the longer stuff should be at the end of a sentence.<br /> In Groovy/Java, while and if statements are usually heavy in the block/statement, and light in the condition.<br /> When the statement/block is short and the condition long, it can be more readable to put the block last.<br /> Eg:</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> do{ i++; return c }if( ( severance.taxAmount >= TaxYear2006.taxAmounts.severanceLimit[ 'dependent' ] || ... ... ... ... ... ... ) </pre></td></tr></table> <p>is transformed to:</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> if( ... ... ... ){ i++; return c } </pre></td></tr></table> <p>We could use 'unless' and 'until' if using 'if' and 'while' caused problems.</p> <h4>Common modifier</h4> <p>When a long list of methods and/or fields have the same modifier/s,<br /> it may be more readable to apply them to a whole block, eg:</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> static{ def a(){ ... } def b(){ ... } } </pre></td></tr></table> <p>becomes</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> static a(){ ... } static b(){ ... } </pre></td></tr></table> <h4>Transform statements</h4> <p>Eg:</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> def genUniqueScriptName(){ static int i=1; 'Script_' + i++ } </pre></td></tr></table> <p>becomes</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 UniqueScriptName{ static int i=1; static gen(){ 'Script_' + i++ } </pre></td></tr></table> <p>triggered by the 'static' modifier of a field in a function.</p> <h4>Names for operators</h4> <p>Use names instead of symbols for operators, eg:</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> if( a and b ) </pre></td></tr></table> <p>becomes</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> if(a && b) </pre></td></tr></table> <p>or change their names to something shorter, eg:</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> if( a of A ) </pre></td></tr></table> <p>becomes</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> if( a instanceof A ) </pre></td></tr></table> <h4>Equivalence of block and statement </h4> <p>Forgoing curlies for all statements, making a statement and block equivalent always:<br /> Eg:</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> try A.call() catch(e) if(e instanceof ABCException) throw new BusinessException() </pre></td></tr></table> <h2>Use other natural languages with Groovy</h2> <p>Enable fuller internationalization of Groovy, eg:</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> mientras( i < 10 ){ ... ... ... } </pre></td></tr></table> <p>would convert to </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> while( i < 10 ){ ... ... ... } </pre></td></tr></table> <p>if a Spanish option for Groovy was loaded. </p> <h2>More Seamless Integration with Java and Java-like languages</h2> <p>Embed Java code within Groovy without quoting it,<br /> and without needing to bind variables to the same name, ie, no binding.setVariable('i', i).<br /> eg:</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> int i= 0 java('jdk1.5.0_07'){ System.out.println i; class ... ... ... } </pre></td></tr></table> <h2>Simplify the Antlr lexer/parser</h2> <p>Many existing syntactic sugars could be re-implemented as macros in a standard macro library.<br /> This might simplify the Antlr lexer/parser, enabling better maintenance of and extensions to it.</p> <p>Eg, properties where:</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> String reading= 'OK </pre></td></tr></table> <p>expands to:</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> private String reading= 'OK' public String getReading(){ this.xxxx } public void setReading( String s ){ this.xxxx= s } </pre></td></tr></table> <p>Other examples:</p> <ul> <li>for( i in ... ) expanding to code using iterator</li> <li>shorthand notation for invoking method with closure/s at last parameter/s</li> </ul>
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