Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
2I1AC3 : Génie logiciel et Patrons de conception
Chapitre 6
Programmation orientée aspect
« J'ai toujours rêvé d'un ordinateur qui soit aussi facile à utiliser qu'un téléphone. Mon rêve s'est réalisé.
Je ne sais plus comment utiliser mon téléphone »
Bjarne Stroustrup
Régis Clouard
regis.clouard@ensicaen.fr
ENSICAEN 14050 Caen
Introduction
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
cannot be implemented without code-tangling and codescattering
2
3
Cross-Cutting
Class A
Class B
Action1
Class C
Action1
Action 2
Action2
Action 1
Action1
Example
: Functional Integrity Constraint
4
Code-Scattering
Class A
Class B
Class C
call method 1
definition method 1
call method 2
call mthod 2
call method 1
call method 2
Example:
Data Persistence
definition method 2
Solutions
Respect
design principles of Chapter 1 !
Aspect oriented Programming
Gregor Kiczales en 1996 à Xerox PARC
Inspiration: Artificial Intelligence
Object language
Meta-programming
Reflection
New programming paradigm
Software programming history
Procedural paradigm
OOP paradigm
OAP paradigm
– Separates business concerns from crosscutting concerns
5
6
Aspect Oriented Programming
OOP without aspect
Class A
Class B Class C
OOP with aspect
Class A
Class B Class C
Aspect
Definitions
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.
7
8
Illustration
Object program
Class A
Class B
Joint Point
A1
Joint Point
B1
Joint Point
C1
Joint Point
A2
Joint Point
B2
Joint Point
C2
Joint Point
A3
Class C
Aspect program
Aspect
Pointcut1(A1,A2,B1,C2)
Pointcut2(A3,B2,C1,C2)
Pointcut1
Code advice 1
Pointcut1 & Pointcut2
Code advice 2
9
Joint point models
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--;
}
}
10
Joint point models
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--;
}
}
11
Joint point models
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--;
}
}
12
Joint Point Models
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--;
}
}
Code Advice
Code
13
advice is a method like construct that expresses the
action to be taken at the join points that are captured by a
pointcut.
Before advice
executes prior to the join point
After
advice
executes following the join point
Around
advice
In such code advice, the keyword 'proceed' is used to specify
where to insert the join point’s execution
can continue original execution, bypass execution or cause
execution with an altered context
can cause execution of the join point multiple times
Weaving
An
OAP application
Set of classes
Set of aspects
Weaving
Weaving rules specify how to integrate the final system
14
15
Types of Weaver
1/
Static weaver
Source to source translation.
Aspect code
Class code
Weaver
Intermediate
source code
Compiler
bytecode
16
Types of Weaver
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
Class code
Pre-Weaver
modifies the
class code
Modifier
class code
Virtual
Machine
Bytecode
AOP Paradigm
Separation
functional / non functional parts
Functional part: business concerns
eg, Add or delete an employee
Non functional part: crosscutting concerns
eg, Security and access control
17
Examples
AspectJ
(Open Source)
Static weaver
URL
AspectJ
– http://www.eclipse.org/aspectj
Plug-in Eclipse
– http://eclipse.org/ajdt
18
Example 1: HelloWorld (before
weaving)
Hello.java
public class Hello {
public static void main(String a[]){
new Hello().sayHello();
}
}
World.aj
public aspect World {
pointcut salutation() : execution
(*Hello.sayHello(..));
public void sayHello() {
System.out.println("Hello!");
}
}
after() :salutation() {
System.out.println("Salut à toi
aussi");
}
19
Example 1: HelloWorld (after
weaving)
Hello.java
public class Hello {
World.aj
public aspect World {
public static void main(String a[]) {
}
new Hello().sayHello();
System.out.println("Salut à toi
aussi");
public void sayHello() {
}
}
pointcut salutation() :execution
(*Hello.sayHello(..));
System.out.println("Hello!");
}
after() :salutation() {
System.out.println("Salut à toi
aussi");
}
20
Example 2: Debug (before
weaving)
Debug.aj
Example2.java
public class Exemple2 {
private int _x=0;
public static void main(String args){
}
public aspect Debug {
pointcut methodEx2() :
execution(*Exemple2.*(..));
for (int i=0;i<10;i++) {
Exemple2.increment();
}
pointcut CallIncr() :
execution(*increment(..));
pointcut Ensemble() :
methodEx2() & CallIncr();
public void increment() {
}
_x++;
around() :Ensemble() {
}
System.out.println(x);
proceed
System.out.println(x);
}
}
21
Example 2: Debug (after
weaving)
Example2.java
Debug.aj
Before weaving
After weaving
public class Exemple2 {
public aspect Debug {
private int _x=0;
pointcut methodEx2() :
execution(*Exemple2.*(..));
public static void main(String a[]){
for (int i=0;i<10;i++){
pointcut CallIncr() :
execution(*increment(..));
System.out.println(_x);
Exemple2.increment();
System.out.println(_x);
pointcut Ensemble() :
methodEx2() & CallIncr();
}
}
public void increment() {
}
_x++;
}
}
around() :Ensemble() {
System.out.println(_x);
proceed
System.out.println(_x);
}
22
Example 3: Design Pattern
Revisited
Singleton
23
OOP : Drawbacks
Use of non classical constructor
Restrict inheritance
Singleton AOP
Add a joint point cut on the constructor
An attribute stores the instance reference
The code advice returns a new instance (1st call) or the attribute
reference
Conclusion
OOP
+ AOP = OOP
AOP is available as extensions of many programming
languages
Java
C++
Php
C#
D
Smalltalk
The
future of programming ?
24