Table of Contents

The Basics: Intro

In this exercise, you will use aspects to instrument a dummy class in order to get acquainted with the basic techniques of AspectJ: pointcuts, advices and inter-type declarations.

Set-up

  1. Create a new AspectJ Project in Eclipse
  2. Create a new class Application containing the code given below.
  3. Run the code. Right-click the Application class and select Run as…AspectJ/Java Application
public class Application {
  public int go = 10;
 
  public void start() {
    System.out.println("Starting");
    stop(go);
  }
 
  public void stop(int i) {
    System.out.println("Stopping");
  }
 
  public void exec() throws Exception {
    throw new Exception("An exception");
  }
 
  public String toString() {
    return "Application go: " + go;
  }
 
  public static void main(String[] arg) {
    Application t = new Application();
    System.out.println(t);
    t.start();
    t.stop(25);
    try { t.exec(); }
    catch(Exception e) { System.out.println(e); }
  }
}

You should see the following output on the Console view (which you can open through WindowShow ViewConsole):

Application go: 10
Starting
Stopping
Stopping
java.lang.Exception: An exception

In the following steps, you can use Eclipse's Run button (or the shortcut CTRL+F11) to quickly rerun the application using the last run configuration.


Start the Basics track at Exercise 1.

basics/intro.txt · Last modified: 26.02.2007 19:01 by bdefrain
Recent changes RSS feed Creative Commons License Donate Minima Template by Wikidesign Driven by DokuWiki