Skip to main content

AspectJ Language

Anatomy

 1 aspect FaultHandler {
2
3 private boolean Server.disabled = false;
4
5 private void reportFault() {
6 System.out.println("Failure! Please fix it.");
7 }
8
9 public static void fixServer(Server s) {
10 s.disabled = false;
11 }
12
13 pointcut services(Server s): target(s) && call(public * *(..));
14
15 before(Server s): services(s) {
16 if (s.disabled) throw new DisabledException();
17 }
18
19 after(Server s) throwing (FaultException e): services(s) {
20 s.disabled = true;
21 reportFault();
22 }
23 }
TermDefinition
PointcutPicks out join points (points in execution of prg)
AdviceBrings tgt pointcut and body of code to define aspect implementation

Examples

ScenarioSyntax
particular method body executionexecution(void Point.setX(int))
method is calledcall(void Point.setX(int))
exception handler executedhandler(ArrayOutOfBoundsException)
object executing is of someTypethis(SomeType)
executing code belongs to SomeClasswithin(SomeClass)
join point in control flow of call to a Test's no-argument maincflow(call(void Test.main()))

Composing pointcuts

DescriptionSyntax
Any call to int method on Point instance regardless of nametarget(Point) && call(int *())
Any call to any method where the call is made from within Point or Linecall(* *(..)) && (within(Line) || within(Point))
execution of any constructor taking exactly one int argument regardless of where call is fromwithin(*) && execution(*.new(int))
any method call to int method when executing object is any type except Point!this(Point) && call(int *(..))

Wildcards

DescriptionSyntax
Execution of any method regardless of return / parameter typeexecution(* *(..))
Call to any set method regardless of return / parameter typecall(* set(..))

Based on Constructors / Modifiers

DescriptionSyntax
call to any public methodcall(public * *(..))
execution of non-static methodexecution(!static * *(..))`
any execution of public non static methodexecution(public !static * *(..))
any call to method in MyInterface's signaturecall(* MyInterface.*(..))

Call Vs Execution

note

When methods and constructors run, there are 2 times associated - when they are called / when they actually execute

call pointcutsexecution pointcuts
enclosing code is that of call site
i.e. call(void m()) && withincode(void m()) will only capture directly recursive calls
enclosing code is the method itself
i.e. execution(void m()) && withincode(void m()) == execution(void m())
does not capture super calls to non static methods
use when targeting when a particular signature is calleduse when actual peice of code runs

Pointcut Compositions

  • Use primitive pointcuts to build more powerful pointcuts