compile time and runtime metaprogramming with groovy, jeff brown

20
!"#$%&' )%#' *+, -.+/%#' 0'/1$2"321##%+3 4%/5 62""78 9':: ;2"<+ ='::>?2"<+@A$2%+3A".2B'>B"#

Upload: openblend-society

Post on 28-Nov-2014

1.942 views

Category:

Technology


0 download

DESCRIPTION

The dynamic nature of Groovy makes it a fantastic language for building dynamic applications for the Java Platform. The metaprogramming capabilities offered by the language provide everything that an application development team needs to build systems that are far more capable than their all Java counterparts. Taking advantage of Groovy's metaprogramming capabilities brings great new possibilities that would be very difficult or just plain impossible to write with Java alone. Building Domain Specific Languages in Groovy is easy to do once a team has a good understanding of the Metaobject-Protocol (MOP) and the method dispatch mechanisms used by the Groovy runtime environment. This session will cover in detail a number of advanced metaprogramming concepts in Groovy. The discussion will cover using dynamic method interception, meta-class manipulation, AST transformations and Groovy's Metaobject-Protocol (MP) to build flexible applications in Groovy including implementing a Domain Specific Language (DSL).

TRANSCRIPT

Page 1: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'()%#'(*+,(-.+/%#'(0'/1$2"321##%+3( (4%/5(62""78

9'::(;2"<+

='::>?2"<+@A$2%+3A".2B'>B"#

Page 2: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'(&$))*+(

! 451/(CA(C/DE ?'517%"2(B51+3'A(1/(2.+/%#'

E B1$1?%&%/%'A(12'(%+/2",.B',(1/(2.+/%#'

E 4%F%$',%1(A18A>>>

!"#$%&'()'%**+,)-+.-$/#-0'+$+,)-(1-2(*&3$#'-&'()'%*.-$/%$-0'+$#-('-*%,+&34%$#-($/#'-&'()'%*.-5('-$/#*.#46#.78889

Page 3: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'(&$))*+(,-+.,/&''01

! 62""78(G'+,A(C/A'&:(4'&&()"(0'/1$2"321##%+3E ,8+1#%B(#'/5",(,%A$1/B5

E #'/5",H$2"$'2/8(%+/'2B'$/%"+

E B2'1/'(#'/5",A(1/(2.+/%#'

E 9I0(A.$$"2/A(7'28(,8+1#%B(?'517%"2! #.B5(B1++"/(?'('1A%&8(/1$$',(?8(9171(/5'(&1+3.13'

! J8+1#%B(;'517%"2(C+(;.%&/KC+(62""78(!&1AA'AE LM$1+,"

E ;.%&,'2A

E '/B>>>

Page 4: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'(&$))*+(,-+.,/&''01

! N'B2'/A(O:()5'(01A/'2A(-'7'1&',E #'/5",(%+/'2B'$/%"+

E B&"A.2'(,'&'31/'A

E B.A/"#(?.%&,'2A

E B.A/"#(#'/1B&1AA

J.2%+3(/5%A(A'AA%"+(<'(<%&&(2'7%'<('1B5(<%/5('M1#$&'A(/"(1B/(1A(A'',A(:"2(8".2("<+(B2'1/%7%/8>((PKQ((

Page 5: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

23%$+.'

! J8+1#%B1&&8(LM$1+,%+3(O?='B/

!"#$%&'()*+!,$-$+".$'()*+!,/0

%&'()*+!,1#*2,345"6*+78*7"$-$9:3,,2&9

%&'()*+!,1*!!;8%<"3=$-$>$4?$@$AB$4$C$@$D

*=="35$9:3,,2&9$--$%&'()*+!,1#*2,345"6*+78*7"

*=="35$EFF$--$%&'()*+!,1*!!;8%<"3=/GF?$HF0

*=="35$%&'()*+!,1#,,$--$+8II

Page 6: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

23%$+.',4,5'6,7*.,89"1,7',89$#:

! N.2$2%A%+3&8(N%#$&'

! -'R.%2'A(*(N#1&&(;%/(O:(0'/5",(J%A$1/B5(S+"<&',3'E J<@"K5$7"5L3,)"35&/M534+7$)3,)"35&0

E 2,4!$="5L3,)"35&/M534+7$)3,)"35&?$J<@"K5$+".N*I8"0

E J<@"K5$%"5O,!P4==4+7/M534+7$+*%"?$J<@"K5$*37=0

Page 7: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

;<+#*)",!$%%*+(=

"()*+!,1#*2,345"6*+78*7"$-$9:3,,2&9

QQ$%*)=$5,111

"()*+!,1="5L3,)"35&/9#*2,345"6*+78*7"9?$9:3,,2&90

"()*+!,1#*2,345"6*+78*7"

QQ$%*)=$5,111

"()*+!,17"5L3,)"35&/9#*2,345"6*+78*7"90

"()*+!,1*!!;8%<"3=/RR?$GG0

QQ$%*)=$5,111

"()*+!,1%"5O,!P4==4+7/9*!!;8%<"3=9?$SRR?$GGT$*=$J<@"K5ST0

Page 8: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

><&,>6+,23%$+.'

4%/5()5"A'(T(N%#$&'(0'/5",A(4'(!1+(!2'1/'(O.2(O<+(I'2A%"+(O:(LM$1+,"(C+(T(0%+./'A>((

G'/UA(J"()51/(V"<>((G%7'(J'#">>>

Page 9: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

?@'=<&",7"@"($#"=

! !&"A.2'A(018(;'(*AA%3+',(*(WJ'&'31/'X

! !&"A.2'A(-'&18(0'/5",(!1&&A()"()5'%2(J'&'31/'

! YA':.&(Z"2(!2'1/%+3(JNGA(*+,(;.%&,'2A

! (N1#'(!&"A.2'(018(;'(LM'B./',(C+(J%::'2'+/(!"+/'M/A(4%/5(J%::'2'+/(J'&'31/'A

Page 10: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

?@'=<&",7"@"($#"=

!"#$%&UI,=83"$-$>

$$$$*))"+!/9V43=5$64+"W+90

$$$$*))"+!/96*=5$64+"W+90

D

QQ$.4II$#*4I$<"K*8="$5O"3"$4=$+,$9*))"+!9$%"5O,!

%&UI,=83"/0

!"#$<8##"3$-$+".$M534+7X8##"3/0

!"#$%&UI,=83"$-$>

$$$$*))"+!/9V43=5$64+"W+90

$$$$*))"+!/96*=5$64+"W+90

D

%&UI,=83"1!"I"7*5"$-$<8##"3

QQ$.4II$*))"+!$5,$5O"$M534+7X8##"3

%&UI,=83"/0

Page 11: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

?@'=<&",7"@"($#"=

KI*==$Y="3$>

$$$$M534+7$I,74+

$$$$M534+7$)*==.,3!

$$$$M534+7$"%*4I

$$$$Z*5"$*7"

$$$$=5*54K$K,+=53*4+5=$-$>

$$$$$$$$I,74+/I"+75O[\11E\?<I*+][#*I="?8+4^8"[538"0

$$$$$$$$)*==.,3!/I"+75O[\11E\?<I*+][#*I="0

$$$$$$$$"%*4I/"%*4I[538"?<I*+][#*I="0

$$$$$$$$*7"/%4+[+".$Z*5"/0?+8II*<I"[#*I="0

$$$$D

D

Page 12: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!$&A<%B<*@."&

!"#$%])$-$+".$73,,2&1(%I1P*3]8)X84I!"3/0

%])1!"#$$>

$$$$!%&'$>

$$$$$$$$"("$%/9P&$P*3]8)X84I!"3$_"=590

$$$$D

$$$$)*'+$>

$$$$$$$$!,/9P&$V*2,345"$M45"=90

$$$$$$$$-$$>

$$$$$$$$$$$$$(/973,,2&1K,!"O*8=1,3790

$$$$$$$$$$$$$(/973*4I=1,3790

$$$$$$$$$$$$$(/9@*2*@"##1<I,7=),51K,%90

$$$$$$$$D

$$$$D

D -'#'#?'2(/51/(/5%A(%A('M'B./1?&'(B",'>( (*&&(":(/5"A'(/13(+1#'A(12'(#'/5",(B1&&A(/51/(12'(?'%+3(%+/'2B'$/',(?8(/5'(?.%&,'2>(

Page 13: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!$&A<%B<*@."&

`O5%IB

$$`O"*!B

$$$$`545I"BP&$P*3]8)X84I!"3$_"=5`Q545I"B

$$`QO"*!B

$$`<,!&B

$$$$`OaBP&$V*2,345"$M45"=`QOaB

$$$$`8IB

$$$$$$`I4B73,,2&1K,!"O*8=1,37`QI4B

$$$$$$`I4B73*4I=1,37`QI4B

$$$$$$`I4B@*2*@"##1<I,7=),51K,%`QI4B

$$$$`Q8IB

$$`Q<,!&B

`QO5%IB

Page 14: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

23%$+.'!"#$?@$==

! LM$1+,"0'/1!&1AA(*,,',(*:/'2([>\E B1#'(:2"#(621%&A

E 32'1/&8(A%#$&%:%'A(1,,%+3(#'/5",A(/"('M%A/%+3(B&1AA'A

M534+71%"5*UI*==12,."I=$-$>

$$$$!"I"7*5"1#4+!bII$>$

$$$$$$$$4515,6,."3U*="/01%*5KO"=/9S*"4,8T90$

$$$$D

D

!"#$%"==*7"$-$9:3,,2&$c=$b$:3"*5$6*+78*7"9

)34+5I+$%"==*7"12,."I=/0

QQ$Sd,d?$d,d?$dcd?$dbd?$d"d?$d*d?$d*d?$d8d?$d*d?$d"dT

Page 15: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

7')$*+,?@$==,C!$(*DC,E+,/&$*@=

! J8+1#%B(J"#1%+(!&1AA(0'/5",AE %&L"3=,+1=*2"/0

E %&L"3=,+12*I4!*5"/0

E L"3=,+1K,8+5/0

E L"3=,+1#4+!X&6*=5;*%"/9X3,.+90

E L"3=,+1#4+!X&b7":3"*5"3_O*+/EG0

E L"3=,+1#4+!X&X435O!*&X"5.""+/#43=5Z*5"?$$ $

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$="K,+!Z*5"0

Page 16: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'()%#'(*'+,$-".-,##%/.

! !"##$%&'())#"*+&,#-)./0&1.-0&20*3)"#4"3--.546 $.3&7'1&1"35+8#"-3*.#5+&

! 9:--(*3;/0

! 9<0/043*0

! 9'.54/0*#5

! 0*=>>>

Page 17: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$#%&'#

!"#$$%&'()"*)+%,

%%%%-.)")/#0)%&#*%1)$0)2&#*%3%456

7

2)8%9:%3%1);%&'()"*)+<=

#$$)+0%9:%>1$0#1!)?8%&#*

Page 18: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

!"#$%&'()$

! !"#$%&'(#)*+,%&-./ 0()1,'2'(3-

/ 4"#2%)5%2--

/ 523&6,%)72#8%"#$)96)1,'2:%&)!'2'&

/ &;,2%-<)=2-=5(8&<)'(!'3"#$

!"#$%&'()$*+&,--*.*/0

Page 19: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

FE))<#$G@"

ec%%85*<I"$#4+*I$KI*==$U,,3!4+*5"=$>

$$$$Z,8<I"$I*5458!"?$I,+7458!"

D

!"#$KE$-$+".$U,,3!4+*5"=/I*5458!"[$Hf1faHFGf?

$$$$$$$$$$$$$$$$$$$$$$$$$I,+7458!"[$a1\REgRR0

!"#$Ka$-$+".$U,,3!4+*5"=/Hf1faHFGf?$a1\REgRR0

*=="35$KE$--$Ka

Page 20: Compile Time and Runtime Metaprogramming With Groovy, Jeff Brown

89$+A,H'<,I'&,?')*+(

](^(*