Stateful Aspects in JAsCo Wim Vanderperren | pag. 1
Natural Evolution of joinpoints: Stateful Aspects Natural Evolution of joinpoints: Static Dynamic (e.g. cflow) Stateful Stateful Aspects in JAsCo | pag. 2
Timing Aspect 1 class DynamicTimer { 3 private Vector<TimeListener> listeners = new Vector<TimeListener>(); 4 private long timestampbefore, timestampafter; 6 void addTimeListener(TimeListener aListener) { 7 listeners.add(aListener); 8 } 9 void removeTimeListener(TimeListener aListener) { 10 listeners.remove(aListener); 11 } 12 void notifyTimeListeners(Method method, long time) { 13 for (TimeListener listener : listeners) { 14 listener.timeStampTaken(method,time); 15 } 16 } 18 hook TimeStamp { 19 TimeStamp(timedmethod(..args)) { 20 execution(timedmethod); 21 } 22 before { 23 timestampbefore = System.currentTimeMillis(); 24 } 25 after { 26 timestampafter = System.currentTimeMillis(); 27 notifyListeners(thisJointPoint,timestampafter-timestampbefore); 28 } 29 } 30 } Stateful Aspects in JAsCo | pag. 3
Timing one method: But how to time a protocol? E.g. time between execution of a-b-c on ComponentX. 1 connector TimeConnector { 2 DynamicTimer.TimeStamp timer = 3 new DynamicTimer.TimeStamp (void ComponentX.a()); 4 timer.before(); 5 timer.after(); 6 } Stateful Aspects in JAsCo | pag. 4
Protocol Timing Aspect? 1 class ProtocolDynamicTimer extends DynamicTimer { 2 3 boolean methodaexecuted, methodbexecuted = false; 4 5 hook ProtocolMethodA { 7 ProtocolMethodA(methodA(..args)) { 8 execution(methodA); 9 } 10 before { 11 timestampbefore = System.currentTimeMillis(); 12 methodaexecuted = true; 13 } 14 isApplicable() { return !methodaexecuted; } 15 } 16 17 hook ProtocolMethodB { 18 ProtocolMethodB(methodB(..args)) { 19 execution(methodB); 20 } 21 before { 22 methodbexecuted = true; 23 } 24 isApplicable() { return methodaexecuted; } 25 } 26 27 hook ProtocolMethodC { 28 ProtocolMethodC(methodC(..args)) { 29 execution (methodC); 30 } 31 after { 32 timestampafter = System.currentTimeMillis(); 33 notifyListeners(method,timestampafter-timestampbefore); 34 methodaexecuted = false; methodbexecuted = false; 35 } 36 isApplicable() { return methodbexecuted; } 37 } 39 } connector TimeConnector { DynamicTimer.ProtocolMethodA timera = new DynamicTimer. ProtocolMethodA (void ComponentX.a()); DynamicTimer.ProtocolMethodB timerb = new DynamicTimer. ProtocolMethodB (void ComponentX.b()); DynamicTimer.ProtocolMethodC timerc = new DynamicTimer. ProtocolMethodC (void ComponentX.c()); } Stateful Aspects in JAsCo | pag. 5
Stateful Aspects: Motivation Protocol based aspects are rarely supported in current AOSD approaches (apart cflow). Result: protocol checking code is scattered over advices, poluting advice code… Is also not optimal, checks at all possible joinpoints while only a subset is required. Stateful Aspects in JAsCo | pag. 6
Example: Realize a more general protocol history condition than cflow. Stateful Aspects Allows specifying regular (=DFA based) hook triggering conditions in a hook’s constructor. Advices can be attached to any transition of the DFA, or all of them (global). Example: Realize a more general protocol history condition than cflow. Stateful Aspects in JAsCo | pag. 7
Timing Example Revisited 1 class ProtocolDynamicTimer extends DynamicTimer { 2 3 hook StatefulProtocolTimer { 4 5 long timestamp; 6 7 StatefulProtocolTimer(methodA(..args),methodB(..args),methodC(..args)) { 8 ATrans: execution(methodA) > BTrans; 9 BTrans: execution(methodB) > CTrans; 10 CTrans: execution(methodC) > ATrans; 11 } 12 13 before ATrans() { 14 timestamp=System.currentTimeMillis(); 15 } 16 after CTrans() { 17 long resultingtime = System.currentTimeMillis(); 18 notifyListeners(calledmethod,resultingtime-timestamp); 19 } 20 21 } 22 } Regular JAsCo Pointcuts for triggering the transition Transition Labels Stateful Pointcut Following Transitions to evaluate Attaching Advices to specific transitions Stateful Aspects in JAsCo | pag. 8
Timing Example Revisited static connector TimingConnector { perthread ProtocolDynamicTimer.StatefulProtocolTimer timer = new ProtocolDynamicTimer.StatefulProtocolTimer( void ComponentX.a()., void ComponentX.b(), void ComponentX.c() ); } Stateful Aspects in JAsCo | pag. 9
Advanced Pointcut Features Explicit start transitions start > XTrans; Starting with two transitions: start > XTrans || QTrans; two destination transitions: XTrans: execution(methodA) > YTrans || QTrans; no destination transition QTrans: execution(methodB) && !cflow(methodC); Stateful Aspects in JAsCo | pag. 10
Advices Multiple specific advices possible, one for each transition per before/around */after * Global advice works for all transitions isApplicable can also be global or specific isApplicable() { //global condition for all transitions // returns true if advices should be executed } isApplicable XTrans() { //local condition only relevant for the transition XTrans // returns true if advices should be executed for the XTrans transition advantages Stateful Aspects in JAsCo | pag. 11
Strict Protocols Per default, stateful aspects are non-strict, e.g. protocol a()-d()-b()-c() also matches timing aspect. When no intermediate transitions are allowed, use the strict keyword: strict: ATrans: execution(methodA) > BTrans; BTrans: execution(methodB) > CTrans; CTrans: execution(methodC) > ATrans; Stateful Aspects in JAsCo | pag. 12
Strict Protocols with Context Only strict with respect to a certain set of joinpoints. E.g. timing aspect does not accept intermediate transitions on ComponentX, on other classes is ok though. StatefulProtocolTimer(methodA(..args),methodB(..args),methodC(..args), context(..args)) { strict[execution(context)]: ATrans: execution(methodA) > BTrans; BTrans: execution(methodB) > CTrans; CTrans: execution(methodC) > ATrans; } Rejects: ComponentX.a() ComponentX.d() ComponentX.b() ComponentX.c() static connector TimingConnector { …. new ProtocolDynamicTimer.StatefulProtocolTimer( void ComponentX.a()., void ComponentX.b(), void ComponentX.c(), void ComponentX.*(*) ); } Accepts: ComponentX.a() Y.d() ComponentX.b() ComponentX.c() Stateful Aspects in JAsCo | pag. 13
Complement of a protocol Trigger advices on everything outside the protocol: COMPLEMENT StatefulProtocolTimer(methodA(..args),methodB(..args),methodC(..args)) { complement: ATrans: execution(methodA) > BTrans; BTrans: execution(methodB) > CTrans; CTrans: execution(methodC) > ATrans; } around complement() { throw new SecurityException(“this is not allowed on “+thisJoinPoint.getClass()); Stateful Aspects in JAsCo | pag. 14
Complement with Context Only complement with respect to a certain set of joinpoints. StatefulProtocolTimer(methodA(..args),methodB(..args),methodC(..args), context(..args)) { complement[execution(context)]: ATrans: execution(methodA) > BTrans; BTrans: execution(methodB) > CTrans; CTrans: execution(methodC) > ATrans; } d() in complement: ComponentX.a() ComponentX.d() ComponentX.b() ComponentX.c() static connector TimingConnector { …. new ProtocolDynamicTimer.StatefulProtocolTimer( void ComponentX.a()., void ComponentX.b(), void ComponentX.c(), void ComponentX.*(*) ); } No complement: ComponentX.a() Y.d() ComponentX.b() ComponentX.c() Stateful Aspects in JAsCo | pag. 15
Default: weaved at all possible joinpoints defined in the pointcut. Weaving A DFA (Deterministic Finite Automaton) executes the stateful aspect at run-time. Default: weaved at all possible joinpoints defined in the pointcut. Optionally, jumping aspect in cooperation with run-time weaver: only weaved at next joinpoints in protocol overhead of reweaving vs overhead of aspect applied at all Stateful Aspects in JAsCo | pag. 16