Académique Documents
Professionnel Documents
Culture Documents
■ OOP limitations
● Code redundancy
● Code tangling
▶ mix-up of multiple concerns at one place
● Code scattering
▶ a concern is scattered over many places
● Cross-cutting
▶ a concern than spans multiple units of OO modularity
scattering
06
Chapitre
Cross-Cutting
3
Action 2
Action2
Action 1
Action1
call mthod 2
definition method 2
call method 1
call method 2
▶ Meta-programming
▶ Reflection
▶ OOP paradigm
▶ OAP paradigm
■ Aspect
● Unit of modularity.
● Can contain fields and methods like a regular Java class.
■ Joint Point
● Places where crosscutting concerns can be woven in.
● Ex. method call, field access, object creation.
■ Pointcut
● Declaration that selects join points and collects contexts at that
point (regular expressions)
■ Code Advice
● Code to be executed at a join point.
06
Chapitre
Illustration
8
Method call
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint point models
10
Exception
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint point models
11
Constructor call
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Joint Point Models
12
Field access
public class HelloWorld {
private int max=10;
public static void main(String args[]) {
new HelloWorld().affichage();
}
public void affichage() {
for (int i=0;i<max;i++) {
try {
System.out.println("HelloWorld!");
} catch (Exception e) {
System.err.println("Problème d'affichage");
}
}
max--;
}
}
06
Chapitre
Code Advice
13
■ An OAP application
● Set of classes
● Set of aspects
■ Weaving
● Weaving rules specify how to integrate the final system
06
Chapitre
Types of Weaver
15
■ 1/ Static weaver
● Source to source translation.
Aspect code
Intermediate
Class code Weaver Compiler bytecode
source code
06
Chapitre
Types of Weaver
16
■ 2/ Dynamic weaver
● Bytecode enhancement, first compile source with original
compiler, then weave aspects into class files.
Weaver
Determine if a
code advice must
be executed
Pre-Weaver
Modifier Virtual
Class code modifies the Bytecode
class code Machine
class code
06
Chapitre
AOP Paradigm
17
■ http://www.eclipse.org/aspectj
▶ Plug-in Eclipse
■ http://eclipse.org/ajdt
06
Chapitre
Example 1: HelloWorld (before
weaving) 19
Hello.java World.aj
Hello.java World.aj
Debug.aj
Example2.java
Example2.java Debug.aj
After weaving Before weaving
public class Exemple2 { public aspect Debug {
private int _x=0; pointcut methodEx2() :
public static void main(String a[]){ execution(*Exemple2.*(..));
for (int i=0;i<10;i++){
System.out.println(_x); pointcut CallIncr() :
Exemple2.increment(); execution(*increment(..));
System.out.println(_x);
} pointcut Ensemble() :
} methodEx2() & CallIncr();
public void increment() {
_x++; around() :Ensemble() {
} System.out.println(_x);
} proceed
System.out.println(_x);
}
}
06
Chapitre
Example 3: Design Pattern
Revisited 23