A Calculus of Untyped Aspect-Oriented Programs
Paper byRadha
JagadeesanAlan JeffreyJames Riely
Presented byShay Cohen
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
Introduction - example
Aspects have emerged as a powerful tool
For example:
Introduction - intended
execution
The intended execution semantics is as follows: a call to foo() invokes the code
associated with the advice (the timer is started)
foo() method is invoked when control reaches proceed()
Upon termination of foo(), control returns to the advice
Introduction - Example
public aspect WeavingAspect {
before (String s): execution(void go(*)) && args(s){
System.out.println("before (String s): execution(void go(*)) && args(s) " + s); }
before (Object s): execution(void go(*)) && args(s){
System.out.println("before (Object s): execution(void go(*)) && args(s) " + s); }
}
Introduction- Problem
public class WeavingObject {
public static void main(String[] args) {WeavingObject obj = new
WeavingObject();
String s = null; obj.go(s);
System.out.println();
s = new String("String");obj.go(s);
}
public void go(Object s) {System.out.println("go(Object s) "
+ s); }
}
before (Object s): execution(void go(*)) && args(s) nullgo(Object s) null
before (String s): execution(void go(*)) && args(s) Stringbefore (Object s): execution(void go(*)) && args(s) Stringgo(Object s) String
Introduction- Conclusion
Knowing the aspects semantics is essential.
One cannot naively extend object-oriented reasoning to aspect-oriented programs.
Introduction Reductionism and a specification for
weaving: Advice looked as a primitive Only around advice No reflection Focusing on dynamics
At the end correctness of the weaving transformation will be shown
Weavability will be defined to exclude programs that cannot be woven statically
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
A Class-based Language - Program
'P P
Transformation of a program
( )D HT
D
H
A program P has the form
is a set of declarations is a set of heap allocated threads and objects.
A Class-based Language – class, thread (table 1)
thrd p{new class c<:d {...};} class c<:d {...}
thrd p{ }
class c<:d {...m(x) {C}...}============= =
Superclass dA set of method declarations
thrd p{S}Controlling object pStack S
A Class-based Language – object (table 1)
obj o:c {... f =v ...} obj o:c {... f =v ...}
thrd p{let x=o.f;} thrd p{let x=v;}
obj o:c {... f =v ...}Actual class of the objectValue of fields
obj o:c {... f =u...} obj o:c {... f =v ...}
thrd p{set o.f =v;} thrd p{}
A Class-based Language – Syntax (table 1)
A Class-based Language cont.
Method calls may be dispatched dynamic type static type
let x=o.m(v)
let x=o.c::m(v)
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
A Class-based Language – Reduction
Reduction proceeds via a standard substitution of parameters
A Class-based Language –Reduction example
c
c
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
(R
-
VAL) th
thrd p{l
rd p{let x=p{let y=p.f; set p.
et x
f=b;
=p.m(b);
return )
}
y
(
c
c
c
c
;};}
(R -GET) thrd p{let x=p{let y=a; set p.f=b; return (y);};}
(R -VAL) thrd p{let x=p{set p.f=b; return (a);};}
(R -SET) thrd p{let x=p{return (a);};}
(R -RET
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=a}
A Class-based Language –Dynamic message (table 2)
c
c
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return
(R -VAL) thrd p{let
(y);}
x=p{le
thrd p{let
t y=p.f; se
;}
t
x=p.m(b
p.f=b;
);
re
tu y
}
rn ( )
c
c
c
c
;};}
(R -GET) thrd p{let x=p{let y=a; set p.f=b; return (y);};}
(R -VAL) thrd p{let x=p{set p.f=b; return (a);};}
(R -SET) thrd p{let x=p{return (a);};}
(R -RET
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=a}
A Class-based Language –Value (table 2)
c
c
thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
(R -VAL)
thrd p{let x
thrd p{let
=p.m(b);}
(
x=p{let y=p.f; set p.f=b; return (y
R -DY
)
N-MSG)
c
c
c
c
(R -GET) thrd p{let x=p{let y=a; set p.f=b; return (y);};}
(R -VAL) thrd p{let x=p{set p.f=b; return (a);};}
(R -SET) thrd p{let x=p{return (a);}
;}
(R -RE
}
T
;};
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=a}
A Class-based Language –Get (table 2)
c
c
thrd p{let x=p.m(b);}
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
thrd p{let x=p{let y=p.f; set p. f= (R -VAL b; return )) (y
c
c
c
c
(R -VAL) thrd p{let x=p{set p.f=b
(R -GET) thrd p{let x=p{
; return (a);};}
(
;};
R -SET
let y=a; se
) thrd p
t p.f=b; return (y
{let x=p{return (a
);};}
);};}
(
R
RE
}
- T
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=a}
A Class-based Language – Set (table 2)
c
c
thrd p{let x=p.m(b);}
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
(R -VAL) thrd p{let x
=p{let y=p.f; set p.f=b; retu
)
rn (y
c
c
c
c thrd p{let
;};}
x=p{set p.f=b;
(R -GET) thrd p{let x=p{let
return (a);};}
(R
y=a
-S
; set p.f=b; ret
ET) thrd p{le
urn (y);};
t x=p{retu
}
rn (a);};
}
(R -V
(R R T
A )
E
L
-
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=b}
A Class-based Language –Return (table 2)
c
c
thrd p{let x=p.m(b);}
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
(R -VAL) thrd p{let x=p{let y=p.f; set p.f=b; return (y)
c
c
c
c
;};}
(R -GET) thrd p{let x=p{let y=a; set p.f=b; return (y);};}
(R -VAL) thrd p{let x=p{set p.f=b; return (a);};}
(R thrd p{let x=p{return (a);};
-SET)
(R ET
}
-R
c
URN) thrd
(R -VAL)
p{let x=a
th {}
;}
rd p
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=b}
A Class-based Language -Reduction
c
c
thrd p{let x=p.m(b);}
(R -DYN-MSG) thrd p{let x=p{let z=b; let y=p.f; set p.f=z; return (y);};}
(R -VAL) thrd p{let x=p{let y=p.f; set p.f=b; return (y)
c
c
c
c
;};}
(R -GET) thrd p{let x=p{let y=a; set p.f=b; return (y);};}
(R -VAL) thrd p{let x=p{set p.f=b; return (a);};}
(R -SET) thrd p{let x=p{return (a);};}
(R -RET
c
URN) thrd p{let x=a;}
(R -VAL) thrd p{}
class d <: Object {m(x) {let z=x; let y=p.f; set p.f=z; return (y);}}
obj p:d{f=b}
A Class-based Language – This, Super (table 2)
(L -THIS)c
D class c<: _ {M, m(x) {C}}
D body(c::m) = (x)C
============= =
============= =T
(R -SUPER)c
D class c<:d {M}
M m(_) {_}
D body(d::m) = (x)C
D body(c::m) = (x)C
============= =
============= =T
T
A Class-based Language – Reduction summary
Rules Usage
(RC-LET), (RC-RETURN) and (RC-GARBAGE)
Deal with pushed frames
(RC-VALUE) Returned values to be substituted
(RC-DEC) and (RC-HEAP) Create new classes, objects and threads
(RC-GET) and (RC-SET) Manipulation of fields
(RC-DYN-MSG) and (RC-STC-MSG) Reduction on method calls
(LC-THIS) Method body to be retrieved
(LC-SUPER) Looks for a method in the superclass
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
An Aspect-based Language – Example
Advised message
An Aspect-based Language – Aspect declaration (table 3)
adv a(x): {C}============= =
Aspect declaration Name – allows reference Command sequence – what to
execute Pointcut – when to execute
pointcuts are presented as elements of the Boolean algebra
An Aspect-based Language – Advice (table 3)
Advised method call Call advice set Execution advice set
let x=o.m[a ; b](v)
ab
let x=proceed(v);
Advice body new command
An Aspect-based Language – method (table 3)
Dynamically dispatched method changed We now have the static type as well Static type is needed for call advice
We ignore call advice for super calls
let x=o:c.m(v)
An Aspect-based Language – Advice creation
Given a method definition
class d <:c {...m(x) {C}...}============= =
class d <:c {...m[ ; cbl_d_m]...}
adv cbl_d_m(x):exec(d::m) {C[proceed/super.m]}
============= =
methods in the aspect calculus do not have any method bodies:
An Aspect-based Language – Aspect based syntax (table 3)
An Aspect-based Language – Semantics of pointcuts (table 4)
(S-EXTENDS)
D class c<:d {_}
D c<:d
T
(S-REFLEX)
D c<:c T
(S-TRANS)
D c<:d
D d <:e
D c<:e
T
T
T
An Aspect-based Language – Semantics of pointcuts (table 4)
(PC-EXEC)
D c<:d
D c::m execadv(exec(d::m))
T
T
(PC-CALL)
D c<:d
D c::m calladv(call(d::m))
T
T
An Aspect-based Language – Semantics of pointcuts (table 4)
An Aspect-based Language – Semantics of pointcuts (table 4)
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
An Aspect-based Language – Reduction example
A
A
A
(R -DYN-MSG) thrd p{let x=o.m[ca ; cb]();}
(R -ADV-MSG2) thrd p{let x=p
{let y=o.m[ ; cb]();return
();
thrd p{let x=o:c.m();}
};}
(R -ADV-MSG1) thrd p{let x=p
A
A
A
A
{let y=o{return ();};return ();};}
(R -RETURN) thrd p{let x=p{let y=();return ();};}
(R -VAL) thrd p{let x=p{return ();};}
(R -RETURN) thrd p{let x=();}
(R -VAL) th
rd p{}
obj p:Main {}
class Main{m[ ; ma]}
adv ma():exec(Main::m) {let x=o:c.m();return ();}
obj o:c {}
class c{m[ca ; cb]}
adv ca():call(c::m) {let y=proceed();return ();}
adv cb():exec(c::m) {return ();}
An Aspect-based Language – Dynamic message (table 5)
A
A
A
(R -DYN-MSG) thrd p{let x=o.m[ca ; cb]
(R -ADV-MSG2) thrd p{l
thr
et
x=p{let y=o.m[ ; cb]();return ()
d p
;};
{let x
}
(R -
ADV-
=o:c.m(
MSG1) thrd p{let x
();
=
}
p
);}
A
A
A
A
{let y=o{return ();};return ();};}
(R -RETURN) thrd p{let x=p{let y=();return ();};}
(R -VAL) thrd p{let x=p{return ();};}
(R -RETURN) thrd p{let x=();}
(R -VAL) th
rd p{}
obj p:Main {}
class Main{m[ ; ma]}
adv ma():exec(Main::m) {let x=o:c.m();return ();}
obj o:c {}
class c{m[ca ; cb]}
adv ca():call(c::m) {let y=proceed();return ();}
adv cb():exec(c::m) {return ();}
An Aspect-based Language – Advice message (call) (table 5)
A
A
A
thrd p{let x=o.m[ca ; cb]
(R -ADV-MSG2)
thrd p{let x=p{let y=o.m[ ; cb
thrd p{let x
]();return (
=o:c.m();}
(R -DYN-MSG
(R -ADV-MSG
)
1
);
)
(
thrd p{let x
)
};
}
p
;}
=
A
A
A
A
{let y=o{return ();};return ();};}
(R -RETURN) thrd p{let x=p{let y=();return ();};}
(R -VAL) thrd p{let x=p{return ();};}
(R -RETURN) thrd p{let x=();}
(R -VAL) th
rd p{}
obj p:Main {}
class Main{m[ ; ma]}
adv ma():exec(Main::m) {let x=o:c.m();return ();}
obj o:c {}
class c{m[ca ; cb]}
adv ca():call(c::m) {let y=proceed();return ();}
adv cb():exec(c::m) {return ();}
An Aspect-based Language – Advice message (execution) (table 5)
A
A
A
thrd p{let x=o:c.m();}
(R -DYN-MSG) thrd p{let x=o.m[ca ; cb]
thrd p{let x=p{let y=o.
m[ ; cb]();re
();}
(R -ADV-MSG turn ();
(
R
2)
-
ADV-MSG1) th
};}
rd
p
{let x=
p
A
A
A
A
(R -RETURN) thrd p{let x=p{let y=();return ();};}
(R -VAL) thrd p{let x=p{return ();};}
(R -RETURN) thrd
{let y=o{return ();};return
p{let x=();}
(R
();};}
-VAL) h
t
rd p{}
obj p:Main {}
class Main{m[ ; ma]}
adv ma():exec(Main::m) {let x=o:c.m();return ();}
obj o:c {}
class c{m[ca ; cb]}
adv ca():call(c::m) {let y=proceed();return ();}
adv cb():exec(c::m) {return ();}
An Aspect-based Language – Reduction
A
A
A
thrd p{let x=o:c.m();}
(R -DYN-MSG) thrd p{let x=o.m[ca ; cb]();}
(R -ADV-MSG2) thrd p{let x=p{let y=o.m[ ; cb]();return ();};}
(R -ADV-MSG1) thrd p{let x=p
A
A
A
A
{let y=o{return ();};return ();};}
(R -RETURN) thrd p{let x=p{let y=();return ();};}
(R -VAL) thrd p{let x=p{return ();};}
(R -RETURN) thrd p{let x=();}
(R -VAL) th
rd p{}
obj p:Main {}
class Main{m[ ; ma]}
adv ma():exec(Main::m) {let x=o:c.m();return ();}
obj o:c {}
class c{m[ca ; cb]}
adv ca():call(c::m) {let y=proceed();return ();}
adv cb():exec(c::m) {return ();}
An Aspect-based Language – Aspect based Reduction (table 5)
An Aspect-based Language – Reduction summary
Rules Purpose
(RA-DYN-MSG) and (RA-STC-MSG) Rely on the advice sets declared by methods.
(LA-TOP) Ensure that (RA-DYN-MSG)always succeeds
(RA-DYN-MSG) Call and execution advice
(RA-ADV-MSG1) Reduction of execution advice.
(RA-ADV-MSG2) Reduction of call advice
An Aspect-based Language – Coherence
advice that appears in a method declaration must be consistent with that which is attached to a pointcut
A collection of declarations D is coherent (resp. semicoherent)
if whenever D adv b(_): {_} and D class c<:_ {...m[a ; a']...} then
b a iff (resp
DEFINITION 1
. implies) D
(COHERENCE)
c::m ca
.
lladv(
T )
and b a' iff (resp. implies) D c::m execadv( ) T
An Aspect-based Language – Close
(C-FIX)
We define the function close(D),
which saturates class
declarations with
DEF
advice:
D is coh
INITION 2 (C
erent
close(D
LOSE).
) = D
(C-CALL)
(C-EXEC)
D adv a(_): {_}
D c::m calladv( )
D = E, class c<:d {M, m[a ; b]}
close(D) = close(E, class c<:d {M, m[a, a; b]})
D adv b(_): {_}
D c::m execadv( )
D= E, class c<:d {M, m[a ; b]}
close
T
T
(D) = close(E, class c<:d { M, m[a; b, b]})
An Aspect-based Language – Lemma
If D is semi-coherent, then close(
LEMMA 1
D) is co
(
h
CLOSE).
erent.
Cohe
LEMMA
rence
2 (COHERE
is preser
NCE PRESE
ved by re
RVATIO
duc
N).
tion.
Note that any program where each class declaration is taken
from the class-based language is semi-coherent by construction.
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
Weaving – Requirements
Correctness is formalized by demanding that the following diagram can be completed
We also expect that a woven program not have spurious new reductions.
Weaving – Macro expansion
Weaving as Macro Expansion The body of a method is determined
by selecting the body of the first advice named in the advice list.
The rule is applied again, after substituting the remaining advice through for proceed.
Weaving – New methods
Weaving by Introducing New Methods given a method m affected by advice
, we create an auxiliary method for each suffix of the list .
Call advised methods are placed in the class of the caller, whereas execution advised methods are placed in the class of the callee.
a
a
Weaving – Advice weaving (table 6)
Weaving – Advice weaving (table 6)
Weaving – Advice weaving, execution, call (table 6, 7)
Contents Introduction A Class-based Language
Syntax Reduction
An Aspect-based Language Syntax Reduction
Weaving The Correctness of Weaving
The Correctness of Weaving – Weavability
Weaving is not correct for all programs. Weaving does not support the dynamic
loading of advice Controlling object of all threads must be
defined All advised messages m[ ; ] in a thread with
controlling object p should arise no dangling references
We formalize these intuitions as Weavability.
The Correctness of Weaving – Weavability definition
We define D; H ; n weavable(·) on stacks in Table 8.
Extend the definition to programs and advice declarations as follows:
D; H; bn(H) weava
DEFINITION 3 (W
ble(D)
D; H; bn(
EAVA
H) weavable(H
BILITY).
T
T
T )
weavable(D H)
D; H; n, x, this, target weavable(this{C})
D; H; n weavable(adv a(x): {C})
============= =
============= =
T
T
T
The Correctness of Weaving – Weavability of stacks (table 8)
The Correctness of Weaving – Lemma
Weavability is preserved by red
LEMMA 3.
uction.
The Correctness of Weaving – Name equivalence
Let be the equivalence on class-based commands generated by:
H obj o:d {_}
D advice(d::m) = [_; b]
D; H let x=o.m(v); let x=o
DEFINITION 4 (NAME EQUIV
.exec_b(v);
D advic
ALENCE)
e(c::m) = b]
.
[_;
T
T
T
D; H let x=o.c::m(v); let x=o.exec_b(v);
C is not a method call
D; H C C
Extend the definition to all other program constructs homorphically using conjunction.
Let P = (D H) and P' = (D' H')
T
T
T T . We write "P P0" when D; H P P' and
D'; H' P P'.
T
T
The Correctness of Weaving – Theorem
Suppose that an aspect-based program P is coherent and weavable, and
that P P'. Then there exists some Q', such that weave(P) Q' and Q' weave(P').
Suppose that an aspect-based p
THEOREM 1
rogram P c
.
is
oherent and weavable, and that weave(P) Q'.
Then there exists some P', such that P P' and P' weave(Q').
The end
Table 1
Table 2
Table 3
Table 4
Table 5
Table 6
Table 7
Table 8
Top Related