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 returning, after throwing
around
use proceed() to execute wrapped joinpoint

Pointcut Args

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

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

Wildcards
*
any charac­ters, but not "."
..
any charac­ters, also "."
+
Any Subclass / Interface
Logical
||, &&, !

Primitive Pointcuts

call(void Foo.m(­int))
call of a Method
execution(Foo.(..) throws IOExce­ption)
execution of any Method throwing an Exception
call Foo.ne­w(..)
any Constr­uctors of Foo
initia­liz­ati­on(­Foo.ne­w(int))
initia­lis­ation by a special constr­uctor
handle­r(I­OEx­cep­tion+)
handling of any IOExce­ption
get(int Foo.bar) or set(int Foo.bar)
within­(Foo)
limit to foo class
* call vs execution: think of "­cal­ler­" vs "­exe­cuting Object­"

thisJo­inPoint

getArgs(): Object[] args
getSig­nat­ure()
getSou­rce­Loc­ation()
thisJo­inP­oin­tSt­ati­cPart equals jp.get­Sta­tic­Part()
thisJo­inPoint 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