Logo
StartDownloadsDocumentationPublicationsContactCommunityDevelopers

Quick Stateful Aspects tutorial

  • This tutorial gives a quick intro on stateful aspects in JAsCo. For a more detailed explanation, please read the following paper:

Vanderperren, W., Suvee, D., Cibran, M. and De Fraine, B. Stateful Aspects in JAsCo. In proceedings of SC 2005, LNCS, April 2005. [download paper]

  • For hands-on experience with stateful aspects, refer to the JAsCo Eclipse plugin homepage. There is a demo JAsCo eclipse project that contains several flavors of stateful aspects.

What are stateful aspects?

Stateful aspects are aspects that define a composite triggering condition based upon regular expressions. For example, suppose you want to implement a logging aspect that only logs the activity when a user is logged in. As such, the aspect should start to log when the login method is executed and stop when the logout method is executed. This requires a stateful triggering condition (= pointcut in AspectJ terminology). Likewise, advices can also be state-aware, and thus only execute when certain branches of the composite triggering condition are fulfilled.

Available since JAsCo 0.5

An example stateful aspect

package test;
 
class ExampleLoggerBean {  
    hook StatefulHook {    
 
        StatefulHook(startmethod(..args1), runningmethod(..args2),stopmethod(..args3)) {	
            start>p1;
            p1: execution(startmethod) > p3||p2;
            p3: execution(stopmethod) > p1;  
            p2: execution(runningmethod) > p3||p2; 
        }      
 
        before p2 (){ 
             //do the logging:
             System.out.println("executing: "
                 +calledmethod.getName()+" "
                 +calledmethod.getClassName());
        }	 
    } 
}

This aspect implements the logging concern that only starts logging when a certain method is executed and ends when another method is executed. Therefore the constructor specifies three abstract method parameters (startmethod, runningmethod and stopmethod). The constructor body specifies the stateful triggering condition itself. The start keyword specifies the first pointcut(s) that have to be matched. In this case, it is not strictly necessary to specify the start transition because when the start transition is not specified, the regular expression starts with the first transition (here p1). Every transition consists of the following format:

"transition name" : "JAsCo compatible pointcut" > "following transitions";

When the pointcut evaluates to true, the state of the stateful aspect is altered and the transitions contained in the “following transitions” enumeration are processed for the consecutive joinpoints encountered. When the “following transitions” is empty, then the aspect’s behavior ends here. The aspect is thus never executed in the program run aymore. Advices specified in the regular way (ex: before() {...}), are executed for every transition that matches. It is also possible to define advices on specific transitions only, by employing the following syntax:

before|after|around "transition name" {....}

The before p2 advice is an example of specifying an advice on a single transition. Note that combining a global advice and a specific adivce of the same type is not allowed!

Notice that the regular JAsCo constructor body “execute(method)” is in fact syntactic sugar for:

p1: execution(method) > p1;

Applying stateful aspects Suppose we instantiate the ExampleLoggerBean onto the Juggler bean in the following way:

static connector testconnector {
    ExampleLoggerBean.StatefulHook hook1 = new 
        ExampleLoggerBean.StatefulHook(
            * *.*.startJuggling(*),
            * *.*(*), 
            * *.*.stopJuggling(*));    
}

As such, the ExampleLoggerBean will do nothing until the startJuggling method is encountered. From then on, the aspect will log every method untill the stopJuggling method is executed. From that point on, the aspect waits again for a startJuggling method. Notice that due to the abstract pointcut description, this aspect is highly reusable, it can for instance be easily applied to the login/logout example elucidated in the first paragraph.

Important Notice

JAsCo does not require to specify the full state machine of a collaboration, a protocol fragment is sufficient. JAsCo stateful aspects are non-struct per default, meaning that intermediate transitions are allowed.

Advanced Features

isApplicable construct

The isApplicable construct can be employed to specify programmed conditions on transitions. Per default, the isApplicable method specifies an additional condition for all transitions. Likewise to the advices, the isApplicable construct can be specialized for a single transition. The following example specifies an additional condition for the StatefulHook, which causes no logging when the logging aspect has been disabled:

isApplicable p2() {
    return loggingEnabled();
}

Inter-crosscut variables

It is also possible to specify inter crosscut variables as explained by [1]. In short, a crosscut variable is bound the first time it is matched with a joinpoint. This allows, for example, to specify an explicit pointcut that allows counting the number of executions of the firstly executed method:

package test;

class CounterBean {  
    hook Count {    
        int c=0;

        Count(method(..args)) {
	    p1: execution(+method) > p1;
        }      
 		 
        before p1 (){ 
            c++;
        }	 
    } 
}

Notice the + sign to denote the crosscut variable. The first time a joinpoint matches the pointcut, the variable is fixed to the context of the current joinpoint.

Other Important Features

  • Specifying the complement of a protocol
  • Strict vs non-strict protocols
  • shorthand notation
  • jumping aspects

For more information of the above features, read the stateful aspects paper. More papers will follow!

References

Formal basis for stateful aspects: [1] R. Douence, P. Fradet, M. Südholt. “Composition, Reuse and Interaction Analysis of Stateful Aspects”, 3rd Int. Conf. on Aspect-Oriented Software Development (AOSD’04), to appear, Mar. 2004.

documentation/stateful.txt · Last modified: 30.07.2005 11:15 by wvdperre
SiteMapRecent Content by SSEL ©2002-2005     Latest JAsCo Release: JAsCo 0.8.7 (revised 2005-11-25 16:29:02)