Vous êtes sur la page 1sur 44

Architecture logicielle

GLO-3001

Partie II
Introduction à AspectJ

Félix-Antoine Bourbonnais

Département d’informatique et de génie logiciel


Université Laval

Hiver 2010
Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Partie II : Introduction à AspectJ


Plan

1 Terminologie

2 Le langage AspectJ

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 2 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

L’aspect
AspectJ

• Un module indépendant
• Très similaire à une classe dans AspectJ
• Représente une préoccupation
• Permet de regrouper dans une même entité une préoccupation
transverse

Note : Ceci est une vision technique telle qu’utilisée avec AspectJ.
Note : Peut varier en fonction du langage et du tisseur.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 3 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

L’aspect
Contenu (AspectJ)

• Du code à injecter (conseils/greffons – advices)

• Code Java (très similaire à une méthode en AspectJ)


• Sera injecté (exécuté) à des emplacements précis

• Des règles (points de coupure – pointcuts)

• Où et sous quelles conditions les greffons (advices) seront


exécutés

• Des injections (inter-type declaration)


• Du code Java standard

• Méthodes « normales » (ne seront pas injectées)


• Privées ou publiques

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 4 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Point de jonction
• Join point (en)
• Parfois appelé point de jointure
• Point précis dans le programme
• Emplacements possibles et valides pour injecter du code
• Généralement, des emplacements dans le flot de contrôle

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 5 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Point de jonction

Exemples
• L’appel d’une méthode
• L’accès (get/set) à un attribut
• Lorsqu’une exception est lancée
• Etc.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 5 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Point de coupure
• Pointcut (en)
• Sous-ensemble des points de jonction
• Identifie les points de jonction (join point) où du code (advices)
sera injecté
• En d’autres mots, où injecter/greffer le code
• Détermine où l’aspect doit s’exécuter en fonction de certaines
conditions parmi tous les points de jonction

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 6 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Point de coupure

Exemple en AspectJ
Tous les appels à une méthode dont le nom débute par « do » situés dans
une méthode de la classe « TheClass » :
1 pointcut callToDoMethodsInTheClass() :
2 call(∗ do∗(..)) &&
3 within(TheClass);

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 6 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Conseil (ou greffon)


• Advice (en)
• Code à exécuter
• Peut généralement s’exécuter avant (before), après (after) ou
autour (around) d’un point de coupure

Exemple en AspectJ
Saluer avant les emplacements désignés par le point de coupure
« saluerPointcut »
1 before() : saluerPointcut() {
2 System.out.prinln("Bonjour le monde!");
3 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 7 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Terminologie → Terminologie (AspectJ)

Les injections
• Inter-type Declarations (en)
• Permettent « d’altérer » une classe sans la modifier
• Exemples :

• Ajouter des méthodes à une classe


• Modifier l’héritage

Exemple en AspectJ
Injection de la méthode « acceptVisitor(Visitor v) » à la classe « TheClass »
1 public aspect TheAspect {
2
3 public void TheClass.acceptVisitor(Visitor v){
4 v.visit(object);
5 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 8 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Aspects

Partie II : Introduction à AspectJ


Plan

1 Terminologie

2 Le langage AspectJ
Aspects
Points de coupure
Greffons

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 9 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Aspects

Les aspects
• Syntaxe très similaire à une classe Java
• Dans un .java ou un .aj
• Syntaxe @AspectJ dans les .java

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 10 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Aspects

Les aspects (suite)


1 public aspect VisitorAspect {
2
3 /∗ −−−−−−−− Membres −−−−−−−− ∗/
4 protected StatisticsVisitor visitor = new StatisticsVisitor();
5
6 /∗ −−−−−−−− Introductions −−−−−−−− ∗/
7 public interface StatisticalElement {
8 public void acceptVisitor(StatisticsVisitor v);
9 }
10
11 declare parents : PercentageData implements StatisticalElement;
12
13 public void PercentageData.acceptVisitor(StatisticsVisitor v) {
14 v.visitValueInPercentage(this.getValue());
15 }
16
17 /∗ −−−−−−−− Pointcuts −−−−−−−− ∗/
18 protected pointcut valueChanged(StatisticalElement element) :
19 set(int PercentageData.value) &&
20 this(element);
21
22 /∗ −−−−−−−− Advices −−−−−−−− ∗/
23 after(StatisticalElement element) : valueChanged(element) {
24 element.acceptVisitor(visitor);
25 }
26 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 11 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Aspects

Ordre de préséance

Conflits !
Plusieurs aspects peuvent être « branchés » sur les mêmes points
de jonction. L’ordre d’exécution est alors indéterminé !
On peut utiliser la ligne « declare precedence » pour y remédier
dans le cas de précédences claires...

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 12 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Partie II : Introduction à AspectJ


Plan

1 Terminologie

2 Le langage AspectJ
Aspects
Points de coupure
Greffons

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 13 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)


Primitives

• call(expression-méthode) :
Au moment des appels des méthodes qui correspondent à
l’expression.
ex. : call( int foo() )
• execution(expr-méthode) :
Au moment de l’exécution des méthodes correspondantes.
• get(expr-attribut) :
À la lecture des attributs correspondants.
• set(expr-attribut) :
À l’assignation des attributs correspondants.
Attention ! list.add(2) : list n’est pas assignée

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 14 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Primitives

• adviceexecuton() (combiné) :
Dans l’exécution d’un « advice ».
• if(expr-bool) (combiné)
• within(type) (combiné) :
... et à l’intérieur de la classe du type donné.
• this(type OU id) (combiné) :
... et l’objet en cours d’éxécution est du type donné.
• target(type OU id) (combiné) :
... et l’objet ciblé est du type donné.
• args(type OU id) (combiné) :
Un argument du type.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 15 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Primitives

• cflow(pointcut) :
À partir de ce point dans le flot de contrôle du programme.
• Etc.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 16 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)


Combinaisons

Les primitives peuvent :


• être combinées avec « && » et « || »

( call(int bar()) ||
call(int foo())
) && within(MyClass)
• être inversées (négatif) avec « ! »

call(int foo()) &&


!within(TheAspect)

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 17 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)


Les expressions de méthodes

Définition
primitive( [visibilite] [final] type-retour
[pkg.][cls.]methode([params|.., ...])
[throws exception] )

• Tous les éléments sont optionnels sauf le type de retour, le nom


de la méthode et ses paramètres
• Généralement, l’omission d’un pramètre veut dire « peu
importe »

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 18 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

Exemples :

• Signature exacte :

call( public final void C.foo(int)


throws Exception )
• call(public final void C.*(int))
• call(public final void *.foo(int))
• call(public final void *.*(int))
• call(public final void *(int))

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 19 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

• Méthodes finales ET publiques :


call(public final void *(int))
• Méthodes finales ou non :
call(public void *(int))
• Méthodes publiques ou privées ou default (package) :
call(void *(int))
• Peu importe le type de retour :
call(* *(int))

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 20 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

• Ayant exactement 1 paramètre de type int :


call(void *(int))
• Ayant aucun (exactement 0) paramètre :
call(void *())
• Peu importe les paramètres (0 ou plus)
call(void *(..))
• call(void *(.., String))

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 21 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

• Lance une exception de type Exception :


call(void *() throws Exception)
• Lance ou non une exception :
call(void *())

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 22 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

• Lance une exception de type Exception (ou une sous-classe) :


call(void *() throws Exception+)
• Méthode de la classe C ou une sous-classe :
call(void C+.*())
• Retourne un type R ou une sous-classe :
call(R+ *())

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 23 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts) (suite)


Les expressions de méthodes

• N’est pas publique :


call( !public void foo(..))
• call(void *(..) throws !IOException)
• Forme la plus générale :
call(* *(..))
• call(* ca.ulaval.glo3001.* m(..))
call(* ca.ulaval.glo3001..* m(..))
• Etc.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 24 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)


Les expressions de méthodes (constructeurs)

• call(Foo.new())
• call(Foo+.new())
• call( (Foo+ && !Foo).new())
• call(*Test+.new(..))

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 25 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)


Les expressions de types

• within(ca.ulaval.glo22879.*)
• within(MaClasse)

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 26 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Les points de coupure (pointcuts)

Etc, etc, etc...

Consultez le AspectJ 5 Quick Reference [1] :

http ://www.eclipse.org/aspectj/doc/released/quick5.pdf

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 27 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

« this » VS « target »
Les primitives « this » et « target » s’interprètent en fonction des
autres primitives auxquelles elles sont combinées.
Exemple
Appels vers une méthode « foo » qui se font à partir de la classe « Bar »

call( int foo() ) && this(Bar)

Exemple
Appels vers une méthode « foo » de la classe « Bar »

call( int foo() ) && target(Bar)


⇐⇒
call( int Bar.foo() )

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 28 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

« this » VS « target » (suite)


Exemple
Exécution des méthodes « foo » de la classe « Bar »

execution( int foo() ) && this(Bar)


⇐⇒
execution( int Bar.foo() )

Exemple
execution( int foo() ) && target(Bar)
⇐⇒
execution( int Bar.foo() )
⇐⇒
execution( int foo() ) && this(Bar)

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 29 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Boucles infinies

Bouclage !
AspectJ recherche les points de jointure dans la classe mais
également dans les aspects. Il est donc dangereux de boucler si un
aspect s’applique sur lui-même !

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 30 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Points de coupure

Boucles infinies

Exemple 1

1 pointcut err1() : execution(∗ ∗(..));


2 pointcut ok1() : execution(∗ ∗(..)) && !adviceexecution();
3 pointcut ok2() : execution(∗ ∗(..)) && !within(MyAspect)

Exemple 2

1 pointcut err2() : call(∗ List.add(..))


2 before() : err2() {
3 List<Integer> l = new ArrayList<Integer>();
4 l.add(2);
5 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 30 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Partie II : Introduction à AspectJ


Plan

1 Terminologie

2 Le langage AspectJ
Aspects
Points de coupure
Greffons

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 31 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Type de greffons
• before()
• after()
• after() returning
after() returning(Type x)
• after() throwing
after() throwing(Type x)
• around

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 32 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Les conseils/greffons (advices)

Exemple
Journaliser le début et la fin de l’exécution de toutes les méthodes qui prennent un « Integer »
comme unique paramètre.

1 pointcut log() :
2 execution(∗ ∗(Integer)) && !adviceexecution();
3
4 before() : log() {
5 System.out.println("DEBUT de " + thisJoinPoint.getSignature());
6 }
7
8 after() : log() {
9 System.out.println("FIN de " + thisJoinPoint.getSignature());
10 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 33 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Mots clefs spéciaux


• thisJoinPoint() :
• Permet d’obtenir de l’information sur le contexte (le point de
jonction) dans lequel s’éxécute le greffon.
• Exemples :
.getTarget() ;
.getThis() ;
.getSignature().getDeclaringType() ;
.getSignature().getModifiers() ;
...

• proceed([arguments]) :
• Dans un around()
• Permet de poursuivre l’exécution
• Les arguments sont ceux du greffon pas de la méthode
interceptée !
Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 34 / 41
Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Intercepter les paramètres

Exemple

1 pointcut pc(int i, String s) :


2 call(void foo(int, String)) &&
3 args(i, s);
4
5 before(int p1, String p2) : pc(p1, p2) {
6 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 35 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Intercepter la valeur de retour

Exemple

1 pointcut pc() :
2 call(int foo(..));
3
4 after() returning(int i) : pc() {
5 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 36 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Variations

1 pointcut log() :
2 execution(∗ ∗(..)) && !adviceexecution();
3
4 before() : log() {
5 System.out.println("DEBUT de " + thisJoinPoint.getThis().toString());
6 }
7
8 after() : log() {
9 System.out.println("FIN de " + thisJoinPoint.getThis().toString());
10 }

1 pointcut log() :
2 execution(∗ ∗(..)) && !adviceexecution();
3
4 Object around() : log() {
5 System.out.println("DEBUT de " + thisJoinPoint.getThis().toString());
6 Object ret = proceed()
7 System.out.println("FIN de " + thisJoinPoint.getThis().toString());
8 return ret;
9 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 37 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Variations (suite)

1 pointcut log(Object obj) :


2 execution(∗ ∗(..)) && !adviceexecution() &&
3 this(obj);
4
5 Object around(Object obj) : log(obj) {
6 System.out.println("DEBUT de " + obj.toString());
7 Object ret = proceed(obj)
8 System.out.println("FIN de " + obj.toString());
9 return ret;
10 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 38 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Variations (suite)

Où est l’erreur ? ?
1 pointcut log(Object source, Object obj) :
2 call(∗ ∗(..)) && !adviceexecution() &&
3 this(source) && target(obj);
4
5 before(Object source, Object obj) : log(source, obj) {
6 System.out.println(String.format("DEBUT de %s à " +
7 "partir de %s", obj, source));
8 }

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 39 / 41


Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Le langage AspectJ → Greffons

Variations (suite)

Version corrigée...
1 pointcut log(Object source, Object obj) :
2 call(∗ ∗(..)) &&
3 this(source) && target(obj) &&
4 !cflow(adviceexecution());
5
6 before(Object source, Object obj) : log(source, obj) {
7 System.out.println(String.format("DEBUT de %s à " +
8 "partir de %s", obj, source));
9 }

cflow ? ?
Suite au prochaine cours...
Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 40 / 41
Terminologie Le langage AspectJ Annexes

Partie II : Introduction à AspectJ → Annexes

Références
[1] AspectJ 5 quick reference.
http://www.eclipse.org/aspectj/doc/released/quick5.pdf.

[2] AspectJ : language semantics.


http://www.eclipse.org/aspectj/doc/released/progguide/semantics.html.

[3] The AspectJ programming guide.


http://www.eclipse.org/aspectj/doc/released/progguide/index.html.

[4] The AspectJTM 5 development kit developer’s notebook.


http://eclipse.org/aspectj/doc/released/adk15notebook/index.html.

Félix-Antoine Bourbonnais Architecture logicielle GLO-3001 Hiver 2010 41 / 41

Vous aimerez peut-être aussi