AspectJ Syntax Basics
description
Transcript of AspectJ Syntax Basics
![Page 1: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/1.jpg)
Com S 362: Object-Oriented Analysis and Design
AspectJ Syntax Basics
![Page 2: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/2.jpg)
2 Com S 362: Object-Oriented Analysis and Design
2
Com S 362: Object-Oriented Analysis and Design
Identify Join Points
1 public class Bit {
2 bool value;
3 void Set() {
4 value = true;
5 }
6 void Clear() {
7 value = false;
8 }
9 void Toggle {
10 if(Get()) Set();
11 else clear();
12 }
13 void Get() {
14 return value;
15 }
16 }
![Page 3: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/3.jpg)
3 Com S 362: Object-Oriented Analysis and Design
3
Com S 362: Object-Oriented Analysis and Design
Pointcuts Named
pointcut AllCall(): call( * *(..));
Anonymous
before(): call( * *(..)) { … }
![Page 4: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/4.jpg)
4 Com S 362: Object-Oriented Analysis and Design
4
Com S 362: Object-Oriented Analysis and Design
Named Pointcut Syntax
[access specifier] pointcut pointcut-
name([args]): pointcut definition
![Page 5: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/5.jpg)
5 Com S 362: Object-Oriented Analysis and Design
5
Com S 362: Object-Oriented Analysis and Design
Why Named Pointcuts? Pointcut Reuse
Separation of two specifications:
Where additional behavior is to be applied?
What is the additional behavior?
Late Binding of Design Decisions
![Page 6: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/6.jpg)
6 Com S 362: Object-Oriented Analysis and Design
6
Com S 362: Object-Oriented Analysis and Design
Pointcut Reuse & Separationpublic pointcut StringArg(String s):
execution( * *(..)) && args(s);
around(String s): StringArg(s) { /* Omit execution if null argument */
}around(String s): StringArg(s) { /* Omit execution if not valid XML */
}
![Page 7: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/7.jpg)
7 Com S 362: Object-Oriented Analysis and Design
7
Com S 362: Object-Oriented Analysis and Design
Late Bindingabstract aspect Lock { pointcut CriticalSections(); before(): CriticalSections() { /* Acquire lock */ } after(): CriticalSections() { /* Release lock */ } }
![Page 8: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/8.jpg)
8 Com S 362: Object-Oriented Analysis and Design
8
Com S 362: Object-Oriented Analysis and Design
Late Binding …
aspect BufferLock extends Lock {
pointcut CriticalSections():
call(* * Buffer.Write(..));
}
![Page 9: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/9.jpg)
9 Com S 362: Object-Oriented Analysis and Design
9
Com S 362: Object-Oriented Analysis and Design
Poincut composition pointcut1 && pointcut2
pointcut1 || pointcut2
! pointcut
![Page 10: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/10.jpg)
10 Com S 362: Object-Oriented Analysis and Design
10
Com S 362: Object-Oriented Analysis and Design
Signatures Type Signatures
Method Signatures
Field Signatures
![Page 11: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/11.jpg)
11 Com S 362: Object-Oriented Analysis and Design
11
Com S 362: Object-Oriented Analysis and Design
Example Type Signature
javax..*Model+
Any number of characters including period
Any number of characters excluding the period
Any sub-type of a given type
![Page 12: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/12.jpg)
12 Com S 362: Object-Oriented Analysis and Design
12
Com S 362: Object-Oriented Analysis and Design
Exercise 1 Write down the type signature to pick out only
the sub-types of all the types in the javax package or its direct and indirect subpackages that have a name ending in Model.
![Page 13: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/13.jpg)
13 Com S 362: Object-Oriented Analysis and Design
13
Com S 362: Object-Oriented Analysis and Design
Example Method Signature
* javax..*.add*Listener(EventListener+)
![Page 14: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/14.jpg)
14 Com S 362: Object-Oriented Analysis and Design
14
Com S 362: Object-Oriented Analysis and Design
Example Field Signature
!public static * banking..*.*
![Page 15: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/15.jpg)
15 Com S 362: Object-Oriented Analysis and Design
15
Com S 362: Object-Oriented Analysis and Design
Cflow and Cflowbelow Cflow (Pointcut) - Captures all join points in
the control flow of the join points matched by the specified pointcut, including the join points themselves.
Cflowbelow(Pointcut) – Excludes the matching join points, matches everything below.
![Page 16: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/16.jpg)
16 Com S 362: Object-Oriented Analysis and Design
16
Com S 362: Object-Oriented Analysis and Design
cflow( execution Account.debit(..)) 1 void debit(int amount) {
2 if( this.getBalance() > amount) {
3 this.setBalance( this.getBalance() – amount);
4 }
5 int getBalance() {
6 return db.Query(name, “balance”);
7 }
8 int setBalance(int balance) {
9 db.Update(name, “balance”, balance);
10 }
![Page 17: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/17.jpg)
17 Com S 362: Object-Oriented Analysis and Design
17
Com S 362: Object-Oriented Analysis and Design
Lexical Pointcuts within( type pattern )
withincode( method or constructor pattern)
![Page 18: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/18.jpg)
18 Com S 362: Object-Oriented Analysis and Design
18
Com S 362: Object-Oriented Analysis and Design
Identify matches within(Bit)
1 public class Bit {
2 bool value;
3 void Set() {
4 value = true;
5 }
6 void Clear() {
7 value = false;
8 }
9 void Toggle {
10 if(Get()) Set();
11 else clear();
12 }
13 void Get() {
14 return value;
15 }
16 }
![Page 19: AspectJ Syntax Basics](https://reader036.fdocuments.net/reader036/viewer/2022083006/56813e05550346895da7e215/html5/thumbnails/19.jpg)
19 Com S 362: Object-Oriented Analysis and Design
19
Com S 362: Object-Oriented Analysis and Design
withincode(* * Bit.*et(..))
1 public class Bit {
2 bool value;
3 void Set() {
4 value = true;
5 }
6 void Clear() {
7 value = false;
8 }
9 void Toggle {
10 if(Get()) Set();
11 else clear();
12 }
13 bool Get() {
14 return value;
15 }
16 }