Show Menu
Cheatography

AspectJ Cheat Sheet by

AspectJ for Fortgeschrittene Java Programmierung, THI

Termin­ology

Aspect
Modula­riz­ation unit, compar­eable with Class
Introd­uction
introduce new method
Joinpoint
Condituion to trigger some action
Pointcut
Collection of Joinpoints
Advice
Behavior introduced at Pointcut
 
Weaving
Aspects get waved into the result bytecode

Aspect

public aspect LogAspect { ... }
Holds Introd­uct­ions, Advices and Pointcuts

Introd­uction

public aspect HashableComplex {
  // introduce an interface
  declare parents: Complex implements Comparable;

  // don’t forget the class name!
  public int Complex.compareTo( Object o ) {
    ...
  }

  @Override
  public boolean Complex.equals(Object obj) {
    ...
  }
}
Can Introduce new Methods and Declar­ations

Advice

before
after
altern­ative: after return­ing, after throwing
around
use proce­ed() to execute wrapped joinpoint

Pointcut Args

target(p)
bind cut target
args(vals)
bind Pointcut arguments
pointcut setter­(Point p, int newval):
 ­ ­ta­rget(p) &&ar­gs(­new­val)
 

Joinpoint

package hello;
public aspect WorldAspect{
  public pointcut mainOperation():
    execution (void hello.World.main(String[]));
    //execution (void.(*));

  before(): mainOperation() {
    println( "Hello aspect world!" );
  }

  after(): mainOperation() {
    println( "Bye aspect world!" );
  }
}

Pointcut Syntax

Wild­cards
*
any charac­ters, but not "."
..
any charac­ters, also "."
+
Any Subclass / Interface
Logi­cal
||, &­&, !

Primitive Pointcuts

call(void Foo.m(­int))
call of a Method
execu­tio­n(­Foo.(..) throws IOExce­ption)
execution of any Method throwing an Exception
call Foo.ne­w(..)
any Constr­uctors of Foo
initi­ali­zat­ion­(Fo­o.n­ew(­int))
initia­lis­ation by a special constr­uctor
handl­er(­IOE­xce­pti­on+)
handling of any IOExce­ption
get(int Foo.bar) or set(int Foo.bar)
withi­n(Foo)
limit to foo class
* call vs execu­tion: think of "­cal­ler­" vs "­exe­cuting Object­"

thisJo­inPoint

getAr­gs(): Object[] args
getSi­gna­ture()
getSo­urc­eLo­cat­ion()
thisJ­oin­Poi­ntS­tat­icPart equals jp.ge­tSt­ati­cPa­rt()
thisJ­oin­Point is available inside the Advice
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by cs8898

          Java Annotation Cheat Sheet
          JUnit 5 Cheat Sheet