So for what it is worth, here is my way of implementing Cedric’s caching problem using AspectWerkz and caller pointcuts.
I’ll start with the cache advice. The CachingAdvicew caches the result and if the cache is used it reports it to the CacheStatistics instance:
The second advice I had to implement was the InvocationCounterAdvice. This advice will be applied on the caller side of the method invocation and simply reports the each invocation to the CacheStatistics instance:
The next step was to define these advices and define where they should be applied:
In this file I first defined the advices by giving them a name, specifying the class and the deployment model (scope). The second thing was to define the aspects and the pointcuts. By setting an aspect’s class attribute to “.*” I am telling the system to apply its pointcuts to all classes. The pointcuts are defined by specifying the type (method/field/callSide etc.), the pattern (which methods should be advised) and last but not least the references to the advices that should be applied to these poincuts. The call side pointcut’s pattern consists of the target class and the target method concatenated by a “#”. (I don’t know if this is the best syntax to describe this, but it works for now.)
Last I had to implement the CacheStatistics class (code not shown here) and a client class:
When running the client class it produces the following output:
The complete example can be checked out along with the aspectwerkz distribution from the AspectWerkz’s CVS repository (please note that this feature is still very much in development). You can run the example using Maven by executing: maven aspectwerkz:samples:caching