DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Groovy AST Browser: Now on the Web

Groovy AST Browser: Now on the Web

Roshan Dawrani user avatar by
Roshan Dawrani
·
Aug. 21, 10 · Interview
Like (0)
Save
Tweet
Share
10.24K Views

Join the DZone community and get the full member experience.

Join For Free

This article introduces the web based version of the Groovy AST Browser tool - http://groovyastbrowser.appspot.com/.

What is Groovy's AST?

Groovy compiler maps the groovy source code in a tree form called Abstract Syntax Tree (AST). The compiler scans and transforms this source tree in various phases and finally generates the bytecode using on the final, transformed AST. In addition to the direct source-code-to-AST mapping, 2 other major sources of AST changes are -

  • AST modifications made by the compiler itself to make many of the groovy features possible, like MOP or boilerplate code reduction, etc.
  • AST Transformations feature, which allows the groovy users to plug into the compilation process and manipulate the AST mid-way so that the final code gets generated as they desire.

An example of compiler introducing AST changes would be the generation of getters/setters for the groovy properties, as shown below:

Code provided by user:

class Test {
String name
}

The class above gets modified by the groovy compiler as below, where the methods getName() and setName() are not provided by the user, but generated by groovy compiler to save the user from providing such boilerplate code:

class Test {
private String Name
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

An example of AST changes being introduced by Groovy AST Transformations could be the usage of @Delegate annotation provided by groovy:

Code provided by user:

class NewList {
@Delegate List list = new ArrayList()
}

The code above gets transformed by groovy as below so that you can all the ArrayList calls made on NewList instances, which are transparently delegated to the private field "list":

class NewList implements List, Iterable, Collection {
private List list = new ArrayList();
public boolean add(Object item) {
list.add(item);
}
public int size() {
return list.size();
}
...
...
...// all other members of the ArrayList class
...

}

Exploring the AST in various scenarios across various compilation phases can greatly help one understand how groovy internally works.

Out-of-the-box, groovy installation comes with 2 tools - Groovy Console and Groovy AST Browser - while Groovy Console lets you quickly experiment with the language features, AST Browser helps you visualize how the groovy compiler takes your source code all the way across to its final form - the bytecode and how it transforms it on the way in multiple phases.

While it is great to have these tools locally installed, the availability of these tools online provides an alternative to those who don’t want to have the local installations but still want to experiment with the language, explore its various features, etc (imagine having a groovy-discussion with your ruby, scala or other non-groovy collagues - wouldn’t such online tools be helpful there?).

While http://groovyconsole.appspot.com/ made the Groovy Console available online, a missing piece was - Groovy’s AST Browser. Now the Groovy source’s AST can be explored online here: http://groovyastbrowser.appspot.com/

How the AST Browser may help?

Say, you wanted to understand how "println ‘hello world’" works in groovy, and how is it mapped to a class for the JVM?

Here is answer shown by Groovy AST Browser - it shows that your 1-liner script resulted in a full class that was generated by groovy - it has a main(args) method and your script code became part of a run() method (which is internally invoked from main())

Ex 1: A 1-liner script converted to a full class by Groovy



Next is the AST shown for the @Delegate example discussed above and it clearly shows how the methods on the delegate's type are added onto delegating type and all these methods basically forward the calls to the delegate object:

Ex 2: AST for @Delegate AST Transformation usage

 

One little feature to mention about this AST browser is the "Include Synthetic Members" option. Groovy generates a lot of synthetic members to support its internal workings - for example, to support its MOP features. Sometimes you may want to exploring these synthetic members and at other times you may want to wish them away from your view. The "Include Synthetic Members" option can be used for the same. Following are the 2 AST snapshots for the same code - one with Groovy’s internally used methods and the other without them:

Ex 3: An AST snapshot with synthetic members:

 

Ex 4: An AST snapshot without synthetic members:

Finally, if you are interested in exploring the AST for a given piece of code across various compilation phases, you can use the "phase" selector on the AST Browser and refresh the AST generated for that phase, as shown below:

Ex 5: Exploring AST across various compilation phases

That's all there is to this web-based Groovy AST Browser. I hope, the information provided in the article and the web-based version of the Groovy AST Browser, are useful to you.

Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Scaling Your Testing Efforts With Cloud-Based Testing Tools
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Chaos Data Engineering Manifesto: 5 Laws for Successful Failures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: