Your SlideShare is downloading. ×
0
Introduction to Groovy runtime
metaprogramming and AST
transforms
by Marcin Grzejszczak
TooMuchCoding blog
Question
How to calculate ten times a number?

Integer getTimesTenOf(Integer number) {
return 10 * number;
}
assert 40 == ...
Let’s invert the concept!
assert 40 == 4.tens
It can’t be done… Can it?
public final class Integer ... {
// ...
}
Add that method!
Integer.metaClass.getTimesTens = {
return 10 * delegate
}
assert 40 == 4.timesTen
Monkey patch
● Extend or modify the run-time code
● Do not alter the original source code
● Also known as duck punching an...
Classes with Groovy
public class Foo implements groovy.lang.GroovyObject
extends java.lang.Object {

}
GroovyObject
public interface GroovyObject {

Object invokeMethod(String name, Object args);

Object getProperty(String pr...
Meta Object Protocol

●
●
●
●

MetaObject is an object that manipulates, creates, describes, or
implements other objects (...
MetaClass

MetaClass defines the behaviour of any given Groovy or Java class ●

●
●

The MetaClass interface has two parts...
Expando
●
●

●

Kind of a dynamic bean.
It will memorize
○ added properties
○ added methods (from closures)
Useful for
○ m...
Expando
def player = new Expando(name: 'Robert')
assert 'Robert' == player.name

player.surname = 'Lewandowski'
assert 'Le...
ExpandoMetaClass
A special implementation of a MetaClass that allows you to
(using closures):
● dynamically add methods,
●...
ExpandoMetaClass
●

ExpandoMetaClass - Borrowing Methods — Borrowing methods from other classes

●

ExpandoMetaClass - Con...
Add that method! - reminder
Integer.metaClass.getTimesTen = {
return 10 * delegate
}
assert 40 == 4.timesTen
Add that method! - watch out!
Integer.metaClass.getTimesTens = {
return 10 * delegate
}
Remember that :
● You are mixing h...
MOP - find that missing method!

Taken from Venkat's
Programming Groovy 2
Metaprogramming in Groovy
● Runtime
○ Categories
○ Expando / MetaClass / ExpandoMetaClass
● Compile Time
○ AST Transformat...
Runtime - drawbacks
● efficiency - finding if method exists
● method / property missing - people start
asking questions ho...
Abstract Syntax Tree
● Representation of the abstract syntactic structure of
source code
● Each node of the tree denotes a...
An abstract syntax tree for the following
code for the Euclidean algorithm:

while b ≠ 0
if a > b
a=a−b
else
b=b−a
return ...
AST Transformation
● Compile-time metaprogramming
● Bytecode manipulation
● Either global or local
○ Global in any compile...
Did you know that…?
Groovy compiler has 9 phases?
● Initialization
●

Parsing

●

Conversion

●

Semantic Analysis (import...
AST Transformation annotations
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●

@AnnotationCollector
@Externalize
@BaseScript
@Canonical...
TooMuchCoding Git Github repository
TooMuchCoding Mercurial Bitbucket repository
Local AST transforms for dummies
●
●
●
●

Check the Guidebook
Define an annotation
Create an AST transform for the annotat...
AST transforms - not that easy :(
● Creating implementation is not trivial
○ Verify node
○ Verify preconditions
○ Make the...
Sources and recommended readings
●
●
●
●
●

Wikipedia (MonkeyPatch, MOP)
http://groovy.codehaus.org/
Groovy AST Demisitifi...
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
Introduction to Groovy runtime metaprogramming and AST transforms
Upcoming SlideShare
Loading in...5
×

Introduction to Groovy runtime metaprogramming and AST transforms

1,526

Published on

Introduction to Groovy runtime metaprogramming and AST transforms - by Marcin Grzejszczak author of the http://toomuchcoding.blogspot.com blog.

The sources can be found here
Mercurial Bitbucket - https://bitbucket.org/gregorin1987/too-much-coding/src/e5ab7c69ab7b2796075fd6f087fbf31346aa2d2b/Groovy/ast/?at=default

Git Github - https://github.com/marcingrzejszczak/too-much-coding/tree/master/Groovy/ast

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,526
On Slideshare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to Groovy runtime metaprogramming and AST transforms"

  1. 1. Introduction to Groovy runtime metaprogramming and AST transforms by Marcin Grzejszczak TooMuchCoding blog
  2. 2. Question How to calculate ten times a number? Integer getTimesTenOf(Integer number) { return 10 * number; } assert 40 == getTimesTenOf(4)
  3. 3. Let’s invert the concept! assert 40 == 4.tens
  4. 4. It can’t be done… Can it? public final class Integer ... { // ... }
  5. 5. Add that method! Integer.metaClass.getTimesTens = { return 10 * delegate } assert 40 == 4.timesTen
  6. 6. Monkey patch ● Extend or modify the run-time code ● Do not alter the original source code ● Also known as duck punching and shaking the bag
  7. 7. Classes with Groovy public class Foo implements groovy.lang.GroovyObject extends java.lang.Object { }
  8. 8. GroovyObject public interface GroovyObject { Object invokeMethod(String name, Object args); Object getProperty(String propertyName); void setProperty(String propertyName, Object newValue); MetaClass getMetaClass(); void setMetaClass(MetaClass metaClass); }
  9. 9. Meta Object Protocol ● ● ● ● MetaObject is an object that manipulates, creates, describes, or implements other objects (including itself). Stored info includes base object's type, interface, class, methods etc. MetaObjects are examples of reflection concept The Meta-Object-Protocol (MOP) is the collection of rules of how a request for a method call is handled by the Groovy runtime system
  10. 10. MetaClass MetaClass defines the behaviour of any given Groovy or Java class ● ● ● The MetaClass interface has two parts: ○ Client API - via the extend MetaObjectProtocol interface ○ Contract with the Groovy runtime system. In general the compiler and Groovy runtime engine interact with methods on MetaClass class In general the MetaClass clients interact with the method defined by the MetaObjectProtocol interface
  11. 11. Expando ● ● ● Kind of a dynamic bean. It will memorize ○ added properties ○ added methods (from closures) Useful for ○ mocking ○ when you don’t want to create a new class (you just want to record behaviour)
  12. 12. Expando def player = new Expando(name: 'Robert') assert 'Robert' == player.name player.surname = 'Lewandowski' assert 'Lewandowski' == player.surname player.presentYourself = { return "Name: $name, Surname: $surname" } String result = player .presentYourself() assert 'Name: Robert, Surname: Lewandowski' == result
  13. 13. ExpandoMetaClass A special implementation of a MetaClass that allows you to (using closures): ● dynamically add methods, ● constructors, ● properties and static methods
  14. 14. ExpandoMetaClass ● ExpandoMetaClass - Borrowing Methods — Borrowing methods from other classes ● ExpandoMetaClass - Constructors — Adding or overriding constructors ● ExpandoMetaClass Domain-Specific Language ● ExpandoMetaClass - Dynamic Method Names — Dynamically creating method names ● ExpandoMetaClass - GroovyObject Methods — Overriding invokeMethod, getProperty and setProperty ● ExpandoMetaClass - Interfaces — Adding methods on interfaces ● ExpandoMetaClass - Methods — Adding or overriding instance methods ● ExpandoMetaClass - Overriding static invokeMethod — Overriding invokeMethod for static methods ● ExpandoMetaClass - Properties — Adding or overriding properties ● ExpandoMetaClass - Runtime Discovery — Overriding invokeMethod for static methods ● ExpandoMetaClass - Static Methods — Adding or overriding static methods
  15. 15. Add that method! - reminder Integer.metaClass.getTimesTen = { return 10 * delegate } assert 40 == 4.timesTen
  16. 16. Add that method! - watch out! Integer.metaClass.getTimesTens = { return 10 * delegate } Remember that : ● You are mixing here a method to a class not to an object ● In the same JVM all objects of that class will have that method added to MetaClass! (You can image the downsides of this) ● It’s safer to use categories - only a block of code will have those methods mixed in
  17. 17. MOP - find that missing method! Taken from Venkat's Programming Groovy 2
  18. 18. Metaprogramming in Groovy ● Runtime ○ Categories ○ Expando / MetaClass / ExpandoMetaClass ● Compile Time ○ AST Transformations ○ Extension Module
  19. 19. Runtime - drawbacks ● efficiency - finding if method exists ● method / property missing - people start asking questions how can that even compile ● no IDE support
  20. 20. Abstract Syntax Tree ● Representation of the abstract syntactic structure of source code ● Each node of the tree denotes a construct occurring in the source code. ● The syntax is "abstract" in not representing every detail appearing in the real syntax.
  21. 21. An abstract syntax tree for the following code for the Euclidean algorithm: while b ≠ 0 if a > b a=a−b else b=b−a return a
  22. 22. AST Transformation ● Compile-time metaprogramming ● Bytecode manipulation ● Either global or local ○ Global in any compiler phase ○ Local in a semantic analysis phase or later
  23. 23. Did you know that…? Groovy compiler has 9 phases? ● Initialization ● Parsing ● Conversion ● Semantic Analysis (important for Local AST transforms) ● Canonicalization ● Instruction Selection ● Class Generation ● Output ● Finalization
  24. 24. AST Transformation annotations ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● @AnnotationCollector @Externalize @BaseScript @Canonical @CompileStatic @TypeChecked @ConditionalInterrupt @EqualsAndHashcode @Mixin @Category @Immutable @Field @IndexedProperty @InheritConstructors @Memoized @PackageScope @Synchronized ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● @ThreadInterrupt @TimedInterrupt @ToString @TupleConstructor @WithReadLock @WithWriteLock @Delegate @DelegatesTo @Lazy @Newify @Singleton @ASTTest @AutoClone @Commons @Log @Slf4j @NotYetImplemented @Grab @GrabResolver
  25. 25. TooMuchCoding Git Github repository TooMuchCoding Mercurial Bitbucket repository
  26. 26. Local AST transforms for dummies ● ● ● ● Check the Guidebook Define an annotation Create an AST transform for the annotation Build the transform
  27. 27. AST transforms - not that easy :( ● Creating implementation is not trivial ○ Verify node ○ Verify preconditions ○ Make the implementation ● Use tools ○ AstBuilder from string ○ AstBuilder from code ○ AstBuilder from spec ● The code might get really messy
  28. 28. Sources and recommended readings ● ● ● ● ● Wikipedia (MonkeyPatch, MOP) http://groovy.codehaus.org/ Groovy AST Demisitified Groovy AST tutorials Project Lombok for Java
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×