Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued.

Similar presentations


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

1 Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued

2 Aspect-Oriented Software Development (236608) 2 Today: AspectJ - Continued Additional pointcut types –cflow –cflowbelow Intertype declarations Compilation errors declarations Examples English – AspectJ phrase book (selected entries)

3 Aspect-Oriented Software Development (236608) 3 Task 1: Greeting before printing Public class Test { public static void main(String[] args) { f(); } static void f() { g(); } static void g() { System.out.println( “ message from g ” ); } Task: Before each printing operation, print a greeting message

4 Aspect-Oriented Software Development (236608) 4 English – AspectJ Phrase Book (3) “All the exceptions thrown by f()” “All the exceptions thrown while f() is executed:”

5 Aspect-Oriented Software Development (236608) 5 English – AspectJ Phrase Book (4) “All the exceptions thrown by functions of class Test” What is the relationship between: (1) pointcut except1(): cflow(call(void Test.g())); (2) pointcut except2(): cflowbelow(call(void Test.f()));

6 Aspect-Oriented Software Development (236608) 6 Task 1: Proposed solution-1

7 Aspect-Oriented Software Development (236608) 7 Task 1: Proposed solution-2

8 Aspect-Oriented Software Development (236608) 8 Cflow Pointcuts Combination P Q cflow(P) cflow(Q)cflow(P) && cflow(Q) P && Q cflow(P && Q)

9 Aspect-Oriented Software Development (236608) 9 Example Class: Point - reminder 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; } }

10 Aspect-Oriented Software Development (236608) 10 Task 2: Named Points Task2: Every point should have a name, and each time the point is moved, the user should get a message with the name of the point and its new coordinates. Pontcut = ?

11 Aspect-Oriented Software Development (236608) 11 Task 2 – contd. Advice = ? Add the “name” field to Point

12 Aspect-Oriented Software Development (236608) 12 Task 2 – version2. Another way to add the “name” field to Point Assume there is a class NamedObject in a base system: public class NamedObject { private String name; … public NamedObject() {this.name = "";} … public void setName(String newName) {this.name = newName;} public String getName() {return this.name;} }

13 Aspect-Oriented Software Development (236608) 13 Task 2 – contd. Advice = ? – contd. Advice type = ? The advice:

14 Aspect-Oriented Software Development (236608) 14 Task 2 – contd. Is that enough to add the name?

15 Aspect-Oriented Software Development (236608) 15 Task 2-A Pointcut = ? (reminder) Advice = ?

16 Aspect-Oriented Software Development (236608) 16 Task 2-A – contd. Name generation: Advice as a whole:

17 Aspect-Oriented Software Development (236608) 17 Task 3: Contract Enforcement Example Constraint : only the factory methods can add an element to the registry of figure elements. Meaning: ensures that no figure element is added to the registry more than once. Implementation:

18 Aspect-Oriented Software Development (236608) 18 More to Inter-type Declarations declare error: Pointcut: String; declare warning: Pointcut: String; New powerful types of compilation warnings and errors! Syntax: For the contract enforcement example: For example, to identify method calls that should not exist in a correct program Attention! Only static information will be used at the join-points!

19 Aspect-Oriented Software Development (236608) 19 More to AspectJ: Subtypes at pointcuts Example base classes: public class NamedObject { …} public class NamedFigure extends NamedObject { …} Example aspect: public aspect NamedObjectsObserver { …}

20 Aspect-Oriented Software Development (236608) 20 Subtypes at pointcuts – contd. class public aspect NamedObjectsObserver { pointcut publicCalled1(): call(public * NamedObject.*(..)); pointcut publicCalled2(): call(public * NamedObject+.*(..)); pointcut publicCalled3(): call(public * (NamedObject+ && !NamedObject).*(..)); after() : publicCalled1() { System.out.println("Public (1)"+thisJoinPoint); } after() : publicCalled2() { System.out.println("Public (2)"+thisJoinPoint); } after() : publicCalled3() { System.out.println("Public (3)"+thisJoinPoint); } }


Download ppt "Aspect-Oriented Software Development (AOSD) Tutorial #3 AspectJ - continued."

Similar presentations


Ads by Google