aspectj - days2011.scala-lang.orgdays2011.scala-lang.org/sites/days2011/files/52. aspectj.pdf ·...
TRANSCRIPT
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
SCAL
A A
ND
ASP
ECTJ
A
ppro
achi
ng M
odul
ariz
ing
of C
ross
cutti
ng
Con
cern
s R
amni
vas
Ladd
ad
ram
niva
s ra
mni
vas!
com
@
ram
niva
s
@ra
mni
vas
• S
prin
g fra
mew
ork
com
mitt
er
• C
loud
Fou
ndry
com
mitt
er
• M
ain
inte
rest
s
• A
spec
t-orie
nted
pro
gram
min
g •
Clo
ud c
ompu
ting
• S
cala
and
func
tiona
l pro
gram
min
g
• A
utho
r of b
ooks
and
arti
cles
•
Asp
ectJ
in A
ctio
n (1
st a
nd 2
nd e
ditio
n)
• S
peak
er a
t man
y pr
ofes
sion
al c
onfe
renc
es
• Ja
vaO
ne, J
avaP
olis
, Spr
ingO
ne, S
oftw
are
Dev
elop
men
t, N
o Fl
uff J
ust S
tuff,
Ecl
ipse
Con
, O’R
eilly
OS
CO
N e
tc.
• A
ctiv
e in
volv
emen
t in
Asp
ectJ
sin
ce it
s ea
rly fo
rm
Aspe
ctJ
and
Scal
a co
nnec
tion
Bot
h st
atic
ally
type
d
Aspe
ctJ
and
Scal
a co
nnec
tion
Bot
h co
mpi
led
to
JVM
-com
patib
le b
yte
code
Aspe
ctJ
and
Scal
a co
nnec
tion
Asp
ectJ
adv
ice
sh
ares
som
e ch
arac
teris
tics
of
high
er o
rder
func
tions
Aspe
ctJ
and
Scal
a co
nnec
tion
Asp
ectJ
sta
tic c
ross
cutti
ng
shar
es s
ome
char
acte
ristic
s of
tra
its
Aspe
ctJ
and
Scal
a co
nnec
tion
Asp
ectJ
wea
ving
use
d in
Sca
la ID
E
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
A Q
UIC
K IN
TRO
TO
AO
P
Cro
sscu
tting
con
cern
•
Func
tiona
lity
who
se im
plem
enta
tion
span
s m
ultip
le
mod
ules
•
Man
y ex
ampl
es
• Lo
ggin
g an
d tra
cing
•
Tran
sact
ion
man
agem
ent
• C
oncu
rren
cy c
ontro
l •
Sec
urity
•
Err
or h
andl
ing
• B
usin
ess
rule
s •
Aud
iting
•
Per
form
ance
mon
itorin
g • !
Dea
ling
with
cro
sscu
tting
con
cern
s • A
OP
• A
prog
ram
min
g m
etho
dolo
gy
• A
spec
tJ
• A
prom
inen
t lan
guag
e to
sup
port
AO
P fo
r Jav
a
Cor
e AO
P co
ncep
ts
• Joi
n po
int
• A
n id
entif
iabl
e po
int i
n th
e ex
ecut
ion
of a
pro
gram
. •
Cen
tral,
dist
ingu
ishi
ng c
once
pt in
AO
P • P
oint
cut
• P
rogr
am c
onst
ruct
that
sel
ects
join
poi
nts
and
colle
cts
cont
ext a
t tho
se p
oint
s.
• Adv
ice
• C
ode
to b
e ex
ecut
ed a
t a jo
in p
oint
that
has
bee
n se
lect
ed b
y a
poin
tcut
• I
nter
-type
dec
lara
tion
• A
dditi
onal
dat
a or
met
hod
to e
xist
ing
type
s,
impl
emen
ting
new
inte
rface
s
Aspe
ctJ
wea
ving
Java
and
asp
ect
sour
ce fi
les
(.jav
a/.a
j)
Java
and
asp
ect
byte
-cod
e fil
es
(.jar
/.cla
ss)
Wea
ver
Wov
en s
yste
m
(.cla
ss fi
les
or .j
ar fi
le)
Com
pile
r, lin
ker,
or lo
ad-ti
me
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
ASPE
CTJ
IN P
RAC
TIC
E Le
sson
s le
arne
d ov
er y
ears
Aspe
cts
com
e in
all s
izes
Loca
lly
scop
ed
conc
erns
Cro
ss-u
nit
conc
erns
Wid
ely
appl
ied
conc
erns
Cac
hing
, re
ad-w
rite
lock
, re
try fa
ilure
s
Tran
sact
ion,
se
curit
y M
onito
ring,
aud
iting
, po
licy
enfo
rcem
ent,
conc
urre
ncy
cont
rol*
AOP
and
anno
tatio
ns
• A
nnot
atio
n us
e ve
ry p
erva
sive
"
Ann
otat
ions
as
mar
ker f
or “a
dvic
e he
re”
• O
bliv
ious
ness
not
con
side
red
criti
cal
" E
xplic
it an
nota
tions
not
frow
ned
upon
Perv
asiv
e an
nota
tions
Obje
ct a
roun
d()
: @a
nnot
atio
n(Re
try)
{
}
@R
etry
publ
ic c
lass
Ord
erSe
rvic
e {
@R
etry
publ
ic v
oid
proc
ess(
) ..
.
}
Scal
a w
ay o
f add
ress
ing
cros
scut
ting
func
tiona
lity
• Im
plem
ent h
ighe
r ord
er fu
nctio
n en
caps
ulat
ing
the
cros
scut
ting
logi
c • A
pply
the
func
tion
to a
ll pl
aces
that
nee
d th
e fu
nctio
nalit
y
Hig
her o
rder
func
tions
def
proc
ess(
) :
Unit
= {
re
try(
5) {
...
}
}
def
retr
y[T]
(max
Retr
y: I
nt)(
thun
k: =
> T)
= {
de
f re
try(
thun
k: =
> T,
att
empt
: In
t):
T =
{
tr
y {
th
unk
} ca
tch
{
case
ex
if (
atte
mpt
< ma
xRet
ry)
=>
retr
y(th
unk,
att
empt
+ 1
)
}
}
}
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
LOC
ALLY
APP
LIED
FU
NC
TIO
NAL
ITY
Ext
erna
l lan
guag
e
Cac
hing
usi
ng A
spec
tJ
@Cac
habl
e(ca
cheS
tore
="ch
art"
,
ke
yScr
ipt=
"#st
ock.
tick
er +
':'
+ #
days
")
publ
ic b
yte[
] ge
tQuo
teGr
aph(
Stoc
k st
ock,
in
t da
ys)
{
..
. Ex
pens
ive
calc
ulat
ion
}
Cac
hing
asp
ect
publ
ic a
spec
t Ca
cheA
spec
t {
pu
blic
poi
ntcu
t ca
ched
(Cac
habl
e ca
chab
le)
: ex
ecut
ion(
@Cac
habl
e *
*(..
)) &
& @a
nnot
atio
n(ca
chab
le);
Ob
ject
aro
und(
Cach
able
cac
habl
e) :
cac
hed(
cach
able
) {
Stri
ng k
ey =
eva
luat
e(ca
chab
le.k
eySc
ript
(),
th
isJo
inPo
int)
;
if
(ca
che.
cont
ains
Key(
key)
) {
re
turn
cac
he.g
et(k
ey)
} el
se {
Ob
ject
val
ue =
pro
ceed
(cac
hebl
e);
ca
che.
put(
key,
val
ue);
re
turn
val
ue;
}
}}
Cac
hing
usi
ng S
cala
de
f ge
tQuo
teGr
aph(
stoc
k: S
tock
,
da
ys:
Int)
: A
rray
[Byt
e] =
{
ca
ched
("ch
art"
, st
ock.
tick
er +
":"
+ d
ays)
{
! ... Expensive calculation !
! }
}
Cac
hing
hig
her-o
rder
func
tion
abst
ract
cla
ss C
achi
ng(v
al c
ache
Mana
ger:
Cac
heMa
nage
r) {
de
f ca
ched
[T](
regi
on:
Stri
ng,
key:
Any
)
(th
unk:
=>
T):
T =
{
va
l ca
che
= ..
.
if
(ca
che.
cont
ains
Key(
key)
) {
ca
che.
get(
key)
.asI
nsta
nceO
f[T]
} el
se {
va
l th
unkV
al:
T =
thun
k
cach
e.pu
t(ke
y, t
hunk
Val)
th
unkV
al
}
}}
Com
parin
g ca
chin
g so
lutio
ns
Asp
ectJ
Sc
ala
Eve
ry c
ache
d el
emen
t nee
ds a
n an
nota
tion
Eve
ry c
ache
d el
emen
t nee
ds
wra
ppin
g in
the
HO
F
Use
s ex
tern
al
lang
uage
(d
ynam
ical
ly ty
ped)
Use
the
nativ
e la
ngua
ge
Thou
ght!
Is re
try o
r cac
hing
eve
n a
crosscutting
func
tiona
lity?
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
CR
OSS
-UN
IT
CR
OSS
CU
TTIN
G
FUN
CTI
ON
ALIT
Y
Anno
tatin
g fo
r tra
nsac
tion
man
agem
ent
publ
ic c
lass
Ord
erSe
rvic
eImp
l
impl
emen
ts O
rder
Serv
ice
{
..
.
@Tra
nsac
tion
al(r
eadO
nly=
true
)
pu
blic
Ord
er f
indO
rder
(Lon
g or
derI
d) {
..
.
}
@Tra
nsac
tion
al
publ
ic v
oid
upda
teOr
der(
Orde
r or
der)
{
..
.
}
@Tra
nsac
tion
al
publ
ic v
oid
addP
rodu
ct(O
rder
ord
er,
Prod
uct
prod
uct,
int
qua
ntit
y) {
..
.
}
@Tra
nsac
tion
al
publ
ic v
oid
remo
vePr
oduc
t(Or
der
orde
r, P
rodu
ct p
rodu
ct,
int
quan
tity
) {
..
.
}
@Tra
nsac
tion
al
publ
ic v
oid
canc
elOr
der(
Orde
r or
der)
{
..
.
}
...
}
Tran
sact
ion
man
agem
ent w
ith S
cala
cl
ass
Orde
rSer
vice
Impl
ext
ends
Ord
erSe
rvic
e {
de
f fi
ndOr
der(
orde
rId:
Lon
g) :
Ord
er =
{
tran
sact
iona
l(re
adOn
ly=t
rue)
{
..
.
}
}
de
f up
date
Orde
r(or
der:
Ord
er)
{
tr
ansa
ctio
nal(
) {
..
.
}
}
de
f ad
dPro
duct
(ord
er:
Orde
r, p
rodu
ct:
Prod
uct,
qua
ntit
y: I
nt)
{
tr
ansa
ctio
nal(
) {
..
.
}
}
de
f re
move
Prod
uct(
orde
r: O
rder
, pr
oduc
t: P
rodu
ct,
quan
tity
: In
t) {
tran
sact
iona
l()
{
...
}
}
def
canc
elOr
der(
orde
r: O
rder
) {
tran
sact
iona
l()
{
...
}
}
...
}
Tran
sact
ion
man
agem
ent a
spec
t pu
blic
asp
ect
Tran
sact
ionM
anag
emen
tAsp
ect
{
priv
ate
Tran
sact
ionM
anag
er t
xMan
ager
;
poin
tcut
tra
nsac
tion
alOp
() :
...
Obje
ct a
roun
d()
: tr
ansa
ctio
nalO
p()
{
Tran
sact
ionA
ttri
bute
ta
= ..
.
Tran
sact
ionS
tatu
s ts
= t
xMan
ager
.get
Tran
sact
ion(
ta);
tr
y {
Obje
ct r
et =
pro
ceed
();
tran
sact
ionM
anag
er.c
ommi
t(ts
);
retu
rn r
et;
} ca
tch
(Thr
owab
le e
x) {
.
.. r
ollb
ack
thro
w ex
;
}
}
Tran
sact
ion
man
agem
ent f
unct
ion
def
tran
sact
iona
l[T]
(pro
pgat
ion:
Pro
paga
tion
= P
ropa
gati
on.R
EQUI
RED,
iso
lati
on:
Isol
atio
n =
Isol
atio
n.DE
FAUL
T,
rea
dOnl
y: B
oole
an =
fal
se,
tim
eout
: In
t =T
rans
acti
onDe
fini
tion
.TIM
EOUT
_DEF
AULT
,
r
ollb
ackF
or:
List
[Thr
owab
le]
= Li
st()
,
n
oRol
lbac
kFor
: Li
st[T
hrow
able
] =
List
())
(th
unk:
=>
T) :
T
Tran
sact
ion
man
agem
ent i
mpl
emen
tatio
n ab
stra
ct c
lass
Tra
nsac
tion
Mana
geme
nt(v
al t
xMan
ager
: Pl
atfo
rmTr
ansa
ctio
nMan
ager
) {
de
f tr
ansa
ctio
nal[
T](.
..)(
thun
k: =
> T)
: T
= {
val
txAt
trib
ute
= ne
w Tr
ansa
ctio
nAtt
ribu
teWi
thRo
llba
ckRu
les(
...)
val
stat
us =
txM
anag
er.g
etTr
ansa
ctio
n(tx
Attr
ibut
e)
try
{
val
ret
= th
unk
tx
Mana
ger.
comm
it(s
tatu
s)
re
t
}
catc
h {
ca
se e
x =>
{
if (
txAt
trib
ute.
roll
back
On(e
x))
{
txMa
nage
r.ro
llba
ck(s
tatu
s)
} el
se {
tx
Mana
ger.
comm
it(s
tatu
s)
}
th
row
ex
}
}
}}
But!
•
Tran
sact
ion
man
agem
ent o
ften
appl
ies
to a
ll m
etho
ds o
f a
clas
s •
Ofte
n w
ith g
ener
ic s
elec
tion:
pub
lic m
etho
ds o
nly
" C
lass
-leve
l ann
otat
ion
suffi
ces
• Tr
ansa
ctio
n m
anag
emen
t ofte
n ap
plie
s to
all
clas
ses
of
certa
in k
ind
decl
are
@typ
e: @
Serv
ice
* :
@Tra
nsac
tion
al;
• S
peci
al a
ttrib
utes
can
be
deriv
ed u
sing
con
vent
ion
over
co
nfig
urat
ion
de
clar
e @m
etho
d: *
(@T
rans
acti
onal
*).
find
*(..
)
: @T
rans
acti
onal
(rea
dOnl
y=tr
ue);
Cla
ss-le
vel a
nnoa
tion
@Tra
nsac
tion
al
publ
ic c
lass
Ord
erSe
rvic
eImp
l
impl
emen
ts O
rder
Serv
ice
{
..
.
@Tra
nsac
tion
al(r
eadO
nly=
true
)
pu
blic
Ord
er f
indO
rder
(Lon
g or
derI
d) {
...
}
publ
ic v
oid
upda
teOr
der(
Orde
r or
der)
{
..
.
}
pu
blic
voi
d ad
dPro
duct
(Ord
er o
rder
, Pr
oduc
t pr
oduc
t, i
nt q
uant
ity)
{
..
.
}
pu
blic
voi
d re
move
Prod
uct(
Orde
r or
der,
Pro
duct
pro
duct
, in
t qu
anti
ty)
{
...
}
pu
blic
voi
d ca
ncel
Orde
r(Or
der
orde
r) {
...
}
..
.
}
How
ever!
• S
cala
can
app
ly tr
ansa
ctio
n m
anag
emen
t to
smal
ler u
nit
than
the
who
le m
etho
d •
Littl
e pr
actic
al v
alue
, sin
ce re
fact
orin
g to
ext
ract
met
hod
that
nee
d tra
nsac
tion
is a
goo
d id
ea a
nyw
ay
Com
parin
g tra
nsac
tion
man
agem
ent
solu
tions!
A
spec
tJ
Scal
a
Not
eve
ry e
lem
ent
need
s a
mar
ker
(ann
otat
ion)
Eve
ry tr
ansa
ctio
nal
logi
c w
rapp
ed in
HO
F
Met
hod-
leve
l gr
anul
arity
A
ny d
esire
d gr
anul
arity
pos
sibl
e
Cop
yrig
ht ©
Ram
niva
s La
ddad
. All
right
s re
serv
ed.
WID
ELY
APPL
IED
C
RO
SSC
UTT
ING
Cla
ssic
cro
sscu
tting
con
cern
s •
Mon
itorin
g •
Trac
ing
• P
rofil
ing
• Fi
rst f
ailu
re d
ata
capt
ure
• P
olic
y en
forc
emen
t •
Con
curr
ency
con
trol
• S
win
g, S
WT,
And
roid
• A
uditi
ng
FFD
C: B
ase
aspe
ct
publ
ic a
bstr
act
aspe
ct F
FDC
{
publ
ic a
bstr
act
poin
tcut
ffd
cOp(
);
af
ter(
) th
rowi
ng(E
xcep
tion
ex)
: f
fdcO
p()
{
Si
gnat
ure
sig
= th
isJo
inPo
intS
tati
cPar
t.ge
tSig
natu
re()
;
lo
gFFD
C(si
g.ge
tDec
lari
ngTy
pe()
.get
Name
(),
sig.
getN
ame(
), e
x,
this
Join
Poin
t.ge
tThi
s(),
thi
sJoi
nPoi
nt.g
etAr
gs()
);
}
publ
ic v
oid
logF
FDC(
Stri
ng c
lass
Name
, St
ring
met
hodN
ame,
Exce
ptio
n ex
, Ob
ject
thi
z, O
bjec
t[]
args
) {
...
}
}
FFD
C: D
eriv
ed a
spec
t pu
blic
asp
ect
DaoF
FDC
exte
nds
FFDC
{
pu
blic
poi
ntcu
t ff
dcOp
()
: ex
ecut
ion(
* co
m.ac
me..
dao.
*.*(
..))
; } No
anno
tatio
n or
oth
er m
arke
r in
advi
sed
code
FFD
C th
roug
h Sc
ala
• P
ossi
ble
thro
ugh
high
er-o
rder
func
tion
• B
ut!
•
Tedi
ous
to w
rap
ever
y fu
nctio
n •
Ther
e ar
e ju
st to
o m
any
of th
ose
• E
rror
pro
ne
• M
isse
d fu
nctio
ns d
imin
ishi
ng v
alue
in d
ata
capt
ured
•
Har
d to
reas
on a
t sys
tem
leve
l
All
clas
sic
cros
scut
ting
sym
ptom
s
Solu
tion
for S
cala
pro
gram
s:
Use
Asp
ectJ
• Com
pile
Sca
la p
rogr
am a
s us
ual
• Wea
ve a
spec
ts u
sing
bin
ary
wea
ving
(b
uild
-tim
e or
load
-tim
e)
Con
clus
ion
• W
ith e
very
ele
men
t car
ryin
g a
mar
ker a
nnot
atio
n, h
ighe
r-or
der f
unct
ion
offe
r a s
impl
er a
nd p
ower
ful s
olut
ion
• O
ften
not r
eally
cro
sscu
tting
con
cern
s to
beg
in w
ith
• A
nnot
atio
n us
ed a
s a
crut
ch to
hav
e as
pect
s ap
ply
HO
F
• C
ross
-uni
t cro
sscu
tting
func
tiona
lity
• A
spec
ts h
ave
an e
dge
• N
ot a
ll lo
st b
y st
ayin
g w
ith S
cala
-onl
y
• W
ide
cros
scut
ting
conc
erns
•
Asp
ectJ
is o
nly
gam
e in
tow
n
You
can
appl
y as
pect
s to
Sca
la-c
ompi
led
code
Q&
A
Ram
niva
s La
ddad
ra
mni
vas
ram
niva
s!co
m
@ra
mni
vas