SlideShare una empresa de Scribd logo
1 de 70
Meta-Programming with AST
Transformations in Groovy
Gagan Agrawal
Xebia
Agenda

Meta-Programming with AST Transformation

What ?

How ?

Why ?
Metaprogramming
What is Metaprogramming

Is the writing of computer programs that write or
manipulate other programs
Minimizes the number of lines of code to express
a solution
Reduces development time
Could be Run-time or Compile-time
Groovy
Run-Time : MetaClass
Compile-Time : AST Transformations
Concrete Syntax Tree
Concrete Syntax Tree

Is a representation of grammar in a tree-like form.

Is a one-to-one mapping from the grammar to a
tree-form.
Concrete Syntax Tree


Example in C
return a+2;
Abstract Syntax Tree
Abstract Syntax Tree

Is a tree representation of the abstract syntactic
structure of source code
Each node of the tree denotes a construct in
source code
'Abstract' – Does not represent every detail that
appears in the real syntax
E.g.
Parentheses are implicit
Semicolon ';' is discarded
Abstract Syntax Tree


return a+2;
AST Transformation
AST Transformations

Compile-time metaprogramming
Allows developers to hook into the compilation
process
To modify Abstract Syntax Tree before bytecode
generation
Performance is better than Run-time
metaprogramming
AST Transformation
Compiler Phases
  Initialization : Source files are opened and
    environment configured
  Parsing : The grammar is used to produce tree of
    tokens representing the source code
  Conversion : An Abstract Syntax Tree(AST) is created
    from token trees
  Semantic Analysis : Performs consistency and validity
    checks that the grammar can't check for and resolves
    classes
AST Transformation
Compiler Phases :
  Canonicalization: Complete the AST
  Instruction Selection : instruction set is choosen, e.g.
    Java5 or pre Java5
  Class Generation : creates binary output in memory
  Output : Write the binary output to the file system
  Finalization : Perform any last cleanup
Expressions, Statements and Blocks
Expressions
An expression is a construct made up of variables,
operators and method invocations

Evaluates to a single value
E.g. 2+3 evaluates to 5


A variable is an expression because it denotes a
value in memory
     int var = 0;
Expression

 Compound Expression
  1*2*3
  x+y/100
  (x+y)/100
 Other Types
  Cast Expression - (int c = (int)d)
  Constant Expression – (null, true, false)
  Field Expression – (this.foo)
  Method Call Expression – (object.method())
  Boolean Expression - (value1==value2)
Statements
Roughly equivalent to sentences in natural
languages.
Forms a complete unit of execution.
Types
Expression Statements
Declaration Statements
Control Flow Statements
Expression Statements

Created by terminating expression with
semicolon (;)
Assignment Statement
aValue = 8933.234;
Increment Statement
aValue++;
Method Invocation Statement
System.out.println("Hello World!");
Object Creation Statement
Bicycle myBike = new Bicycle();
Declaration and Control flow Statements

Declaration Statements
double aValue = 8933.234;
Control flow Statements
If-then and if-then-else Statements
Switch Statements
While and do-while Statements
For Statements
Block Statements

A block is a group of zero or more statements
between balanced braces

Can be used anywhere a single statement is
allowed.
Block Statements

class BlockDemo {
      public static void main(String[] args) {
           boolean condition = true;
           if (condition) { // begin block 1
                    System.out.println("Condition is true.");
           } // end block one
           else { // begin block 2
                    System.out.println("Condition is false.");
           } // end block 2
      }
  }
AST Building Blocks
AST Building Blocks

AST can be transformed in one of the two ways

Core AST APIs

ASTBuilder
Core AST APIs
Core AST APIs



                                               A S T No d e




        A n n o ta te d No d e   G e n e r ic s T y p e       M o d u le N o d e   Sta te m e nt




           E x p r e s s io n
Core AST APIs

ASTNode
Base class for any AST node.
This class supports basic information used in all nodes of
the AST

Line and column number information.
Meta data of a node
A phase operation or transform can use this to transport arbitrary
information to another phase operation or transform provided
transform runs after the part storing the information.
Core AST APIs - Expression
Expression
BooleanExpression
CastExpression
ConstantExpression
ConstructorCallExpression
FieldExpression
MethodCallExpression
StaticMethodCallExpression
VariableExpression
Core AST APIs - Expression
 this.println(message) can be represented as-


  new MethodCallExpression(
     new VariableExpression("this"),
     new ConstantExpression("println"),
     new ArgumentListExpression(
        new ConstantExpression(message)
     )
  )
Core AST API - Statement
 Statement
BlockStatement
SwitchStatement
CaseStatement
BreakStatement
ExpressionStatement
IfStatement
TryCatchStatement
WhileStatement
ReturnStatement
Core AST API - Statement


 this.println(message) can be represented as-

  return new ExpressionStatement(
  new MethodCallExpression(
      new VariableExpression("this"),
      new ConstantExpression("println"),
      new ArgumentListExpression(
          new ConstantExpression(message)
      )
  )
  )
AST Builder
AST Builder

Three ways to build AST via ASTBuilder

Build From Strings

Build From Code

Build From a DSL-like Specification
Building AST From String
 def builder = new AstBuilder()

 List<ASTNode> nodes =
 builder.buildFromString(
 CompilePhase.SEMANTIC_ANALYSIS,
 true,
 """
   this.println('Hello World')
 """
 )
Building AST From String

Phase parameter tells AST from which phase to
return AST.
 "statementsOnly" boolean parameter tells the
builder to discard the generated top level Script
ClassNode.
 String paramter is the input
 The builder returns List<ASTNode>
Building AST From String
Produces following AST

BlockStatement
-> ExpressionStatement
   -> MethodCallExpression
     -> VariableExpression
     -> ConstantExpression
     -> ArgumentListExpression
       -> ConstantExpression
Building AST From String
Advantages
Does not require author to understand ASTNode subtypes
Allows author to target a CompilePhase
Communicates source code being generated
Robust - Should need no changes even if AST is updated in
release
Building AST From String
Disadvantages
IDE can not check syntax or grammar
IDE can not refactor across String
Some entities cannot be created, like the AST for a field
declaration
Building AST From Code


 def builder = new AstBuilder()

 List<ASTNode> nodes = builder.buildFromCode(
  CompilePhase.SEMANTIC_ANALYSIS,
  true){
    println "Hello World"
  }
Building AST From Code
Produces following AST

 BlockStatement
  -> ExpressionStatement
    -> MethodCallExpression
      -> VariableExpression
      -> ConstantExpression
      -> ArgumentListExpression
        -> ConstantExpression
Building AST From Code
Advantages
Clearly communicates source being generated
Does not require author to understand ASTNode subtypes
Allows author to target a CompilePhase
Robust - Should need no changes even if AST is updated in
a release
IDE supports syntax checking and refactoring in Closure
Building AST From Code
Disadvantages
 Some entities cannot be created, like the AST for a field
declaration
Building AST From DSL-like Specification
 List<ASTNode> nodes = new AstBuilder().buildFromSpec{
  block{
     expression {
        methodCall {
           variable 'this'
           constant 'println'
           argumentList {
              if(locale == 'US') constant 'Hello'
              if(locale == 'FR') constant 'Bonjour'
              else constant "Ni hao"
           }
        }
     }
  }
 }
Building AST From DSL-like Specification
Advantages
Allows conditionals (or any Groovy code) to be executed
during the AST building process.
Allows any ASTNode subtype to be created
Fully documented with lengthy examples in TestCase
Building AST From DSL-like Specification
Disadvantages
It can be difficult to determine what AST you need to
write
Verbose - does not always communicate the source being
created
Fragile - AST may need to change between major releases
IDE does not yet provide code tips
AST Transformation
Two types of AST Transformations

Local Transformations

Global Transformations
Local AST Transformations
Can be applied on a per class basis.

Can only be applied at semantic analysis or later
phases

Annotation Driven
@Retention - Retention Policy should be SOURCE
@Target – Can be Method, Field, Constructor ..//need to
verify
@GroovyASTTransformationClass
Local AST Transformations
Logging Example
 @Retention(RetentionPolicy.SOURCE)
 @Target([ElementType.METHOD])

 @GroovyASTTransformationClass("LoggingASTTrans
 formation")
 @interface WithLogging {

 }
Local AST Transformations
  @GroovyASTTransformation(phase=CompilePhase.
  SEMANTIC_ANALYSIS)

  class LoggingASTTransformation implements
   ASTTransformation{

      @Override
      public void visit(ASTNode[] nodes,
      SourceUnit
      source) {
           .....
        }
  }
Local AST Transformations
Usage

 class TestAst {
  @WithLogging
  void testMethod(){
    println "This is test Method"
  }
 }
Global AST Transformations
Global transformations are applied to by the
compiler on the code being compiled, wherever
the transformation apply.
A JAR should be added to the classpath of the
compiler
It should contain a service locator file at META-
INF/services/org.codehaus.groovy.transform.ASTTr
ansformation
File should have a line with the name of the
transformation class
Global AST Transformations
The transformation class must have a no-args
constructor and implement the
org.codehaus.groovy.transform.ASTTransformatio
n interface
It will be run against every source in the
compilation
Global AST Transformation




                Example
Global AST Transformation
Summary
Write an ASTTransformation subclass
Create a Jar metadata file containing the name of your
ASTTransformation
Create a Jar containing the class and metadata
Invoke groovyc with that Jar on your classpath.
Built-in Annotations
@Delegate
With @Delegate annotation, a class field becomes
an object to which method calls are delegated.

Example
  class Event{
  @Delegate Date when
  String title, url
  }
@Delegate
 Groovy compiler adds all of Date's methods to the
Event class.

 Behind the scene methods simply delegate the
call to Date field.
@Singleton
  class T {
  private static volatile T instance
  private T() {}
  static T getInstance () {
      if (instance) {
          instance
      } else {
          synchronized(T) {
              if (instance) {
                   instance
              } else {
                   instance = new T ()
              }
          }
      }
  }
  }
@Singleton

 @Singleton class T {
 //properties...
 //methods..
 }
@Immutable
No mutators (methods that modify internal state)
Class must be final
Fields must be private and final
equals, hashCode and toString must be
implemented in terms of the fields if you want to
compare your objects or use them as keys in e.g.
maps
@Immutable
Java code requires lot of boiler plate code.
Groovy requires only annotation @Immuatable on
class

 @Immutable final class Person {
  String first
  String last
 }
@PackageScope


 class PackageScopeExample {

 @PackageScope
 String name

 }
@Lazy
 class LazyExample {

 @Lazy pets = ['Cat', 'Dog', 'Bird']

  static main(args){
     def example = new LazyExample()
     println "Pets intialized :
 "+example.dump().contains('Cat')
     println "List size : "+example.pets.size()
     println "Pets intialized :
 "+example.dump().contains('Cat')
  }

 }
@Newify
Ruby Approach

 @Newify([Integer])
 void test(){
   println Integer.new(23)
 }
@Newify
Python Approach
  class Tree {
     def elements
     Tree(Object... elements) { this.elements =
     elements as List }
  }


 class Leaf {
    def value
    Leaf(value) { this.value = value }
 }
@Newify
 def buildTree() {
   new Tree(new Tree(new Leaf(1), new
     Leaf(2)), new Leaf(3))
 }

 @Newify([Tree, Leaf])def buildTree() {
   Tree(Tree(Leaf(1), Leaf(2)), Leaf(3))
 }
@Category
 interface Vehicle{
 String getName()
 }

 @Category(Vehicle)
 class FlyingAbility{
 def fly(){
   "I am ${name} and I fly"
 }
 }
@Mixin
 @Category(Vehicle)
 class DrivingAbility{
  def drive(){
     "I am ${name} and I drive"
  }
 }

 @Mixin([DrivingAbility, FlyingAbility])
 class JamesBondVehicle implements Vehicle{
  public String getName(){
     "James Bond Vehicle"
  }
 }
Where AST Transformations are used?
Grails 2.0
GormTransformer
ControllerActionTransformer
ControllerTransformer
GormToJpaTransformer
Spock framework
Grails Plugins
Audit-Trail Plugin
Rich Domain Classes Plugin
Redis Plugin
Neo4j Plugin
Thank You

Más contenido relacionado

La actualidad más candente

Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with GroovyAli Tanwir
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Dierk König
 
Sonar rules in action with walkmod
Sonar rules in action with walkmodSonar rules in action with walkmod
Sonar rules in action with walkmodRaquel Pau
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 featuresshrinath97
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeIan Robertson
 
Mirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Groupbaroquebobcat
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMDierk König
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java DevelopersAndres Almiray
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMashleypuls
 
C# for-java-developers
C# for-java-developersC# for-java-developers
C# for-java-developersDhaval Dalal
 
Groovy for java developers
Groovy for java developersGroovy for java developers
Groovy for java developersPuneet Behl
 
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf
 
Grooscript gr8conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8confGR8Conf
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Ovidiu Farauanu
 

La actualidad más candente (20)

Metaprogramming with Groovy
Metaprogramming with GroovyMetaprogramming with Groovy
Metaprogramming with Groovy
 
Meta Programming in Groovy
Meta Programming in GroovyMeta Programming in Groovy
Meta Programming in Groovy
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015
 
Groovy intro
Groovy introGroovy intro
Groovy intro
 
Exploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systemsExploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systems
 
Sonar rules in action with walkmod
Sonar rules in action with walkmodSonar rules in action with walkmod
Sonar rules in action with walkmod
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 features
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive Code
 
Mirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Group
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java Developers
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 
C# for-java-developers
C# for-java-developersC# for-java-developers
C# for-java-developers
 
Groovy for java developers
Groovy for java developersGroovy for java developers
Groovy for java developers
 
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume LaforgeGR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
Grooscript gr8conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8conf
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Groovy 2.0 webinar
Groovy 2.0 webinarGroovy 2.0 webinar
Groovy 2.0 webinar
 

Similar a Ast transformation

Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Tom Lee
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancementRakesh Madugula
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
JCConf 2021 - Java17: The Next LTS
JCConf 2021 - Java17: The Next LTSJCConf 2021 - Java17: The Next LTS
JCConf 2021 - Java17: The Next LTSJoseph Kuo
 
Introduction to c sharp
Introduction to c sharpIntroduction to c sharp
Introduction to c sharpimmamir2
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdfAdilAijaz3
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsLourens Naudé
 
ParaSail
ParaSail  ParaSail
ParaSail AdaCore
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandrarantav
 

Similar a Ast transformation (20)

Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Java 17
Java 17Java 17
Java 17
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
JCConf 2021 - Java17: The Next LTS
JCConf 2021 - Java17: The Next LTSJCConf 2021 - Java17: The Next LTS
JCConf 2021 - Java17: The Next LTS
 
Introduction to c sharp
Introduction to c sharpIntroduction to c sharp
Introduction to c sharp
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
Annotations
AnnotationsAnnotations
Annotations
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
ParaSail
ParaSail  ParaSail
ParaSail
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
Matlab isim link
Matlab isim linkMatlab isim link
Matlab isim link
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
 
c# at f#
c# at f#c# at f#
c# at f#
 

Más de Gagan Agrawal

Building Complex Data Workflows with Cascading on Hadoop
Building Complex Data Workflows with Cascading on HadoopBuilding Complex Data Workflows with Cascading on Hadoop
Building Complex Data Workflows with Cascading on HadoopGagan Agrawal
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)Gagan Agrawal
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and EnhancementsGagan Agrawal
 

Más de Gagan Agrawal (6)

Building Complex Data Workflows with Cascading on Hadoop
Building Complex Data Workflows with Cascading on HadoopBuilding Complex Data Workflows with Cascading on Hadoop
Building Complex Data Workflows with Cascading on Hadoop
 
Hadoop2
Hadoop2Hadoop2
Hadoop2
 
Graph db
Graph dbGraph db
Graph db
 
Hadoop
HadoopHadoop
Hadoop
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and Enhancements
 

Último

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Ast transformation

  • 1. Meta-Programming with AST Transformations in Groovy Gagan Agrawal Xebia
  • 2. Agenda Meta-Programming with AST Transformation What ? How ? Why ?
  • 4. What is Metaprogramming Is the writing of computer programs that write or manipulate other programs Minimizes the number of lines of code to express a solution Reduces development time Could be Run-time or Compile-time Groovy Run-Time : MetaClass Compile-Time : AST Transformations
  • 6. Concrete Syntax Tree Is a representation of grammar in a tree-like form. Is a one-to-one mapping from the grammar to a tree-form.
  • 7. Concrete Syntax Tree Example in C return a+2;
  • 9. Abstract Syntax Tree Is a tree representation of the abstract syntactic structure of source code Each node of the tree denotes a construct in source code 'Abstract' – Does not represent every detail that appears in the real syntax E.g. Parentheses are implicit Semicolon ';' is discarded
  • 12. AST Transformations Compile-time metaprogramming Allows developers to hook into the compilation process To modify Abstract Syntax Tree before bytecode generation Performance is better than Run-time metaprogramming
  • 13. AST Transformation Compiler Phases Initialization : Source files are opened and environment configured Parsing : The grammar is used to produce tree of tokens representing the source code Conversion : An Abstract Syntax Tree(AST) is created from token trees Semantic Analysis : Performs consistency and validity checks that the grammar can't check for and resolves classes
  • 14. AST Transformation Compiler Phases : Canonicalization: Complete the AST Instruction Selection : instruction set is choosen, e.g. Java5 or pre Java5 Class Generation : creates binary output in memory Output : Write the binary output to the file system Finalization : Perform any last cleanup
  • 16. Expressions An expression is a construct made up of variables, operators and method invocations Evaluates to a single value E.g. 2+3 evaluates to 5 A variable is an expression because it denotes a value in memory int var = 0;
  • 17. Expression Compound Expression 1*2*3 x+y/100 (x+y)/100 Other Types Cast Expression - (int c = (int)d) Constant Expression – (null, true, false) Field Expression – (this.foo) Method Call Expression – (object.method()) Boolean Expression - (value1==value2)
  • 18. Statements Roughly equivalent to sentences in natural languages. Forms a complete unit of execution. Types Expression Statements Declaration Statements Control Flow Statements
  • 19. Expression Statements Created by terminating expression with semicolon (;) Assignment Statement aValue = 8933.234; Increment Statement aValue++; Method Invocation Statement System.out.println("Hello World!"); Object Creation Statement Bicycle myBike = new Bicycle();
  • 20. Declaration and Control flow Statements Declaration Statements double aValue = 8933.234; Control flow Statements If-then and if-then-else Statements Switch Statements While and do-while Statements For Statements
  • 21. Block Statements A block is a group of zero or more statements between balanced braces Can be used anywhere a single statement is allowed.
  • 22. Block Statements class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } }
  • 24. AST Building Blocks AST can be transformed in one of the two ways Core AST APIs ASTBuilder
  • 26. Core AST APIs A S T No d e A n n o ta te d No d e G e n e r ic s T y p e M o d u le N o d e Sta te m e nt E x p r e s s io n
  • 27. Core AST APIs ASTNode Base class for any AST node. This class supports basic information used in all nodes of the AST Line and column number information. Meta data of a node A phase operation or transform can use this to transport arbitrary information to another phase operation or transform provided transform runs after the part storing the information.
  • 28. Core AST APIs - Expression Expression BooleanExpression CastExpression ConstantExpression ConstructorCallExpression FieldExpression MethodCallExpression StaticMethodCallExpression VariableExpression
  • 29. Core AST APIs - Expression this.println(message) can be represented as- new MethodCallExpression( new VariableExpression("this"), new ConstantExpression("println"), new ArgumentListExpression( new ConstantExpression(message) ) )
  • 30. Core AST API - Statement Statement BlockStatement SwitchStatement CaseStatement BreakStatement ExpressionStatement IfStatement TryCatchStatement WhileStatement ReturnStatement
  • 31. Core AST API - Statement this.println(message) can be represented as- return new ExpressionStatement( new MethodCallExpression( new VariableExpression("this"), new ConstantExpression("println"), new ArgumentListExpression( new ConstantExpression(message) ) ) )
  • 33. AST Builder Three ways to build AST via ASTBuilder Build From Strings Build From Code Build From a DSL-like Specification
  • 34. Building AST From String def builder = new AstBuilder() List<ASTNode> nodes = builder.buildFromString( CompilePhase.SEMANTIC_ANALYSIS, true, """ this.println('Hello World') """ )
  • 35. Building AST From String Phase parameter tells AST from which phase to return AST. "statementsOnly" boolean parameter tells the builder to discard the generated top level Script ClassNode. String paramter is the input The builder returns List<ASTNode>
  • 36. Building AST From String Produces following AST BlockStatement -> ExpressionStatement -> MethodCallExpression -> VariableExpression -> ConstantExpression -> ArgumentListExpression -> ConstantExpression
  • 37. Building AST From String Advantages Does not require author to understand ASTNode subtypes Allows author to target a CompilePhase Communicates source code being generated Robust - Should need no changes even if AST is updated in release
  • 38. Building AST From String Disadvantages IDE can not check syntax or grammar IDE can not refactor across String Some entities cannot be created, like the AST for a field declaration
  • 39. Building AST From Code def builder = new AstBuilder() List<ASTNode> nodes = builder.buildFromCode( CompilePhase.SEMANTIC_ANALYSIS, true){ println "Hello World" }
  • 40. Building AST From Code Produces following AST BlockStatement -> ExpressionStatement -> MethodCallExpression -> VariableExpression -> ConstantExpression -> ArgumentListExpression -> ConstantExpression
  • 41. Building AST From Code Advantages Clearly communicates source being generated Does not require author to understand ASTNode subtypes Allows author to target a CompilePhase Robust - Should need no changes even if AST is updated in a release IDE supports syntax checking and refactoring in Closure
  • 42. Building AST From Code Disadvantages Some entities cannot be created, like the AST for a field declaration
  • 43. Building AST From DSL-like Specification List<ASTNode> nodes = new AstBuilder().buildFromSpec{ block{ expression { methodCall { variable 'this' constant 'println' argumentList { if(locale == 'US') constant 'Hello' if(locale == 'FR') constant 'Bonjour' else constant "Ni hao" } } } } }
  • 44. Building AST From DSL-like Specification Advantages Allows conditionals (or any Groovy code) to be executed during the AST building process. Allows any ASTNode subtype to be created Fully documented with lengthy examples in TestCase
  • 45. Building AST From DSL-like Specification Disadvantages It can be difficult to determine what AST you need to write Verbose - does not always communicate the source being created Fragile - AST may need to change between major releases IDE does not yet provide code tips
  • 46. AST Transformation Two types of AST Transformations Local Transformations Global Transformations
  • 47. Local AST Transformations Can be applied on a per class basis. Can only be applied at semantic analysis or later phases Annotation Driven @Retention - Retention Policy should be SOURCE @Target – Can be Method, Field, Constructor ..//need to verify @GroovyASTTransformationClass
  • 48. Local AST Transformations Logging Example @Retention(RetentionPolicy.SOURCE) @Target([ElementType.METHOD]) @GroovyASTTransformationClass("LoggingASTTrans formation") @interface WithLogging { }
  • 49. Local AST Transformations @GroovyASTTransformation(phase=CompilePhase. SEMANTIC_ANALYSIS) class LoggingASTTransformation implements ASTTransformation{ @Override public void visit(ASTNode[] nodes, SourceUnit source) { ..... } }
  • 50. Local AST Transformations Usage class TestAst { @WithLogging void testMethod(){ println "This is test Method" } }
  • 51. Global AST Transformations Global transformations are applied to by the compiler on the code being compiled, wherever the transformation apply. A JAR should be added to the classpath of the compiler It should contain a service locator file at META- INF/services/org.codehaus.groovy.transform.ASTTr ansformation File should have a line with the name of the transformation class
  • 52. Global AST Transformations The transformation class must have a no-args constructor and implement the org.codehaus.groovy.transform.ASTTransformatio n interface It will be run against every source in the compilation
  • 54. Global AST Transformation Summary Write an ASTTransformation subclass Create a Jar metadata file containing the name of your ASTTransformation Create a Jar containing the class and metadata Invoke groovyc with that Jar on your classpath.
  • 56. @Delegate With @Delegate annotation, a class field becomes an object to which method calls are delegated. Example class Event{ @Delegate Date when String title, url }
  • 57. @Delegate Groovy compiler adds all of Date's methods to the Event class. Behind the scene methods simply delegate the call to Date field.
  • 58. @Singleton class T { private static volatile T instance private T() {} static T getInstance () { if (instance) { instance } else { synchronized(T) { if (instance) { instance } else { instance = new T () } } } } }
  • 59. @Singleton @Singleton class T { //properties... //methods.. }
  • 60. @Immutable No mutators (methods that modify internal state) Class must be final Fields must be private and final equals, hashCode and toString must be implemented in terms of the fields if you want to compare your objects or use them as keys in e.g. maps
  • 61. @Immutable Java code requires lot of boiler plate code. Groovy requires only annotation @Immuatable on class @Immutable final class Person { String first String last }
  • 62. @PackageScope class PackageScopeExample { @PackageScope String name }
  • 63. @Lazy class LazyExample { @Lazy pets = ['Cat', 'Dog', 'Bird'] static main(args){ def example = new LazyExample() println "Pets intialized : "+example.dump().contains('Cat') println "List size : "+example.pets.size() println "Pets intialized : "+example.dump().contains('Cat') } }
  • 64. @Newify Ruby Approach @Newify([Integer]) void test(){ println Integer.new(23) }
  • 65. @Newify Python Approach class Tree { def elements Tree(Object... elements) { this.elements = elements as List } } class Leaf { def value Leaf(value) { this.value = value } }
  • 66. @Newify def buildTree() { new Tree(new Tree(new Leaf(1), new Leaf(2)), new Leaf(3)) } @Newify([Tree, Leaf])def buildTree() { Tree(Tree(Leaf(1), Leaf(2)), Leaf(3)) }
  • 67. @Category interface Vehicle{ String getName() } @Category(Vehicle) class FlyingAbility{ def fly(){ "I am ${name} and I fly" } }
  • 68. @Mixin @Category(Vehicle) class DrivingAbility{ def drive(){ "I am ${name} and I drive" } } @Mixin([DrivingAbility, FlyingAbility]) class JamesBondVehicle implements Vehicle{ public String getName(){ "James Bond Vehicle" } }
  • 69. Where AST Transformations are used? Grails 2.0 GormTransformer ControllerActionTransformer ControllerTransformer GormToJpaTransformer Spock framework Grails Plugins Audit-Trail Plugin Rich Domain Classes Plugin Redis Plugin Neo4j Plugin