300 likes | 432 Views
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
E N D
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
Scope of discussion • From the AspectJ Programming Guide: “call(MethodPattern): Picks out each method call join point whose signature matchesMethodPattern” • 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
Motivation • Automatically generated AOP code • JOSE tool – enforcement of Design by Contract methodology using AspectJ Call and Execution Semantics in AspectJ, FOAL 2004
public class A1 { public void f() {} public void g() {} } A1 f() g() public class A2 extends A1 { public void h() {} } A2 h() public class A3 extends A2 { public void f() {} } A3 f() Class hierarchy Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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() Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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() Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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() 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 Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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
The call model • Notation: • pointcut • variable defined as • join point Call and Execution Semantics in AspectJ, FOAL 2004
The call model • Notation: • pointcut • variable defined as • join point • For this to compile Call and Execution Semantics in AspectJ, FOAL 2004
The call model • Notation: • pointcut • variable defined as • join point • For this to compile • And we get: Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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() Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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() • The execution of f() from inside A3 matched A1.f() • Reasonable for call • Surprising for execution Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() 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()) • call and execution differ in more than just their context Call and Execution Semantics in AspectJ, FOAL 2004
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
The execution model • Notation: • pointcut • variable defined as • join point Call and Execution Semantics in AspectJ, FOAL 2004
The execution model • Notation: • pointcut • variable defined as • join point • And we get: Call and Execution Semantics in AspectJ, FOAL 2004
A1 f() g() A2 h() A3 f() A1 s1 = new A1(); A3 s3 = new A3(); A1 s1d3 = new A3(); Running in the family • s3.h() • call(void A1+.h()) • call(void A3+.h()) • Consistent, but surprising due to previous problems Call and Execution Semantics in AspectJ, FOAL 2004
The subtype pattern model • Notation: • call pointcut • execution pointcut • variable defined as • join point Call and Execution Semantics in AspectJ, FOAL 2004
The subtype pattern model • Notation: • call pointcut • execution pointcut • variable defined as • join point • And we get: Call and Execution Semantics in AspectJ, FOAL 2004
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
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
Alternative semantics • In our opinion, the lexical definition requirement should be removed • Two questions remain: • Should subclasses be included when the subtype pattern modifier does not appear in the pointcut? (“narrow” vs. “broad”) • Should call join points based on their “static” or “dynamic” type? Call and Execution Semantics in AspectJ, FOAL 2004
call pointcut in the proposed semantics Broad Narrow Static Dynamic Call and Execution Semantics in AspectJ, FOAL 2004
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
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
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
Questions ? A Discussion