Copyright © 2004 Eclipse.org. All Rights Reserved

Adding a New Aspect to Your Project

 

Select File -> New -> Aspect. The new aspect wizard appears. Name your aspect “World” and click finish.

 

 

Start typing inside the body of the aspect: “pointcut greeting() : exe” and then hit “Ctrl+Space” (code completion). The editor will suggest auto-completions for the execution pointcut:

 

 

Select “execution_method  (AJDT defines a range of code completion templates for AspectJ constructs – from the window menu select preferences, and then Java -> editor -> templates to see the full list). Edit the buffer so it looks as follows and then save it:

 

/*

 * Created on 20-Aug-2003

 *

 * To change this generated comment go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

 

/**

 * To change this generated comment go to

 * Window>Preferences>Java>Code Generation>Code and Comments

 */

public aspect World {

 

      pointcut greeting() : execution(* HelloWorld.sayHello(..));

 

      after() : greeting() {

            System.out.println("Hello to you too...");

      }

 

}

 

(we should really have used after() returning advice here…)

 

Press the build button

 

 

and the outline and cross references views populate. Expand the “after(): greeting..” node in the cross references view.

 

 

You can see that this advice is affecting the HelloWorld.sayHello() method. Double clicking on the “HelloWorld.sayHello()” node takes you to the declaration of HelloWorld.sayHello().  Notice the gutter annotation in the editor margin and that the “sayHello” method in the outline view has a small orange triangle decorating it on the left hand side, showing that it is advised.

 

 

Double clicking on the “World.afterReturning()…” node in the cross references view takes you back to the advice declaration. Right-clicking on the gutter annotation brings up a context menu that also allows you to navigate to the advice.