Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course:...

53
Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P Case studies in Eiffel and AspectJ Philippe Lahire I3S Laboratory UNSA/CNRS – OCL team Zurich – 16 April 2003

Transcript of Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course:...

Page 1: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

Course: Advanced Topics in Object Technology

Extensibility and Reusability:A.O.P. vs. O.O.P

Case studies in Eiffel and AspectJ

Philippe Lahire

I3S Laboratory UNSA/CNRS – OCL teamZurich – 16 April 2003

Page 2: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

2

Objective: answering some questions

• What are the programmer needs?• Why looking at some other paradigms?• What is ASoC?• What is AOP? • What is AspectJ?• What are the pros and cons of ASoC?• Should we extend O-O languages?• etc.

Page 3: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

3

Summary I. Context of the presentation

Programmer needsOOP limitationsMain (current) implementation of AOP (AspectJ)

II. Case studiesN°1: Implementating contractsN°2: Integrating design patterns a posterioriN°3: Equipping components with testing material

III. Conclusion and perspectivesSummaryPossible research topics

Page 4: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

4

Setting the contextClass-hierarchies:– Does perfect design exist?– Need of maintenance– Need of using new services (“big” functionalities)

Perfect::

Everything is in Everything is in the hierarchy (for ever)the hierarchy (for ever)

andandIt is at the right placeIt is at the right place

Context of use may vary: – Life-cycle: debug, test, etc.– Applications may not use it in the same wayIts needs may evolve:– To add set of functionalities (one point /spread away)– To adapt existing functionalities

A A posterioriposteriori changes within classchanges within class--hierarchyhierarchy

Page 5: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

5

Answers of O-O languages

Inheritance– Multiple / repeated– Feature redefinition / renaming– Frozen / deferred featureClient-Supplier relationshipGenericityAnchorsOnce featuresFirst-class features

Page 6: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

6

Deficiencies of O-O languagesPrinting_FunctionalitiesC1

C3

C4 C5

C2

C7 C8

C6

C9

Drawing_Functionalities

C8’C7’

Printing_Functionalities

C9’

C8”C9”

Drawing_Functionalities

Hierarchy becomesHierarchy becomesToo complex Too complex

Page 7: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

7

References (deficiences of O-O)• K. Ostermann, M. Mezini. “Object-Oriented Composition is

Tangled”, Workshop ASoC ECOOP 01, 2001• T. Lawson & al. “The Potential for Reverse Inheritance in

Eiffel”, TOOLS Europe, 1994• Case study : Eliminating Redundant Codes in the Buffer Library.

http://www.comp.nus.edu.sg/~lisb/javalib/classes.htm• H. Ossher, W. Harrison. “Combination of inheritance

hierarchies”, OOPSLA’92• U. Hölzle. “Integrating independently-Developed Components

in Object-Oriented Languages”, ECOOP’93• …

Page 8: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

8

Approach by Separation of Concerns

Concern A

Concern B

•Concern A•Concern B

Data type encapsulation vs. Preoccupation encapsulationData type encapsulation vs. Preoccupation encapsulation

Page 9: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

9

ASoC (AspectJ): surveyPoints addressed in this surveyPoints addressed in this survey :

Join Point ModelPointcut designatorsAdvicesFeature introductionAspect declarationReflectionMore on aspects: abstract, instantiation, …Implementation: static crosscutting…

Page 10: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

10

ASoC (AspectJ): contextJava extension:– Single inheritance– Interfaces, (abstract) classes– Modifiers: private, protected, public– Implicit redefinition / no renaming– Overloading, no varianceNew features:– Keep the same philosophy– Pattern matching language– …

Page 11: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

11

ASoC (AspectJ): Joint Point modelclass Person {protected Person partner ;protected int age ;

public Person (int a) {setAge(a);partner = new Person(0);

}public setPartner (Person s) {

if (partner.age = = 0)partner = s;

elsethrow new … ;

}…}

Field assignmentField reference

Exception handler execution

Constructor callMethod call

Method execution

Initializer executionConstructor execution

Page 12: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

12

ASoC (AspectJ): Pointcut DesignatorsA A pointcutpointcut is a program element, which allows catching a is a program element, which allows catching a

set of joint points within a hierarchy of classes set of joint points within a hierarchy of classes

A syntax for pointcut declaration:• Naming it• Picking out execution context • Selecting joint points• More complex pointcut

PointcutPointcut int ii) : …pointcut Person pp) : …

pointcut Person pp) :

basicPointcut () : …basicPointcut (basicPointcut (

basicPointcut (

Type of Joint point

call (public void Person.* (.., Person))

Method modifier

Return type

Class container

Method name

Method parameters

Associate an object

to pp

&& target (pp)

Page 13: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

13

ASoC (AspectJ): Advicespointcut viewNames (Person pepe, Person papa) :

target (pepe) && args (papa) && call (public void Person.* (.., Person))

around (Person pepe, Person papa) : after (Person pepe, Persorn papa) : before ( Person pepe, Person papa) :

viewNames (Person pepe, Person papa) Match joint point + set context

{

System.out.println (papa.name + “will become the partner of”

+ pepe.name);

}

{

System.out.println (papa.name + “became the partner of” +

pepe.name);

}

Action to perform

Possible advices:• before• after• around

{

if (pepe.age == papa.age) proceed (pepe, papa);

else

System.out.println (“Error …”)

}

Page 14: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

14

ASoC (AspectJ): Aspect Declarationaspect PersonCrossCutting {pointcut viewNames (Person pepe, Person papa) : target (pepe) && args (papa) && call (public void Person.* (.., Person))

before (Person pepe, Person papa) : viewNames (Person pepe, Person papa) {

System.out.println (papa.name + “will become the partner of” + pepe.name); }after ( Person pepe, Person papa) :

viewNames (Person pepe, Person papa) {

System.out.println (papa.name + “became the partner of” + pepe.name); }around (Person pepe, Person papa) :

viewNames (Person pepe, Person papa) {

if (pepe.age == papa.age) proceed (pepe, papa);

else System.out.println (“Error : the age of persons are not compatible”) }…}

Page 15: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

15

ASoC (AspectJ): More about advices

Raised exception is attached toRaised exception is attached to ee

Pointcut viewNames (Person pepe) :

target (pepe) && call (public Person Person.getPartner (..))

{

System.out.println (pa.name +“is the partner of” + pe.name);

}Possible clauses:• returning• throwing

after (Person pepe)

viewNames (Person pepe)

returning (Person papa) :

Object returned by Object returned by getPartnergetPartner (applied on(applied on pepe))

Both Both pepe are the same objectsare the same objects

3

1

An object is attached toAn object is attached to pepe

2

Result of Result of getPartnergetPartner is attached tois attached to papaObject on which Object on which

the advice appliesthe advice applies

{

System.out.println (e.getMessage() + “is the explanation of exception”);

}**

**

**

**

**

throwing (Exception ee) :

Exception raised by Exception raised by getPartnergetPartner (applied on(applied on pepe))

Page 16: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

16

ASoC (AspectJ): More about pointcuts

Some other pointcuts:–– executionexecution (public * Person+.*(..))–– InitializationInitialization (Person (int))–– getget/setset (public int Person.*)–– handlerhandler (Person)

Combining pointcuts:–– getget (int Person+.*) && ! getget (* Employee.*)–– cflowcflow (execution (public * Person.set*(..)))

Signature

Type

Pointcut

Page 17: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

17

ASoC (AspectJ): More about pointcuts

Class Person {

…}

Person

p1

OneClientsetPartner (Person p) {

If (p.getAge() == age) …

}

p1.setPartner(p)

withincodewithincode (Person.(Person.setPartnersetPartner))Within (Person)Within (Person)

this (this (OneClientOneClient))

Target (Person)Target (Person)

argsargs (Employee)(Employee)Take into Take into account the account the

code of called code of called routinesroutines

cflowcflow (Person.(Person.setPartnersetPartner))call (* set* (..))

Any method Any method

Starting by “set”Starting by “set”

c1

Page 18: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

18

ASoC (AspectJ): Static Crosscuttingaspect PersonCrossCutting {

public int count = 0

private boolean Person.isEfficient = False;

public void Person.setIsEfficient () { isEfficient = true;}

public boolean Person.IsEfficient () { return isEfficient ;}

private boolean application.*.trace = True;

public void application.*.setTrace() {this.r;};

declare parents : Person extends Observer ;

declare parents : Person implements Subject ;

pointcut … …}

Attribute

Method

Several classes!

In aspect only!

Class or interface

privilegied aspect PersonCrossCutting {

Access to Access to private feature!private feature!

Page 19: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

19

ASoC (AspectJ): More on aspectsabstractabstract aspect viewing {

abstract abstract pointcut viewNames (Person pepe, Person papa) ;

before (Person pepe, Person papa) : viewNames (Person pepe, Person papa) {

System.out.println (papa.name + “will become the partner of” + pepe.name); }…}

aspect viewing1 extendsextends viewing {

pointcut viewNames (Person pepe, Person papa) ; target (pepe) && args (papa) && call (public void Person.* (.., Person))

…}

issingletonissingleton PerthisPerthis (Person)(Person)

Page 20: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

20

ASoC (AspectJ): Reflectionaspect PersonCrossCutting {pointcut viewNames (Person pepe) : target (pepe) && call (public void Person++.* (..))

before (Person pepe) : viewNames (Person pepe) {

Object targetObject = thisJoinPoint.getTarget(); // Dynamic

Object currentObject = thisJoinPoint.getThis(); // Dynamic

Object[] currentargs = thisJoinPoint.getargs(); // Dynamic

Signature currentSignature = thisJoinPoint. getSignature(); // StaticClass currentClass = currentSignature.getDeclaringType(); // Static…. Doing something according to dynamic type of pepe or J.P. static information

}…}

Including heirs

Page 21: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

21

ASoC (AspectJ): SummaryTo adapt existing hierarchies:– Piece of code in existing methods– New methods or attributes– New ancestors

To adapt easily:– Reusable adaptation– Expressive language for the specification of adaptation

Join points to catch where to adapt

Non invasive adaptation

Adaptations written in hierarchy of aspects

Page 22: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

22

ASoC and Component adaptations

Possible needs• To add testing material

Check/debug clauses from Eiffel To handle capture/replay of events in GUI (regression testing)

– Test automation (execution, logs, generation)To insert design patternsTo handle assertions

• To try some structure modifications• To try some new implementation• …• More generally : to add/adapt/“remove”to add/adapt/“remove” functionalities

Page 23: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

23

Case-study n°1: Handling of contracts

Description:To provide a way to define assertionsTo provide a mechanism to execute assertions

Context:Adding a new service to the programmerIt is orthogonal to application specification

Is Is ASoCASoC a good alternative to implement assertions?a good alternative to implement assertions?

Is Is ASoCASoC a good alternative to implement check / debug?a good alternative to implement check / debug?

Page 24: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

24

Contracts: A basic example with Eiffelset_age (a: INTEGER) is

-- Set age to arequire

a >= 0 do

age := aensure

age = aend

…invariant

is_married impliespartner /= Void andpartner /= Current

end

class PERSON

feature partner: PERSONage: INTEGERis_married: BOOLEAN

set_partner (s: PERSON) is-- Set partner to s.

requires /= Voids /= Current

dopartner := sis_married := True

ensurepartner = s and is_married

end

Page 25: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

25

Contracts: same example with AspectJclass Person {protected Person partner ;protected int age ;protected boolean isMarried ;

setPartner (Person s) {// set partner with ‘s’

partner = s; …}setAge (int a) {// set age with 'a'

age = a}…}

aspect PersonAssertion {

pointcut assertSetAge(Person p, int a) :target(p) && target (Person+) && args(a) &&call(void Personne+.setAge(int));

before(Person p, int a): // // preconditionpreconditionassertSetAge(p, a) {if (a < 0 || a > 100)

throw new IllegalArgumentException(“….");}…

// continuation continuation -- invariantinvariantpointcut invPerson (Person p) :target (p) && target (Person+) && call (* * (..))&& !within (PersonAssertion);

private inv1 (Person p) {if p.isMarried() && (p.partner == p || p.partner == null)

throw new IllegalArgumentException(“…”);}

before(Person p) : invPerson (p) {inv1 (p);}after (Person p) returning : invPerson (p) {inv1(p);}…}An aspect per class hierarchy & a pointcut per assertion!

// continuationafter (Person p, int a): // // postconditionpostcondition

assertSetAge(p, a) {if (p.age != a)

throw new IllegalArgumentException(“….”);}…

Page 26: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

26

Contracts: reification and AspectJaspect GenericContract {

private JoinPoint currentJoinPoint;private void applyAssertion (String s) { … }

pointcut checkToBePerformed () :target (Object+) && call(* * (..)) && !within (GenericContract ) &&! call(* inv_* (..)) && ! call(* pre_* (..)) && ! call(* post_* (..)) ;

before() : checkToBePerformed () { currentJoinPoint = thisJoinPoint;applyAssertion (“inv_”); applyAssertion (“pre_”); }

after() returning : checkToBePerformed () { currentJoinPoint = thisJoinPoint;applyAssertion (“post_”); applyAssertion (“inv_”); }

}

Two alternatives: Abstract or not

Much more complicated but quite generic

Page 27: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

27

Contracts: reification and AspectJclass Person { // normal contents + assertionsprivate boolean pre_setAge_1 (int arg) {return arg >= 0 && arg <= 100; }

private boolean pre_setName_1 (String arg) {return arg != null;}

private boolean pre_setPartner_1 (Person arg) {return arg != null;}

private boolean post_setPartner_1 (Person arg) {return arg != this;}

private boolean inv_Person_1 () {return this.age >= 0;}

private boolean inv_Person_2 () {

if (this.is_married && (this.partner == null || this.partner.partner != this))

return false;

else return true;}…

} 1st solution: Assertions are embedded in a class

Page 28: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

28

Contracts: reification and AspectJ

Privileged Aspect PersonContract extends genericContract {

private boolean Person. Pre_setAge_1 (int arg) {identical }

private boolean Person.pre_setName_1 (String arg) {identical }

private boolean Person.pre_setPartner_1 (Person arg) {identical }

private boolean Person.post_setPartner_1 (Person arg) {identical }

private boolean Person.inv_Person_1 () {identical }

private boolean Person.inv_Person_2 () {identical }

} 2nd solution: Assertions are embedded in concrete aspect

Caution: Features are added to class Person

genericContract becomes an abstract aspect

Page 29: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

29

Contracts: reification and AspectJ

•To insert assertion

•To adapt the pointcut

Staticp1.p1.setPartnersetPartner (p2); p1.(p2); p1.setAgesetAge(30);(30);

Class Person

assertions

aspect* GenericContract

aspect PersonContract

p1

p2

agepartner

agepartner

For each method call:

•To get target object & class

•To get method signature

•To find matching assertion

•To perform the assertion

Dynamic

Page 30: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

30

Contracts: reification and AspectJaspect genericContract { // Declarationprivate void applyAssertion (String typeOfAssertion) {

CodeSignature currentSignature; // Signature of the called methodMethod currentMethod; // Method corresponding to currentSignatureClass currentClass; // Class containing currentMethodClass[] currentFormalArgs; // Formal arguments of currentMethodObject[] currentEffectiveArgs; // effective arguments of the called methodObject currentObject; // object on which is called currentMethodBoolean currentCallResult; // contains the result of the assertion execution…

Source code: around 200 lines

Here: some of the declarations

Next: a piece of code applied on setPartner

Page 31: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

31

Contracts: reification and AspectJcurrentEffectiveArgs = currentJoinPoint.getArgs();currentObject = currentJoinPoint.getTarget();currentSignature = (CodeSignature) currentJoinPoint.getStaticPart ().getSignature();currentClass = currentSignature.getDeclaringType();currentFormalArgs = currentSignature.getParameterTypes();currentMethod = currentClass.getDeclaredMethod

(currentSignature.getName(), currentFormalArgs);…While (!stop) {

currentM = currentClass.getDeclaredMethod (currentName, new Class[]{});ReflectPermission x = new ReflectPermission("suppressAccessChecks");AccessibleObject.setAccessible (new AccessibleObject[] {currentM}, true);

currentCallResult = (Boolean) currentM.invoke (currentObject, null);if (! currentCallResult.booleanValue())

throw new IllegalArgumentException(currentMethod.getName() + “…”);…} ...} …}

p2p1

void setPartner (Person)Person

setPartner (Person)

Person_inv1()

Person

Succeed / failed

Page 32: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

32

Contracts and ASoC: summaryBenefits:

Several models of assertion can be addressedA very expressive implementation platform

Drawbacks: Naming constraint / robustness of implementationNot straightforward to use / Specification purpose?Inheritance and assertions hard to implement

Assertion handling depending on user needs but Assertion handling depending on user needs but requiring a set of tools for requiring a set of tools for visualizationDisconnectvisualizationDisconnectassertions from the languageassertions from the language

Page 33: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

33

Contracts and ASoC: conclusion

To implement contracts as aspects?To implement contracts as aspects?

Contracts may be seen as a concern of the applicationBut all concerns are not at the same level:– Robustness of application depends on it– Contracts requires some syntactical sugar

(documentation, easy to use, …)

Aspects could be used for implementation purpose onlyAspects could be used for implementation purpose only

Page 34: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

34

Implementing check/debug

First impression: AspectJ does not bring any significant facility for inserting debug/check clauses into a routine code.

Possible solutions:

AspectJ: To use the reification of the source code (line number within the file)

Java: To insert a routine in the class + a call to it at the right place

First solution is too complicated

Both are poorly readable

Page 35: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

35

Case-study n°2: Use a Design PatternDescription:• To adapt a library in order to use a design pattern• Design pattern: Observer/Subject• An interface handles the interactions between buttons and

labels. When people click on a button the label should be modified (color change).

Context:• Adding new functionalities to some library classes • It depends on one type of application

Is Is ASoCASoC a good alternative to use design patterns?a good alternative to use design patterns?

Page 36: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

36

Design pattern Observer : UML view

<<description>>

forall o in observers{

o.update (this);}

1 *

I_Subject

+detach(observer:Observer)+notifyObservers()

-observers [*]: Observer

Subject

+attach(observer:Observer)+detach(observer:Observer)+notifyObservers()

<<interface>>

Observer

+update(o: Subject)

<<interface>>observers

+attach(observer:Observer )

Page 37: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

37

Use of Design pattern: ExampleEV_WIDGET

EV_PRIMITIVE

EV_LABEL EV_BUTTON

EV_TOGGLE_BUTTONEV_RADIO_BUTTON

SUBJECT*

I_SUBJECT

OBSERVER*

ROOT_CLASSupdate (o: SUBJECT) is

do

-- Change label color.

endLabel colorchanges

Notifies label

Creation of buttons (subject)

Creation of labels (observers)

Buttons attached to labels

….

Attach to label User click on Buttons

….

Page 38: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

38

Use of Design pattern: O-O answerEV_WIDGET

EV_PRIMITIVE

EV_LABEL EV_BUTTON

EV_TOGGLE_BUTTONEV_RADIO_BUTTON

SUBJECT*

I_SUBJECTOBSERVER*

ROOT_CLASS

EV_RADIO_BUTTON2

EV_LABEL2b1: EV_BUTTON; l1: EV_LABEL ……

create {EV_RADIO_BUTTON2EV_RADIO_BUTTON2} b1

create {EV_LABEL2EV_LABEL2}} l1

b1.attach (l1)

b1.click ……

update (o: SUBJECT) is

local

c1: EV_COLOR

do

create c1.make (…)

set_foreground_color (c1)

end

Page 39: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

39

OO Answer : summary

The same drawbacks as explained (part I)The same drawbacks as explained (part I)

• Number of classes in the hierarchy may be increased• Clients must use new name• Composition of several hierarchies• …

Or: changes in the source class itself …

Ability to reuse the library decreased?Ability to reuse the library decreased?

Page 40: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

40

Use of Design pattern: ASoCEV_WIDGET

EV_PRIMITIVE

EV_LABEL EV_BUTTON

EV_TOGGLE_BUTTONEV_RADIO_BUTTON ROOT_CLASS

ProtocolOfObserver1EV_BUTTON b1 = new EV_RADIO_BUTTONEV_RADIO_BUTTON () ;

EV_LABEL l1 = new EV_LABEL () EV_LABEL () ;

b1.attach (l1) ; b1.click(); ……

abstractProtocolOfObserver

Subject*Observer*

DesignPatternObserver

Page 41: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

41

Design pattern Observer: ASoCimport DesignPatternObserverDesignPatternObserver ;

abstract pointcut specificStateModification () ;pointcut stateModification (Subject ss) : target (ss) && specificStateModification () ;after (Subject ss) : stateModification (ss) { ss.notifyObservers () ;}

private Vector Subject.observers = new Vector () ;public void Subject.attach (Observer oo) { observers.addElement (oo); }public void Subject.detach (Observer oo) { observers.removeElement (oo); }public void Subject.notifyObservers () {

for (int ii = 0 ; ii < observers.size () ; ii++ ) ( (Observer) observers [ii] ).update (this) ;

}

public abstract aspect ProtocolOfObserverProtocolOfObserver

{

}

Page 42: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

42

Design pattern Observer: ASoC

pointcut specificStateModification () : call (void EV_BUTTON.click () ) ;

declare parents : EV_BUTTON implements Subject ;

public aspect ProtocolOfObserver1 ProtocolOfObserver1 extends ProtocolOfObserver ProtocolOfObserver

{

}

public void EV_LABEL.update (Subject ss) {

EV_COLOR c c = new EV_COLOR (……);

set_foreground_color (cc);

}

declare parents : EV_LABEL implements Observer ;

Page 43: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

43

ASoC Answer: summary• Does not increase the number of classes of the hierarchy

• Modification are specified outside of the class (aspects)

• A large part of adaptation is shared (abstract aspect)

• Only application specific part is repeated (concrete aspects)

• Clients can use the same name

• Composition of several hierarchies is possible

• Code for adaptation is mixed with application code

• …

Ability to reuse the library is increasedAbility to reuse the library is increased

Page 44: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

44

Case-study n°3: Handling of tests

Description:• Handling capture/replay of events in graphical libraries• To be able to activate/deactivate capture/replay• Test automation (execution, logs, generation)

Context:• Adding new functionalities to some classes of library • It depends on one type of application

Is Is ASoCASoC a good alternative to implement tests?a good alternative to implement tests?

Page 45: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

45

Testing techniques• A wide subject

• Tracking bugs / Deviations from specification

• Granularity, reification and storage

• Several categories of test (scope/intent categorization)

• …

• Objectives of next slides

• Scope categorization: unit testing & regression testing

• Intent categorization: white-box & fault-based testing

• Small example to show how to implement with ASoC

Page 46: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

46

Handling Capture/replay of eventsEiffelvision on WindowsACTION_SEQ

• call• extend

TUPLE

2

1

4

5

6

8

EV_WINDOW_IMP• process_message

WEL_WINDOW• process_message

…EV_BUTTON_IMP• process_message

WEL_MSG• peek_all• dispatch

WEL_DISPATCH• window_procedure

EV_APP_I

EV_WINDOWEV_WINDOW_I

ARRAYEV_APP• launch

• loop_message• process_message

EV_APP_IMP

WEL_APP

3

7

Page 47: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

47

Handling Capture/replay of eventsTrying to understand Eiffelvision event handling (Windows) :

Initialisation of graphic objects and user actions generate events

Application object receipts all events and then dispatch them

-- message_loopmessage_loop: “to get message” and then “to handle message”

- use MSG_WEL routines: peek_allpeek_all, dispatchdispatch

window_procedure window_procedure (call-back): to dispatch events related to one WEL_WINDOW (in WEL_DISPATCHER)

Call process_messageprocess_message: send one message to one graphic objectwith its two coordinates (in WEL_WINDOW)

It looks quite centralizedIt looks quite centralized

Page 48: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

48

Handling Capture/replay of eventsPossible approach :

- To implement a flag : capture / replay / normal use

- All work is at one location EV_APP_IMP and suppliersEV_APP_IMP and suppliers

- To add a pieces of code aroundaround and afterafter peek_all

- Capture :

- To take info from current object (mainly MSG_WEL)

- To store information in DB after peek_allafter peek_all

- Replay :

- To retrieve information from DB, to put it in MSG_WEL object

- To bypass peek_allbypass peek_all

Page 49: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

49

Handling Capture/replay of eventsimport eiffelVisioneiffelVision ;public aspect EventCaptureReplayProtocolEventCaptureReplayProtocol

{private int EV_APP_IMP.action = normal_usenormal_use ;

public void EV_APP_IMP.set_normal () { action = normal_usenormal_use; }

public void EV_APP_IMP.set_capture () { action = capturecapture; }public void EV_APP_IMP.set_replay () { action = replayreplay; }

New New functionalities for functionalities for

EV_APP_IMP EV_APP_IMP

pointcut eventCaptureReplay (WEL_MSG mm, EV_APP_IMP aa) :

target (mm) && this (aa) call (void WEL_MSG.peek_all()) && (

( withincode (void WEL_APP+.message_loop()) ||

withincode (void EV_APP_IMP.process_events()) ) ;

Page 50: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

50

Handling Capture/replay of events// ContinuationContinuation

private Database db = new Database () ;

after (WEL_MSG mm, EV_APP_IMP aa) : eventCaptureReplay (mm, aa) {

if (action == action == capturecapture) // // store information in databasestore information in database

db.set_hwnd (mm.get_hwnd () ) ; …… // same for all information to storesame for all information to store

}

around (WEL_MSG mm, EV_APP_IMP aa) : eventCaptureReplay (mm, aa) {

if (action == action == replayreplay) {

mm.set_hwnd (db.get_hwnd ()); …… // same for all stored informationsame for all stored information

else proceed (m,a) ; // normal call to peek_all

}}

Page 51: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

51

Automation of test execution: summaryOO ProgrammingOO Programming :

• Insertion using inheritance (new descendant): In this case it looks fine

• EV_APP(LICATION)_IMP is a leaf

• EV_APP_IMP never used by clients other than eiffelvision itself

AO ProgrammingAO Programming :

• Capture / replay is also encapsulated at one place

• Open to more sophisticated test implementation

• Access to “process_message” in descendant of WEL_WINDOW

OOP is sufficient but AOP is more openOOP is sufficient but AOP is more open

Page 52: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

52

Conclusion & Perspectives• Maintenance:

– Pointcut, advices, facility to add declaration– Powerful language for code adaptation

• Adding of new services:– Most should be carried out by syntactical add-

on: contract, debugging, concurrency, etc.– Fit for specific services but not for others

• AOP for Application MaintenanceAOP for Application Maintenance

• Syntactical addSyntactical add--on for better readabilityon for better readability

Page 53: Extensibility and Reusability: A.O.P. vs. O.Oi3s.unice.fr/~lahire/publications/eth.pdf · Course: Advanced Topics in Object Technology Extensibility and Reusability: A.O.P. vs. O.O.P

22/04/2003 Philippe Lahire - team OCL - I3S Laboratory UNSA / CNRS

53

Perspectives

• To keep the O-O language semantics and syntax for treatment handling– Is it necessary to distinguish maintenance code

from original using special keyword (aspectaspect)?– Is it necessary to get some other capability?

• To delegate to “Maintenance/configuration” language adaptation of class hierarchy– Do we need all the power of AspectJ?– As soon as modifications are stable: application

should be rebuilt … In theoryIn theory!