Quatrième cours sur la réflexion et les annotations. Fichier

publicité
Le but du jour
En savoir plus sur les aspects
Programmation par Aspect
(AOP)
z Réflexion
z Annotations
Frédéric Fondement
Cours ENSISA 3AIR
2009-2010
-2-
La réflexion
© F. Fondement
API Réflective d’AspectJ
Java possède son mécanisme de réflexion.
AspectJ également
Class
z Dans un advice: thisJoinPoint
SourceLocation
fileName : EString
line : EInt
withinType
• Le contexte: getTarget(), getThis(), getArgs()
• Lent !
• Préférer systématiquement les sélecteurs de contexte
class
1
Conceptuel
Object
• Fil d’exécution: thisJoinPointStaticPart
• thisJoinPoint.getStaticPart() ==
thisJoinPointStaticPart
• getKind() : method-execution, constructor-call, field-get,…
• getSignature() : informations sur la nature du join point
• getSourceLocation() : information sur l’endroit du join point
– Fichier (.java ou .aj)
– Numéro de ligne
-3-
1
© F. Fondement
location
JoinPoint
1
staticPart
1
JoinPointStaticPart
kind : EString
toString() : EString
thisJoinPoinStaticPart
0..* args
1
0..1 target
0..1 this
thisJoinPoint
signature
Signature
name : EString
toString() : EString
-4-
thisEnclosingJoinPointStaticPart
© F. Fondement
thisJoinPointStaticPart.getKind()
thisJoinPointStaticPart.getSignature()
Class
1
Signature
name : EString
toString() : EString
declaringClass
class 1
org.aspectj.lang.JoinPoint
public static final java.lang.String $'9,&(B(;(&87,21
public static final java.lang.String &216758&725B&$//
&216758&725B(;(&8
public static final java.lang.String
7,21
public static final java.lang.String (;&(37,21B+$1'/(5
public static final java.lang.String ),(/'B*(7
public static final java.lang.String ),(/'B6(7
public static final java.lang.String ,1,7,$/,=$7,21
public static final java.lang.String 0(7+2'B&$//
public static final java.lang.String 0(7+2'B(;(&87,21
public static final java.lang.String 35(,17,$/,=$7,21
public static final java.lang.String 67$7,&,1,7,$/,=$7,21
"adviceexecution"
"constructor-call"
"constructorexecution"
"exception-handler"
"field-get"
"field-set"
"initialization"
"method-call"
"method-execution"
"preinitialization"
"staticinitialization"
0..* parameterTypes
0..1 exceptionTypes
fields 0..*
Field
MemberSignature
parameterNames : EString
FieldSignature
ConstructorSignature
AdviceSignature
InitializerSignature
initializer
constructors
advice 1
Class
class
1
class 1
0..*
Join Point
Signature
© F. Fondement
Kind
Method Call
MethodSignature
JoinPoint.METHOD_CALL
Method Execution
MethodSignature
JoinPoint.METHOD_EXECUTION
Constructor Call
ConstructorSignature
JoinPoint.CONSTRUCTOR_CALL
Constructor Execution
ConstructorSignature
JoinPoint.CONSTRUCTOR_EXECUTION
Static initializer execution
InitializerSignature
ConstructorSignature
JoinPoint.PREINTIALIZATION
Object initialization
ConstructorSignature
JoinPoint.INITIALIZATION
Field reference
FieldSignature
JoinPoint.FIELD_GET
Field assignment
FieldSignature
JoinPoint.FIELD_SET
Advice execution
-6-
MethodSignature
1 method
Method
methods
0..*
© F. Fondement
Exercice
Dans l’aspect d’observation, notifier le nom du champ
modifié aux observateurs.
Faire une autre concrétisation de l’aspect d’observation
pour observer l’appel des méthodes move et resize.
JoinPoint.STATICINITIALIZATION
Object pre-initialization
Handler execution
CatchClauseSignature
CodeSignature
1
Constructor
thisJoinPointStaticPart.getSignature()
0..1
returnType
field 1
constructor 1
-5-
Class
1
parameterType
CatchClauseSignature JoinPoint.EXCEPTION_HANDLER
AdviceSignature
-7-
JoinPoint.ADVICE_EXECUTION
© F. Fondement
-8-
© F. Fondement
thisEnclosingJoinPointStaticPart
Les annotations
Informations sur le Join Point englobant
« Nouveauté » de Java 1.5
Pris en compte dans AspectJ 1.5
z Le join point qui a déclenché thisJoinPoint
z Injection d’annotations en déclaration intertype
z Utile en cas de:
• Types, attributs, méthodes, constructeurs
• Method/Constructor call
• Field get/set
z Définition des sujets en fonction de leurs annotations
z On trouve
• Types, attributs, méthodes, constructeurs
• Method/Constructor execution
• thisJoinPointStaticPart dans les cas non listés cidessus
-9-
z Lecture d’annotations
• Code standard dans les advices
• Sélecteurs contextuels
© F. Fondement
- 10 -
© F. Fondement
Déclaration intertype
Définition de sujet
Injection d’annotations:
Les patrons de définition de sujet peuvent donner les
annotations attendues sous forme de patrons
z Forme:
• declare @(type|method|constructor|field):
(TypePattern|MethodPattern|ConstructorPattern|
FieldPattern) :
AnnotationDeclaration;
z Exemple
• declare @method:
void *..Resizeable+.resize(double, double) :
@Change(kind="resize");
z Patron d’annotation:
• (!)? @(Type|(TypePattern))
• Exemples
• @Ann éléments annotés par Ann
• !@Ann éléments non annotés par Ann
• @(*) éléments annotés
• @(fr.uha..*) éléments annotés par une annotation définie
dans un sous-paquetage de fr.uha
• @A @B éléments annotés par les annotations A et B
• @(A || B) éléments annotés soit A, soit B, soit les A et B
• @(@Inherited *) éléments annotés par une annotation
annotée Inherited
z Exemple de patron de méthode
• @Change * Figure+.*(..)
- 11 -
© F. Fondement
- 12 -
© F. Fondement
Sélecteurs contextuels d’annotations
z Demandent à ce qu’un élément contextuel soit annoté
• Le sujet: @annotation(AnnotationDef)
• L’objet this: @this(AnnotationDef)
• Les arguments: @args(AnnotationDef1,
AnnotationDef2, ..)
• Le récepteur: @target(AnnotationDef)
• La méthode où se trouve le join point:
@withincode(AnnotationDef)
• La classe où se trouve le join point:
@within(AnnotationDef)
Conclusion
Intérêts des aspects:
Inconvénients des aspects:
z Touts ces sélecteurs peuvent être sujets à
reconnaissance de paramètres:
• after(Change change): @annotation(change) {
String [] cparams = change.getParameters();
…
}
- 13 -
© F. Fondement
Conclusion
Comment choisir ses aspects ?
z Éléments partagés entre plusieurs besoins / éléments
z Modification systématiques sur l’existant
• Quand on a envie de faire du copier/coller
z Comme la programmation par objet: il faut de
l’expérience !
z Voir ce qu’en pensent le bouquins
- 15 -
© F. Fondement
- 14 -
© F. Fondement
Téléchargement