Stoppt die Vorratsdatenspeicherung! Jetzt klicken & handeln!Willst du auch bei der Aktion teilnehmen? Hier findest du alle relevanten Infos und Materialien:
 

First steps in AspectJ 5

Mit Eclipse 3.3 ist AspectJ direkt in der Eclipse Distribution enthalten. Daher gestaltet sich gerade die Installation extrem einfach. In der Europa Discovery site findet sich AspectJ unter Programming languages.

Nach der Installation und einem Neustart können wir auch gleich in Eclipse ein AspectJ Projekt anlegen. AspectJ ist bei meinem Test leider erst Java 1.5 kompatibel, wesswegen man bei Eclipse 3.3 entweder im Projekt oder im workspace das JDK auf 1.5 ändern muss, da Eclipse 3.3 das JDK 1.6 standardtgemäß nutzt.

Im Kern ist dies ein ganz normales Java Projekt das ich “HelloAspectJ” genannt habe und in dem ich folgende einfache Testklasse abgelegt habe:

package com.example.helloaspectj;

public class Main {

	public static void main(String[] args) {
		new Main().run(args);
	}

	private void run(String[] args) {
		System.out.println("This ist the run() Method, saying: Hello AspectJ");
	}

}

Wenn wir diese kleine Anwendung ganz normal starten erhalten wir die Ausgabe die wir erwarten würden:

This ist the run() Method, saying: Hello AspectJ

Als nächstes legen wir uns noch einen Ersten Aspekt an:

package com.example.helloaspectj;

public aspect FirstAspect {
	pointcut justAnPointcut() :
		 within(Main);
  	
	before(): justAnPointcut() {
  	    System.out.println("before Pointcut \t"+thisJoinPoint);
	}
	after(): justAnPointcut() {
  	    System.out.println("after Pointcut \t"+thisJoinPoint);
	}
}

Wenn wir nun die Main Klasse als “AspectJ” starten ändert sich die Ausgabe:

before Pointcut 	staticinitialization(com.example.helloaspectj.Main.<clinit>)
after Pointcut 	staticinitialization(com.example.helloaspectj.Main.<clinit>)
before Pointcut 	execution(void com.example.helloaspectj.Main.main(String[]))
before Pointcut 	call(com.example.helloaspectj.Main())
before Pointcut 	preinitialization(com.example.helloaspectj.Main())
after Pointcut 	preinitialization(com.example.helloaspectj.Main())
before Pointcut 	initialization(com.example.helloaspectj.Main())
before Pointcut 	execution(com.example.helloaspectj.Main())
after Pointcut 	execution(com.example.helloaspectj.Main())
after Pointcut 	initialization(com.example.helloaspectj.Main())
after Pointcut 	call(com.example.helloaspectj.Main())
before Pointcut 	call(void com.example.helloaspectj.Main.run(String[]))
before Pointcut 	execution(void com.example.helloaspectj.Main.run(String[]))
before Pointcut 	get(PrintStream java.lang.System.out)
after Pointcut 	get(PrintStream java.lang.System.out)
before Pointcut 	call(void java.io.PrintStream.println(String))
This ist the run() Method, saying: Hello AspectJ
after Pointcut 	call(void java.io.PrintStream.println(String))
after Pointcut 	execution(void com.example.helloaspectj.Main.run(String[]))
after Pointcut 	call(void com.example.helloaspectj.Main.run(String[]))
after Pointcut 	execution(void com.example.helloaspectj.Main.main(String[]))

Wie man sieht wird der bzw. die Aspekt(e) öfters aufgerufen. Welche Funktionen abgefangen werden, entscheidet der pointcut. In unserem Fall ist dies within(Main), was eine Filterung auf die Klasse Main erzeugt. Für den Syntax der möglichen Eingrenzungen kann man der AspectJ Dokumentation entnehmen.

 
tutorial/java/aspectj_beginners.txt · Last modified: 2007/10/08 08:53 by rainerw
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki