Inhaltsverzeichnis
- 1- Die Vorstellung
- 2- Die Anforderungen vor dem Beginn
- 3- Die Klasse zur Teilnahme an die Beispiele
- 4- AspectJ Projekt erstellen
- 5- Zum Beispiel
- 6- Die grundlegende Definition im AspectJ
- 7- Zum Beispiel: das grundlegende AspectJ
-
- 7.1- Die Regelung zur Bezeichnung von class, method, * và ..
- 7.2- Target & args
- 7.3- Der Operator && ||
- 7.4- Der Feld (Field) im AspectJ
- 7.5- Inter-type declarations)
- 7.6- Tracking
- 7.7- Cflow
- 7.8- Cflowbelow
Die Anleitung zu Java Aspect Oriented Programming (AOP)
View more Tutorials:
- 1- Die Vorstellung
- 2- Die Anforderungen vor dem Beginn
- 3- Die Klasse zur Teilnahme an die Beispiele
- 4- AspectJ Projekt erstellen
- 5- Zum Beispiel
- 6- Die grundlegende Definition im AspectJ
- 7- Zum Beispiel: das grundlegende AspectJ
-
- 7.1- Die Regelung zur Bezeichnung von class, method, * và ..
- 7.2- Target & args
- 7.3- Der Operator && ||
- 7.4- Der Feld (Field) im AspectJ
- 7.5- Inter-type declarations)
- 7.6- Tracking
- 7.7- Cflow
- 7.8- Cflowbelow
-
....
-
1- Die Vorstellung
-
Das Dokument wird auf der Basic von ... geschrieben
-
-
Eclipse 4.4 (LUNA)
-
AspectJ 1.8.2
-
-
2- Die Anforderungen vor dem Beginn
-
Sie sollen die Entwicklungsinstrument AspectJ in Eclipse installieren, Sie können die Hinweise sehen bei ...
-
3- Die Klasse zur Teilnahme an die Beispiele
-
Im Dokument benutze ich einige Klasse, die an die Beispiel AspectJ teilnehmen
-
- Box
- FigureElement
- Group
- Line
- Point
- ShapeFigureElement
- SlotfulPoint
-
Die Quelle von der Class kommt aus ...
-
-
Box.java
package figures; import java.awt.Rectangle; import java.awt.Shape; public class Box extends ShapeFigureElement { private Point _p0; private Point _p1; private Point _p2; private Point _p3; public Box(int x0, int y0, int width, int height) { _p0 = new Point(x0, y0); _p1 = new Point(x0 + width, y0); _p2 = new Point(x0 + width, y0 + height); _p3 = new Point(x0, y0 + height); } public Point getP0() { return _p0; } public Point getP1() { return _p1; } public Point getP2() { return _p2; } public Point getP3() { return _p3; } @Override public void move(int dx, int dy) { _p0.move(dx, dy); _p1.move(dx, dy); _p2.move(dx, dy); _p3.move(dx, dy); } public void checkBoxness() { if ((_p0.getX() == _p3.getX()) && (_p1.getX() == _p2.getX()) && (_p0.getY() == _p1.getY()) && (_p2.getY() == _p3.getY())) return; throw new IllegalStateException("This is not a square."); } @Override public String toString() { return "Box(" + _p0 + ", " + _p1 + ", " + _p2 + ", " + _p3 + ")"; } @Override public Shape getShape() { return new Rectangle(getP1().getX(), getP1().getY(), getP3().getX() - getP1().getX(), getP3().getY() - getP1().getY()); } }
-
FigureElement.java
package figures; import java.awt.*; import java.awt.geom.*; public interface FigureElement { public static final Rectangle MAX_BOUNDS = new Rectangle(0, 0, 500, 500); public abstract void move(int dx, int dy); public abstract Rectangle getBounds(); public abstract boolean contains(Point2D p); public abstract void paint(Graphics2D g2); }
-
Group.java
package figures; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Group implements FigureElement { private Collection _members; private String _identifier; public Group(FigureElement first) { this._members = new ArrayList(); add(first); } public void add(FigureElement fe) { _members.add(fe); } public Iterator members() { return _members.iterator(); } public void move(int dx, int dy) { for (Iterator i = _members.iterator(); i.hasNext();) { FigureElement fe = (FigureElement) i.next(); fe.move(dx, dy); } } public void setIdentifier(String identifier) { _identifier = identifier; } @Override public String toString() { if (_identifier != null) { return _identifier; } StringBuffer buf = new StringBuffer("Group("); for (Iterator i = _members.iterator(); i.hasNext();) { buf.append(i.next().toString()); if (i.hasNext()) { buf.append(", "); } } buf.append(")"); return buf.toString(); } public Rectangle getBounds() { Rectangle previous = null; for (Iterator i = _members.iterator(); i.hasNext();) { FigureElement fe = (FigureElement) i.next(); Rectangle rect = fe.getBounds(); if (previous != null) { previous = previous.union(rect); } else { previous = rect; } } return previous; } public boolean contains(Point2D p) { for (Iterator i = _members.iterator(); i.hasNext();) { FigureElement fe = (FigureElement) i.next(); if (fe.contains(p)) return true; } return false; } public void paint(Graphics2D g2) { for (Iterator i = _members.iterator(); i.hasNext();) { FigureElement fe = (FigureElement) i.next(); fe.paint(g2); } } public int size() { return _members.size(); } }
-
Line.java
package figures; import java.awt.*; import java.awt.geom.*; public class Line extends ShapeFigureElement { private Point _p1; private Point _p2; public Line(Point p1, Point p2) { _p1 = p1; _p2 = p2; } public Point getP1() { return _p1; } public Point getP2() { return _p2; } @Override public void move(int dx, int dy) { _p1.move(dx, dy); _p2.move(dx, dy); } @Override public String toString() { return "Line(" + _p1 + ", " + _p2 + ")"; } /** * Used to determine if this line {@link contains(Point2D)} a point. */ final static int THRESHHOLD = 5; /** * Returns <code>true</code> if the point segment distance is less than * {@link THRESHHOLD}. */ @Override public boolean contains(Point2D p) { return getLine2D().ptLineDist(p) < THRESHHOLD; } private Line2D getLine2D() { return new Line2D.Float((float) getP1().getX(), (float) getP1().getY(), (float) getP2().getX(), (float) getP2().getY()); } public Shape getShape() { return getLine2D(); } }
-
Point.java
package figures; import java.awt.*; import java.awt.geom.*; public class Point extends ShapeFigureElement { private int _x; private int _y; public Point(int x, int y) { _x = x; _y = y; } public int getX() { return _x; } public int getY() { return _y; } public void setX(int x) { _x = x; } public void setY(int y) { _y = y; } @Override public void move(int dx, int dy) { _x += dx; _y += dy; } @Override public String toString() { return "Point(" + _x + ", " + _y + ")"; } /** The height of displayed {@link Point}s. */ private final static int HEIGHT = 10; /** The width of displayed {@link Point}s. -- same as {@link HEIGHT}. */ private final static int WIDTH = Point.HEIGHT; @Override public Shape getShape() { return new Ellipse2D.Float((float) getX() - Point.WIDTH / 2, (float) getY() - Point.HEIGHT / 2, (float) Point.HEIGHT, (float) Point.WIDTH); } }
-
ShapeFigureElement.java
package figures; import java.awt.*; import java.awt.geom.*; public abstract class ShapeFigureElement implements FigureElement { @Override public abstract void move(int dx, int dy); public abstract Shape getShape(); @Override public Rectangle getBounds() { return getShape().getBounds(); } @Override public boolean contains(Point2D p) { return getShape().contains(p); } public Color getLineColor() { return Color.black; } public Color getFillColor() { return Color.red; } @Override public final void paint(Graphics2D g2) { Shape shape = getShape(); g2.setPaint(getFillColor()); g2.fill(shape); g2.setPaint(getLineColor()); g2.draw(shape); } }
-
SlothfulPoint.java
package figures; import java.awt.*; import java.awt.geom.*; /** * This class makes mistakes to be caught by invariant checkers. */ public class SlothfulPoint extends ShapeFigureElement { private int _x; private int _y; public SlothfulPoint(int x, int y) { } public int getX() { return _x; } public int getY() { return _y; } public void setX(int x) { } public void setY(int y) { } @Override public void move(int dx, int dy) { System.out.println("Slothful moving"); } @Override public String toString() { return "SlothfulPoint"; } @Override public Shape getShape() { return new Ellipse2D.Float((float) _x, (float) _y, 1.0f, 1.0f); } }
-
4- AspectJ Projekt erstellen
-
Zuerst erstellen Sie ein normales Project mit dem Name von AspectJTutorial.
-
-
Klicken Sie die Rechtmaustaste in Project, wählen Sie Configure/ Convert to AspectJ Project.
-
-
Convert ist erfolgreich
-
-
5- Zum Beispiel
-
Zuerst machen wir ein Beispiel bevor wir die Begriffen studieren
-
Erstellen Sie die class HelloAspectJDemo:
-
HelloAspectJDemo.java
package org.o7planning.tutorial.aspectj.helloaspectj; public class HelloAspectJDemo { public static void sayHello() { System.out.println("Hello"); } public static void greeting() { String name = new String("John"); sayHello(); System.out.print(name); } public static void main(String[] args) { sayHello(); System.out.println("--------"); sayHello(); System.out.println("--------"); greeting(); } }
-
Eine solche Class ist absolut nicht erwähnenswert. Aber eine Frage wird gestellt: Sie möchten, das Programm macht etwas gleich nach oder vor der Aufrufung von der Methode sayHello(), zum Beispiel auf dem Bildschirm eine Nachricht anzeigen. Nach der traditionellen Maßnahme fügen Sie einen Befehl der Anzeige der Nachricht vor der Aufrufung von sayHello() ein
-
-
AspectJ ist eine Aspekt Programmierung (Aspect programming). Sie gibt Ihnen eine Maßnahme zur Lösung der Frage. Und AspectJ kann natürlich mehr tun. Erinnern Sie sich, dass das nur Beispiel von HelloWorld. ist
-
- File/New/Other..
-
-
-
Eine File mit dem Name von HelloAspectJ.aj wird erstellt. Ihre Konstruktur ist so ähnlich wie eine Class
-
AspectJ der vorherigen Version benutzt die Annotation zu bezeichnen. Die jetzigen Version führt die file *.aj ein. Im Dokument benutze ich keine Annotation, denn sie ist nicht so klar wie die Benutzung von der file *.aj. Außerdem hat die File *.aj die gleiche Grammatik einer Class, eclipse informierte Ihnen, welche Grammatik false ist.
-
-
Koriggieren Sie die Code von HelloAspectJ.aj:
-
HelloAspectJ.aj
package org.o7planning.tutorial.aspectj.helloaspectj; public aspect HelloAspectJ { // Define a Pointcut is // collection of JoinPoint call sayHello of class HelloAspectJDemo. pointcut callSayHello(): call(* HelloAspectJDemo.sayHello()); before() : callSayHello() { System.out.println("Before call sayHello"); } after() : callSayHello() { System.out.println("After call sayHello"); } }
-
AspectJ hat einen kleinen Unterschied mit einer normalen Class. es hat viele Schlüsselwörte. Und die File mit der Ende von *.aj, nicht *.java. Einige Wörte von AspectJ:
-
- aspect pointcut privileged call execution
- initialization preinitialization handler get set
- staticinitialization target args within withincode
- cflow cflowbelow annotation before after around
- proceed throwing returning adviceexecution declare
- parents warning error soft precedence thisJoinPoint
- thisJoinPointStaticPart thisEnclosingJoinPointStaticPart
- issingleton perthis pertarget percflow percflowbelow
- pertypewithin lock unlock thisAspectInstance
-
-
Rückkehr zum Beispiel HelloWorld. Jetzt starten wir die Class HelloAspectJDemo. Und erhalten wir die Ergebnis:
-
-
AspectJ ist sehr stark. Im oben Beispiel: Wenn Sie die Code auf HelloAspectJ.aj etwas korrigieren, können sie auf die Class von der Aufrufung der Funktion sayHello().wirken
-
// Define a pointcut is a Collection of JoinPoint // call sayHello() of HelloAspectJDemo pointcut callSayHello(): call(* HelloAspectJDemo.sayHello()); // Change to: // This means that everywhere (class, AspectJ) called sayHello() pointcut callSayHello(): call(* *.sayHello());
-
6- Die grundlegende Definition im AspectJ
-
Im AspectJ sollen Sie einigen Begriffen unterscheiden:
-
- Advice
- Pointcut
- JoinPoint
-
-
6.1- JoinPoint
-
JoinPoint ist eine Punkt, die in der Datenstrom des Programms klar bestimmt wird
-
- Wir möchten ein Code (“advice”) bei jeder Begegnung mit JoinPont implementieren
- Wir möchten die Code durch die Anzeige “Das ist die JointPoint” unordentlich machen
- d.h schreiben wir kein Code in dieser Position schreiben.
- AspectJ bietet eine Syntax um die JointPoint vom außen zu zeigen.
- markieren Sie und dann implementieren Sie die vom außen geschriebene Code
-
JoinPoint ist eine Punkt in dem Strom des Programm "Wo etwas passieren wird". Etwas hier kann sein ...
-
- Wo die Method aufgeruft wird
- Wo eine Ausnahme geworfen wird
- Wo eine Variable zugegriffen wird (accessed)
- Wo ein Objekt erstellt wird
- Wo ein Object zugewiesen wird (referenz)
-
Deshalb ist JoinPoint sehr umfangreich. Der Ort, wo Sie ein Object erstellen, kann als einen JoinPoint. gesehen wird
-
Rückkehr zum Beispiel von HelloWorld bestimmen wir einige JoinPoint:
-
-
6.2- PointCut
-
Die Definition von Pointcut fasst eine rechte Seite und eine linke Seite um, die durch das Komma getrennt werden
-
- Die linke Seite schließt den Name von pointcut und die Parameter pointcut ein (Zum Beispiel: die verfügbaren Daten wenn die Events passieren)
- Die rechte Seite enthaltet selbst Pointcut
-
Zum Beispiel
-
pointcut callSayHello(): call(* HelloAspectJDemo.sayHello());
-
- Der Name von Pointcut ist callSayHello
- Der Pointcut hat keinen Parameter
- Der Pointcut ist call(* HelloAspectJDemo.sayHello())
- Der Pointcut wird erwähnt irgendwann die Method HelloAspectJDemo.sayHello() aufgeruft wird
-
-
6.3- Advice
-
Rückkehr zum Beispiel von HelloWorld, Wir haben 2 advice
-
- before()
- after()
-
-
Advice (die Beratung oder der Rat) bestimmt eine Handlung. Die Code von einer Advice läuft in jedem Joint point vom Pointcut. Die Laufenweise von der Code ist abhängig von der Advice (Die Beratung)
-
AspectJ unterstüzt 3 Advice. Die Advice bestimmt, wie sie mit den JoinPoint auswirken. Deshalb teilt AspectJ ins vor JointPoint (before), nach Jointpoint (after) und um JoinPoint herum (around).
-
Während "before Advice" kein Problem hat, hat "after Advice" 3 Situationen: Nach der Erledigung eines JoinPoint, Nach dem Werfen einer Ausnahem oder Nach der Durchührung einer der 2 Situation, ermöglicht AspectJ "after Advice" für die 2 Situation
-
Sie können die Beispiel von Advice sehen um mehr zu verstehen
-
HelloAspectJ2.aj
package org.o7planning.tutorial.aspectj.helloaspectj; public aspect HelloAspectJ2 { pointcut callSayHello(): call(* HelloAspectJDemo.sayHello()); // Advice "after returning". after() returning (Object retObj): callSayHello() { System.out.println("Returned normally with " + retObj); } // Advice "after throwing". after() throwing (Exception e): callSayHello() { System.out.println("Threw an exception: " + e); } // Advice "after returning" + "after throwing". after() : callSayHello() { System.out.println("Returned or threw an Exception"); } }
-
Die Advice Type "after returning" kann die Rückgegebene Wert von der Method nicht beachten, da können Sie schreiben ...
-
// Advice "after returning". // care about the returning value of method after() returning (Object retObj): callSayHello() { System.out.println("Returned normally with " + retObj); } // Advice "after returning" not care about the returning value of method after() returning(): callSayHello() { System.out.println("Returned normally"); } // or // Advice "after returning" not care about the returning value of method after() returning : callSayHello() { System.out.println("Returned normally"); }
-
-
7- Zum Beispiel: das grundlegende AspectJ
-
Vor der höheren Erforschung sehen wir die grundlegenden Beispiele und die Grundsätze im AspectJ. Wenn Sie diese Regelung nicht verstehen, können Sie die AspectJ.nicht verstehen
-
-
7.1- Die Regelung zur Bezeichnung von class, method, * và ..
-
Wenn Sie im AspectJ eine Klasse, eine Methode in der Pointcut Erklärung sprechen möchten, sollen Sie ihren vollen Name schreiben. Zum Beispiel
-
- figures.Point
- figures.Point.setX(int)
-
Sie können kurz schreiben falls die class und AspectJ in einer Package liegen. Zum Beispiel:
-
- Point
- Point.setX(int)
-
Sehen Sie das unten Beispiel, AspectJ01.aj und die class Point liegen in 2 unterschiedlichen Package
-
-
AspectJ01.aj
package org.o7planning.tutorial.aspectj.demo01; public aspect AspectJ01 { // Class Point and AspectJ is not the same package // so must specify the package (Required). // This pointcut definition of JoinPoints // only within the class ClassTest01 // This ClassTest01 and AspectJ same package, // so can be ignored package in within(..). pointcut callSetX() : call(void figures.Point.setX(int)) && within (ClassTest01) ; // Advice before() : callSetX() { System.out.println("Before call Point.setX(int)"); } }
-
ClassTest01.java
package org.o7planning.tutorial.aspectj.demo01; import figures.Point; public class ClassTest01 { public static void main(String[] args) { Point point = new Point(10, 200); System.out.println("---- (1) ----"); point.setX(20); System.out.println("---- (2) ----"); point.setY(100); System.out.println("---- (3) ----"); } }
-
Das Ergebnis vom Starten der Class ClassTest01:
-
-
Deshalb gibt es einigen Notizen hier
-
// 'within' used to limit the scope of the pointcut // in the case below: // Only Containing join point inside class ClassTest01. within (ClassTest01) // If you using import import figures.Point; ..... // Then Can write shorter code: pointcut callSetX() : call(void Point.setX(int)) && within (ClassTest01) ;
-
Das Zeichen * im AspectJ.
-
// Collection of JoinPoints call Point.setX(int), any package name, // and method return void pointcut callSetX() : call(void *.Point.setX(int)) ; // Collection of JoinPoints call Point.setX(int), any package name, // and method return any pointcut callSetX() : call(* *.Point.setX(int)) ; // Collection of JoinPoints call public static method setX(int) of class with package name is 'sample' // and class name have suffix Point, // and setX(int) return int pointcut callSetX() : call(public static int sample.*Point.setX(int)) ; // Using (..) to describe the method has 0 or more parameters. pointcut callSetX() : call(public static int sample.*Point.setX(..)) ;
-
7.2- Target & args
-
target: ist das Objekt zur Teilnahma an JoinPoint, Das Objekt zur Methode Aufruf (Oder das Objekt zum Feld-Zugang).
args: ist der Parameter.
Schauen Sie das Beispiel:
-
AspectJ02.aj
package org.o7planning.tutorial.aspectj.demo02; // Ready import Point class. import figures.Point; public aspect AspectJ02 { // Using target: define object on which the method is called // Using args: define args on the method is called // Using within: to restrict JoinPoint within ClassTest02 pointcut callMove(Point point, int dx, int dy) : call(* figures.Point.move(int,int)) && args(dx,dy) && target(point) && within(ClassTest02) ; before(Point point, int dx, int dy) : callMove(point, dx, dy ) { System.out.println("Before call move(" + dx + "," + dy + ")"); System.out.println(point.toString()); } }
-
ClassTest02.java
package org.o7planning.tutorial.aspectj.demo02; import figures.Point; public class ClassTest02 { public static void main(String[] args) { Point point = new Point(10, 200); System.out.println("---- (1) ----"); point.move(20, 30); System.out.println("---- (2) ----"); System.out.println(point.toString()); System.out.println("---- (3) ----"); point.setX(100); } }
-
-
Das Ergebnis der Beispieldurchführung.
-
-
7.3- Der Operator && ||
-
AspectJ03.aj
package org.o7planning.tutorial.aspectj.demo03; // Note: Must import FigureElement & Point import figures.FigureElement; import figures.Point; public aspect AspectJ03 { // pointcut: Include move actions pointcut moveAction() : ( call(void FigureElement.move(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) ) && within (ClassTest03); before() : moveAction() { System.out.println("before move"); } }
-
ClassTest03.java
package org.o7planning.tutorial.aspectj.demo03; import figures.FigureElement; import figures.Line; import figures.Point; public class ClassTest03 { public static void main(String[] args) { Point point = new Point(10, 200); System.out.println("---- (1) ----"); point.setX(20 ); System.out.println("---- (2) ----"); FigureElement line= new Line(new Point(1,1), new Point(10,10)); line.move(10, 10); System.out.println("---- (3) ----"); } }
-
Das Ergebnis der Beispieldurchführung.
-
-
7.4- Der Feld (Field) im AspectJ
-
FieldInAspectJ.aj
package org.o7planning.tutorial.aspectj.demo04; import java.io.PrintStream; public aspect FieldInAspectJ { // Field in AspectJ. PrintStream logStream = System.err; pointcut move() : call(* figures.Point.move(int,int)) && within(FieldInAspectJTest); before(): move() { logStream.println("Before Point move"); } }
-
FieldInAspectJTest.java
package org.o7planning.tutorial.aspectj.demo04; import figures.Point; public class FieldInAspectJTest { public static void main(String[] args) { Point point = new Point(10, 200); System.err.println("---- (1) ----"); point.setX(20); System.err.println("---- (2) ----"); point.move(10, 10); System.err.println("---- (3) ----"); } }
-
Das Ergebnis der Beispieldurchführung.
-
-
7.5- Inter-type declarations)
-
PointObserving.aj
package org.o7planning.tutorial.aspectj.demo05; import java.util.ArrayList; import java.util.List; import figures.Point; public aspect PointObserving { // Class Point have no field: observers // However, it can declare here. // observers: Store the change point position. private List<Point> Point.observers = new ArrayList<Point>(); pointcut moveAction(Point point) : call(void Point.move(int,int) ) && target(point) && within(PointObservingTest); after(Point point) : moveAction(point) { System.out.println("Point moved"); // add new position point.observers.add(point); // Print the location of the point went through. System.out.println(" - "+point.observers); } public static void addObserver(Point p) { // p.observers.add(s); } public static void removeObserver(Point p) { // p.observers.remove(s); } }
-
PointObservingTest.java
package org.o7planning.tutorial.aspectj.demo05; import figures.Point; public class PointObservingTest { public static void main(String[] args) { Point point1 = new Point(100, 100); // First move point1.move(10, 10); // Second move point1.move(10, 10); System.out.println("----------------------"); Point point2 = new Point(200, 200); // First move point2.move(15, 10); // Second move point2.move(15, 10); // Third Move point2.move(25, 10); } }
-
Das Ergebnis der Beispieldurchführung.
-
-
7.6- Tracking
-
Method tracking:
-
Wir werden ein Aspect erstellen, das ein pointcut als den Ort, wo die Methode implementieren definiert und erstellen eine Advice zur Definition der implementierten Code in dieser Stelle
-
SimpleTracing.aj
package org.o7planning.tutorial.aspectj.demo06; import org.aspectj.lang.Signature; public aspect SimpleTracing { // Collection of JoinPoint call any method // And within SimpleTracingTest pointcut tracedCall() : call (* *(..)) // && !within(SimpleTracing) && within(SimpleTracingTest) ; before() : tracedCall() { Signature sig = thisJoinPointStaticPart.getSignature(); String line = "" + thisJoinPointStaticPart.getSourceLocation().getLine(); String sourceName = thisJoinPointStaticPart.getSourceLocation() .getWithinType().getCanonicalName(); // System.out.println("Call from " + sourceName + " line " + line + "\n to " + sig.getDeclaringTypeName() + "." + sig.getName() +"\n"); } }
-
SimpleTracingTest.java
package org.o7planning.tutorial.aspectj.demo06; import figures.Point; public class SimpleTracingTest { private static void testMethod1() { Point point = new Point(100, 100); point.setX(100); } private static void testMethod2() { String text = "This is text"; String s = text.substring(2); System.out.println(s); } public static void main(String[] args) { testMethod1(); testMethod2(); } }
-
Das Ergebnis der Beispieldurchführung.
-
-
7.7- Cflow
-
Das Control flow ist ein Thread der Durchführungspunkte in einem bestimmten JointPoint. cflow() und cflowbelow() werden erstellt um die anderen Joint Point wie einen Parameter zu fangen, damit wireinen Thread nach pointcut definieren können - pointcuts wird die Jointpoint in dem control flow jedes bestimmten JointPoint fangen
-
cflow() wird alle JointPoints wie call, execution, set und get field, error handlers in dem Control Flow eines konkreten JointPont fangen
-
Das Illustration folgend enthaltet die JointPoint zur Aufruf der Methode MyClass.callA().
-
-
cflow(..) benutzen:
-
-
-
Das detailierte Beispiel schauen
-
-
CflowAspectJ.aj
package org.o7planning.tutorial.aspectj.demo07; public aspect CflowAspectJ { pointcut call_cflow_callA() : cflow( call( * MyClass.callA() ) ) && within(CFlowDemo || MyClass); before() : call_cflow_callA() { System.out.println( "Join Point at: " + thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName() + " --> " + thisJoinPointStaticPart.getSourceLocation().getLine()); } }
-
MyClass.java
package org.o7planning.tutorial.aspectj.demo07; public class MyClass { public void callA() { callB(); callC(); } public void callB() { callC(); } public void callC() { } }
-
CFlowDemo.java
package org.o7planning.tutorial.aspectj.demo07; public class CFlowDemo { public static void main(String[] args) { MyClass myClass= new MyClass(); myClass.callA(); myClass.callA(); } }
-
Das Ergebnis der Durchführung der Klasse CFlowDemo:
-
-
7.8- Cflowbelow
-
Control flow is just the flow of execution of program within specific join point. cflow() and cflowbelow() constructs takes another join point as argument and allow us to define control flow based pointcuts - the pointcuts that captures all the join points in the control flow of specified join point.
-
cflowbelow() verhaltet wie cflow, aber es fangt die JointPoint als Argument nicht aber fangt die anderen JointPoint
-
Sie können mehr in cflow() sehen.
-
-