Introduction

Aspect-oriented programming helps programmers in the separation of (cross-cutting) concerns to improve the overall architecture of an application by making it more modular and removing duplicate code.

Tuesday, May 4th, 2004 at 6:43 pm

AOP: Lexical-Structure Based Pointcuts

You can capture join points based on their lexical structure, that means on the scope of the code as it was written. There do exist two lexical-structure pointcuts: within(Type) and withincode(Method-/ConstructorSignature). I’ll explain how the function with the help of two examples:

within(MyClass+)
This would capture all join points inside the lexical scope of the class MyClass and all its subclasses.

withincode(* MyClass.myMethod(..))
This would capture all join points inside the lexical scope of any myMethod() method of the class MyClass.

I have used wildcards in the examples to make the explanation simpler. Refer to the article AOP: Wildcards if you do not understand what ‘*’, ‘+’ or ‘..’ mean.

You probably wonder why one would need lexical-scope based pointcuts. The answer is quite simple. When you capture calls to methods like System.out.println() that are probably also used within the aspect itself you must exclude this calls to avoid recursion.

Example:
call (* System.out.println(..)) && !within(MyAspect)
This pointcut will capture all calls to the System.out.println() method excluding the calls from within the aspect MyAspect.

I won’t write any more articles on pointcuts. But there do also exist pointcuts to capture the execution object and the arguments as well as conditional check pointcuts. I have found a comprehensive article on http://dev.eclipse.org called Pointcuts. Just refer to this article to learn about everything you have to know about pointcuts.