Call and Execution Semantics in AspectJ

Call and Execution
Semantics in AspectJ
Ohad Barzilay
Yishai A. Feldman
Shmuel Tyszberowicz
Amiram Yehudai
Agenda




Describe the current semantics of AspectJ call
and execution join points with respect to
inheritance
Argue with the semantics of some of these
constructs
Suggest some alternative semantics
Discuss the expressive power of the
alternatives
Call and Execution Semantics in AspectJ, FOAL 2004
2
Scope of discussion

From the AspectJ Programming Guide:
“call(MethodPattern):
Picks out each method call join point
whose signature matches MethodPattern”

The semantics of “matches”:
•
•
•
Based on which type?
•
•
Type of the object?
Type of the reference?
Does the match include subclasses as well?
…
Call and Execution Semantics in AspectJ, FOAL 2004
3
Motivation


Automatically generated AOP code
JOSE tool – enforcement of Design by
Contract methodology using AspectJ
Call and Execution Semantics in AspectJ, FOAL 2004
4
Class hierarchy
public class A1
{
public void f() {}
public void g() {}
}
public class A2 extends A1
{
public void h() {}
}
public class A3 extends A2
{
public void f() {}
}
A1
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
5
Calling who?


A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut call(void A1.f())
s1.f()
s3.f()
s1d3.f()
pointcut call(void A1.g())
s1.g()
s3.g()
s1d3.g()
A1
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
6
Calling who?


A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut call(void A1.f())
s1.f()
s3.f()
s1d3.f()
pointcut call(void A1.g())
s1.g()
s3.g()
s1d3.g()
A1
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
7
Calling who?



A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut call(void A1.f())
s1.f()
s3.f()
s1d3.f()
pointcut call(void A1.g())
s1.g()
s3.g()
s1d3.g()
What is the ‘+’ modifier for?
Call and Execution Semantics in AspectJ, FOAL 2004
A1
f()
g()
A2
h()
A3
f()
8
Calling who?

A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut call(void A3.f())
s3.f()
s1d3.f()


The match here was based on the
static type of the reference
Not consistent with java
A1
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
9
Calling who?

A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut call(void A3.g())
s1.g()
s1d3.g()
s3.g()



Does not capture any of our join
points
g() was not lexically defined in
A3
Not consistent with java
Call and Execution Semantics in AspectJ, FOAL 2004
A1
f()
g()
A2
h()
A3
f()
10
The call model

Notation:
•
•
•
pcc  call(void C. f ())
pointcut
S x = new D()
variable defined as
join point
jp  x. f ()
Call and Execution Semantics in AspectJ, FOAL 2004
11
The call model


Notation:
•
•
•
pcc  call(void C. f ())
pointcut
S x = new D()
variable defined as
join point
jp  x. f ()
For this to compile D  S
Call and Execution Semantics in AspectJ, FOAL 2004
12
The call model



Notation:
•
•
•
pcc  call(void C. f ())
pointcut
S x = new D()
variable defined as
join point
jp  x. f ()
For this to compile D  S
And we get:
jp  pcc  S  C  f is lexically defined in C
Call and Execution Semantics in AspectJ, FOAL 2004
13
Executing what?

A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut execution(void A1.f())
s1.f()
s3.f()
s1d3.f()
A1
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
14
Executing what?

A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
pointcut execution(void A1.f())
s1.f()
s3.f()
s1d3.f()
A1
f()
g()
A2

The execution of f() from inside A3
matched A1.f()
•
•
Reasonable for call
Surprising for execution
Call and Execution Semantics in AspectJ, FOAL 2004
h()
A3
f()
15
Executing what?
A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
Almost the same as call
But:
 s1d3.f()


execution(void A3.f())
call(void A3.f())
A1
f()
g()
A2
h()

call and execution differ in
more than just their context
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
16
call vs. execution

From the AspectJ Programming Guide:
“The rule of thumb is that if you want to pick a
join point that runs when an actual piece of code
runs (as is often the case for tracing), use
execution, but if you want to pick one that runs
when a particular signature is called (as is often the
case for production aspects), use call.”

No!
Call and Execution Semantics in AspectJ, FOAL 2004
17
The execution model

Notation:
•
•
•
pce  execution(void C. f ())
pointcut
S x = new D()
variable defined as
join point
jp  x. f ()
Call and Execution Semantics in AspectJ, FOAL 2004
18
The execution model

Notation:

And we get:
•
•
•
pce  execution(void C. f ())
pointcut
S x = new D()
variable defined as
join point
jp  x. f ()
jp  pce  D  C  f is lexically defined in C
Call and Execution Semantics in AspectJ, FOAL 2004
19
A1 s1 = new A1();
A3 s3 = new A3();
A1 s1d3 = new A3();
Running in the family


s3.h()

A1
call(void A1+.h())
call(void A3+.h())
Consistent, but surprising due to
previous problems
f()
g()
A2
h()
A3
f()
Call and Execution Semantics in AspectJ, FOAL 2004
20
The subtype pattern model

Notation:
•
•
•
•

pcc  call(void C  . f ())
call pointcut
pce  execution(void C  . f ())
execution pointcut
variable defined as S x = new D()
join point jp  x. f ()
Call and Execution Semantics in AspectJ, FOAL 2004
21
The subtype pattern model


Notation:
•
•
•
•

pcc  call(void C  . f ())
call pointcut
pce  execution(void C  . f ())
execution pointcut
variable defined as S x = new D()
join point jp  x. f ()
And we get:
jp  pcc  S  C 
f is lexically defined in some F s.t. S  F  C
jp  pce  D  C 
f is lexically defined in some F s.t. D  F  C
Call and Execution Semantics in AspectJ, FOAL 2004
22
Summary

Intuitive:
• Pointcuts with subtype patterns are equivalent
•
to the union of all pointcuts with subtypes
substituted for the given type.
The semantics of execution pointcuts is based
on the dynamic type of the target.
Call and Execution Semantics in AspectJ, FOAL 2004
23
Summary

Unintuitive:
• The semantics of call pointcuts depends on
•
•
the static type of the target.
Call and execution pointcuts only capture join
points for classes where the given method is
lexically defined.
As a result of this, the difference between
pointcuts with or without subtype patterns is
subtle and unintuitive.
Call and Execution Semantics in AspectJ, FOAL 2004
24
Alternative semantics


In our opinion, the lexical definition
requirement should be removed
Two questions remain:
1. Should subclasses be included when the
subtype pattern modifier does not appear in the
pointcut? (“narrow” vs. “broad”)
2. Should call join points based on their “static” or
“dynamic” type?
Call and Execution Semantics in AspectJ, FOAL 2004
25
call pointcut in the proposed
semantics
pcc  call(void C. f ())
jp  pcc  ...
Narrow
S x = new D()
jp  x. f ()
Broad
Static
S  C  f exists in C
S  C  f exists in C
Dynamic
D  C  f exists in C
D  C  f exists in C
Call and Execution Semantics in AspectJ, FOAL 2004
26
Alternative semantics



Each of the four semantics is consistent and
reasonable
Perhaps the broad–dynamic semantics best
reflects object oriented principles, in that a
reference to a class includes its subclasses
AspectJ has other constructs to get the
desired behavior
•
•
within()
target()
Call and Execution Semantics in AspectJ, FOAL 2004
27
Bottom line

A starting point for a discussion of desired AspectJ
semantics

Driving force should be user needs

Common tasks should be simple
•
Use other pointcut specifiers for less common tasks

Should conform with OOP methodology

Should be internally consistent
Call and Execution Semantics in AspectJ, FOAL 2004
28
Related work
A Calculus of Untyped Aspect-Oriented Programs
R. Jagadeesan, A. Jeffrey, and J. Reily
A Theory of Aspects
D. Walker, S. Zdancewic, and J. Ligatti
A Semantical Approach to Method-Call Interception
R. Lämmel
Compilation semantics of aspect-oriented programs
H. Masuhara, G. Kiczales, and C. Dutchyn
A semantics for pointcuts and advice in higher-order languages
D. B. Tucker and S. Krishnamurthi
A semantics for advice and dynamic join points in aspect-oriented
Programming
M. Wand, G. Kiczales, and C. Dutchyn
Call and Execution Semantics in AspectJ, FOAL 2004
29
Questions ?
A Discussion