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>AST Transformations</h1><p>Although at times, it may sound like a good idea to extend the syntax of Groovy to implement new features (like this is the case for instance for multiple assignments), most of the time, we can't just add a new keyword to the grammar, or create some new syntax construct to represent a new concept. However, with the idea of AST (Abstract Syntax Tree) Transformations, we are able to tackle new and innovative ideas without necessary grammar changes.</p><p>When the Groovy compiler compiles Groovy scripts and classes, at some point in the process, the source code will end up being represented in memory in the form of a Concrete Syntax Tree, then transformed into an Abstract Syntax Tree. The purpose of AST Transformations is to let developers hook into the compilation process to be able to modify the AST before it is turned into bytecode that will be run by the JVM.</p><p><strong>AST Transformations provides Groovy with improved compile-time metaprogramming capabilities</strong> allowing powerful flexibility at the language level, without a runtime performance penalty.</p><h2>Annotation based AST Transformations</h2><p>One hook for accessing this capability is via annotations. In your Groovy code you can make use of one of more annotations to mark a class for receiving an AST transformation during compilation. Behind the scenes, an AST processor relevant to the annotation you are using is inserted into the compiler phases at the appropriate point.</p><p>There are a number of standard Annotations that cause AST transformations to be performed.</p><h3>Standard Annotations</h3><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2NoaWxkcmVuOmFsbD10cnVlfQ&locale=en_GB&version=2" data-macro-name="children" data-macro-parameters="all=true"></p><p><a class="confluence-link" href="/display/GROOVY/Grape" data-linked-resource-id="107806787" data-linked-resource-type="page" data-linked-resource-default-alias="Grape" data-base-url="http://docs.codehaus.org">Grape</a> also provides its own transformation with @Grab.</p><p> </p><h2>Implementing your own AST Transformations</h2><p> </p><p>There are two kinds of transformations: global and local transformations.</p><ul><li>Global transformations are applied to by the compiler on the code being compiled, wherever the transformation apply. Compiled classes that implement global tranasformations are in a JAR added to the classpath of the compiler and contain service locator file META-INF/services/org.codehaus.groovy.transform.ASTTransformation with a line with the name of the transformation class. The transformation class must have a no-args constructor and implement the org.codehaus.groovy.transform.ASTTransformationinterface. It will be run against every source in the compilation, so be sure to not create transformations which scan all the AST in an expansive and time-consuming manner, to keep the compiler fast.<br /><br /></li><li>Local transformations are transformations applied locally by annotating code elements you want to transform. For this, we reuse the annotation notation, and those annotations should implement org.codehaus.groovy.transform.ASTTransformation. The compiler will discover them and apply the transformation on these code elements.</li></ul><h3>See Also</h3><ul><li><a class="confluence-link" href="/display/GROOVY/Building+AST+Guide" data-linked-resource-id="126320645" data-linked-resource-type="page" data-linked-resource-default-alias="Building AST Guide" data-base-url="http://docs.codehaus.org">Building AST Guide</a> - decide how best to create AST</li><li><a class="confluence-link" href="/display/GROOVY/Compiler+Phase+Guide" data-linked-resource-id="126025822" data-linked-resource-type="page" data-linked-resource-default-alias="Compiler Phase Guide" data-base-url="http://docs.codehaus.org">Compiler Phase Guide</a> - decide on the compiler phase in which to perform the work</li><li>implementing a <a class="confluence-link" href="/display/GROOVY/Global+AST+Transformations" data-linked-resource-id="117900506" data-linked-resource-type="page" data-linked-resource-default-alias="Global AST Transformations" data-base-url="http://docs.codehaus.org">Global AST Transformations</a></li><li>implementing a <a class="confluence-link" href="/display/GROOVY/Local+AST+Transformations" data-linked-resource-id="117900504" data-linked-resource-type="page" data-linked-resource-default-alias="Local AST Transformations" data-base-url="http://docs.codehaus.org">local AST transformation</a></li></ul><p> </p><p> </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