Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics.

Similar presentations


Presentation on theme: "Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics."— Presentation transcript:

1 Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics

2 Aspect-Oriented Software Development (236608) 2 Today: Getting started with AspectJ Advice types Pointcuts –Primitive pointcuts –Pointcuts composition Examples English – AspectJ phrase book (selected entries)

3 Aspect-Oriented Software Development (236608) 3 Example Class: Point class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void MoveTo(Point p) {setX(p.x); setY(p.y); } public int getX() { return x; } public int getY() { return y; } }

4 Aspect-Oriented Software Development (236608) 4 English – AspectJ Phrase Book (1) pointcut getters(): target(Point) && (call(int getX()) || call(int getY())); Pointcut: all the calls to the “getters” of Point A short way to say the same: pointcut getters(): target(Point) && (call(get*(..))); Is this the same? pointcut getters2(Point pt): target(pt) && (call(get*(..))); The same join-points; different parameters exposed

5 Aspect-Oriented Software Development (236608) 5 English – AspectJ Phrase Book (2) What does this mean? pointcut questionPointcut(Point p1, Point p2): (target(p1) && call(void getX(int))) || (target(p2) && call(void getY(int))); Answer: empty pointcut (and compilation error)! (p1 is only bound when calling getX, and p2 is only bound when calling getY, but the pointcut picks out all of these join points and tries to bind both p1 and p2.)

6 Aspect-Oriented Software Development (236608) 6 Task 1: Positive Quarter Check Task1: After each change in our points, check whether they are in the positive quarter of the space “Positive quarter” means … ? When is the aspect called? (pointcut) - at every coordinate change What should the aspect do? (advice) - after each change, check the values of x and y of the point 0 ≤ x, y

7 Aspect-Oriented Software Development (236608) 7 Task 1 – contd.: Pointcut = ? call to public void setX(int x) call to public void setY(int y) call to public Point(int x, int y) In words: every coordinate change Where in the code? In AspectJ: (call(void set*(..))); (call(Point.new(..)));

8 Aspect-Oriented Software Development (236608) 8 Task 1 – contd.: Pointcut = ? pointcut movePoint(Point pt, int newArg): target(pt) && (call(void set*(newArg)))); pointcut createPoint(int x, int y): args(x,y) && (call(Point.new(int,int)))); Parameters? Pointcut as a whole, in AspectJ:

9 Aspect-Oriented Software Development (236608) 9 Task 1 – contd.: Advice = ? Advice type: after / after returning / after throwing Advice parameters: (Point pt, int newArg) ? (int x, int y) ? Both! => Need 2 pieces of advice!

10 Aspect-Oriented Software Development (236608) 10 Task 1: Advice = ? (In AspectJ) after(Point pt, int newArg) returning(): movePoint(pt, newArg) { if( newArg < 0) System.out.println( “…” ); } after(int x, int y) returning(): createPoint(x, y) { if( x < 0 || y < 0) System.out.println( “…” ); }

11 Aspect-Oriented Software Development (236608) 11 Task 2: Positive Quarter Alert Task2: Alert the user before each change that will result in points out of the positive quarter of the space Pointcut = ? The same as before!

12 Aspect-Oriented Software Development (236608) 12 Task 2 – contd. Advice = ? The only change: “after”  “before” before(Point pt, int newArg) : movePoint(pt, newArg) { if( newArg < 0) System.out.println( “…” ); } before(int x, int y) : createPoint(x, y) { if( x < 0 || y < 0) System.out.println( “…” ); }

13 Aspect-Oriented Software Development (236608) 13 Task 3: Enforce Positive Quarter Task3: Make sure that all our points are in the positive quarter of the space Pointcut = ? The same as before!

14 Aspect-Oriented Software Development (236608) 14 Task 3 – contd. Advice = ? Advice type: “around” void around(Point pt, int newArg): movePoint(pt, newArg) { if( newArg >= 0) proceed(pt, newArg); else System.out.println( “…” ); } Point around(int x, int y) : createPoint(x, y) { if( x < 0) { x=0; System.out.println( “…” );} if( y < 0) { y=0; System.out.println( “…” );} proceed(x,y); }

15 Aspect-Oriented Software Development (236608) 15 Call vs. Execute – contd. call(void m()) && withincode(void m()) = ? –directly recursive calls execution(void m()) && withincode(void m()) = ? –the same as execution(void m()) Enclosing code at a call join-point = ? –That of the call site. Enclosing code at an execution join point = ? –The method itself (as the program is already executing the method)

16 Aspect-Oriented Software Development (236608) 16 Execution vs. Withincode withincode(void m()) : –Matches all the join-points in the body of the function m() –Evaluated statically (by examining the code of m()) execution(void m()) : –Matches the beginning of the execution of the function m() for “before” advice, and the end of the execution of m() – for “after” advice –Evaluated dynamically, when the system is running –“Beginning of execution of m()” = the moment the execution enters m(); “End of the execution of m()” = the moment before the execution leaves m() –Does not match join-points inside the body of the function, except for the two points mentioned above (and does not match join-points in functions called by m()…)

17 Aspect-Oriented Software Development (236608) 17 More details… For more details please see the AspectJ Programming Guide, at: http://www.eclipse.org/aspectj/doc/released/ progguide/index.html To be continued at the next tutorial…


Download ppt "Aspect-Oriented Software Development (AOSD) Tutorial #2 AspectJ Basics."

Similar presentations


Ads by Google