29
1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,[email protected]) Informatics Center Federal University of Pernambuco Brazil

1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,[email protected]) Informatics Center Federal University of Pernambuco Brazil

Embed Size (px)

Citation preview

Page 1: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

1

Deriving Refactorings for AspectJ

Leonardo Cole Paulo Borba

(lcn,[email protected])

Informatics CenterFederal University of Pernambuco

Brazil

Page 2: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

2

Problem

Aspect-oriented developers are identifying common transformations for aspect-oriented programs

Refactorings are generally complex and global

It is difficult to verify if a defined refactoring preserves behaviour

Page 3: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

3

Our Approach Primitive laws of programming

• simple, localized, intuitive and easier to understand

• Two transformations• Bi-directional• Guarded by pre-conditions

We use the laws to - Derive complex and global refactorings- Verify that an existing refactoring

preserves behaviour

Page 4: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

4

Deriving Refactorings

Composing

Laws Refactoring

Page 5: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

5

Outline

Aspect-oriented programming laws Evaluation Conclusions

Page 6: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

6

Aspect-Oriented Programming Laws Simple and localized Two transformations Bi-directional One direction may not increase

code quality

Our laws define equivalences between two programs given that the preconditions

are respected

Page 7: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

7

Make Aspect Privileged

tsprivileged aspect A { pcs as}

=

() Advice bodies from as do not refer to private members declared in ts.

tsaspect A { pcs as}

Page 8: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

8

Add before-executiontsclass C { fs ms T m(ps) { body’; body }}

tsclass C { fs ms T m(ps) { body }}

=

paspect A { pcs before(context) : exec(C.m) && bind(context) { body’ } as}

paspect A { pcs as}

() body’ does not declare or use local variables;body’ does not call super;…

Page 9: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

9

Example

public class Account { private double balance; ... public void debit(double amount) { Access.check(new Permission(…)); //debit logic } ...}privileged aspect PermissionAspect {}

Source: Laddad, 2003 Aspect Oriented Refactoring Series

Page 10: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

10

Applying Add Before-Execution Lawpublic class Account { private double balance; ... public void debit(double amount) { //debit logic } ...}privileged aspect PermissionAspect { before(Account cthis, double amount): execution(void Account.debit(double) && this(cthis) && args(amount) { Access.check(new Permission(…)); } }

Page 11: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

11

Summary of lawsLaw Name Law Name

1 Add empty aspect 16 Remove argument parameter

2 Make aspect privileged 17 Add catch for softened exception

3 Add before-execution 18 Soften exception

4 Add before-call 19 Remove exception from throws clause

5 Add after-execution 20 Remove exception handling

6 Add after-call 21 Move exception handling to aspect

7 Add after returning-execution 22 Move field to aspect

8 Add after returning-call 23 Move method to aspect

9 Add after throwing-execution 24 Move implements declaration to aspect

10 Add after throwing-call 25 Move extends declaration to aspect

11 Add around-execution 26 Extract named pointcut

12 Add around-call 27 Use named pointcut

13 Merge advices 28 Move field introduction up to interface

14 Remove this parameter 29 Move method introduction up to interface

15 Remove target parameter 30 Remove method implementation

Page 12: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

12

Evaluation

We derived existing aspect-oriented refactorings from the laws

We restructured two object-oriented applications modularizing crosscutting concerns with aspects

Page 13: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

13

Refactorings: Extract Pointcut

tsaspect A { pointcut p(ps) : exp … before(ps) : p (ps) { body } … after(ps) : p (ps) { body’ } …}

=

tsaspect A { … before(ps) : exp { body } … after(ps) : exp { body’ } …}

Page 14: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

14

Extract Pointcut

Preconditions

Summary• Law 12 – Extract named pointcut• Law 32 – Use named poitncut

() There is no pointcut named p in pcs;() There is no reference to p in ts and A.

Law 12 Law 32

Page 15: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

15

Extract Method Calls Summary

• Law 2 – Add before-execution• Law 9 – Merge advices• Law 27 – Remove this parameter• Law 28 – Remove argument

parameter

Law 2 Law 9 Law 27 Law 28 Extract Pointcut

Page 16: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

16

Other Refactorings

Extract Interface Implementation Extract Exception Handling Extract Concurrency Control Evolving Product Lines …

Page 17: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

17

Refactoring to AspectJ

General OO Refactorings+

AO Derived Refactorings+

Laws

Before: OO After: AO

Page 18: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

18

Applications

Two commercial applications First

• Mobile Server• Concurrency control

Second• HealthWatcher• Distribution• Problem with Extract Exception

Handling

Page 19: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

19

Conclusions AOP laws useful for deriving aspect-

oriented refactorings Derivation of existing refactorings

• Some not derived Refactored applications Also useful to

• Education• Transformation tools• Provide deeper understanding of the

language

Page 20: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

20

Conclusions Hipotesys

• No packages, this is mandatory, …• No abstract aspects, concurrency and

reflection, … Soundness

Page 21: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

21

Deriving Refactorings for AspectJ

Leonardo Cole Paulo Borba

(lcn,[email protected])

Informatics CenterFederal University of Pernambuco

Brazil

Page 22: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

22

Remove this Parameter

() t is not referenced from body.

=

tspaspect A { … before(ps) : this(T) && … { body } …}

tspaspect A { … before(T t , ps) : this(t) && … { body } …}

Page 23: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

23

Merge Advices

() The set of join points captured by exp1 and exp2 are disjoint.…

=

tspaspect A { ... before(ps) : exp1 || exp2 { body } ...}

tspaspect A { ... before(ps): exp1 { body } before(ps): exp2 { body } ...}

Page 24: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

24

Move field to aspect

() The field field of class C does not appear ints and ms.

=

tsclass C { fs ms}paspect A { T C.field ...}

tsclass C { fs; T field ms}paspect A { ...}

Page 25: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

25

Add before-executiontsclass C { fs ms T m(ps) { body’; body }}

tsclass C { fs ms T m(ps) { body }}

=

paspect A { pcs bars before(context) : exec(C.m) && bind(context) { body’ } afs}

paspect A { pcs bars afs}

Page 26: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

26

Extract Method Calls Examplepublic class Account { private double balance; ... public void debit(double amount) { Access.check(new Permission(…)); //debit logic } public void credit(double amount) {…} ...}public aspect PermissionAspect {}

Page 27: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

27

Applying Add Before-Execution

public class Account { private double balance; ... public void debit(double amount) { //debit logic } public void credit(double amount) {…} ...}public aspect PermissionAspect { before(…): execution(void Account.debit(double) && … { Access.check(new Permission(…)); } before(…): execution(void Account.credit(double) && … { Access.check(new Permission(…)); } }

Page 28: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

28

Another Examplepublic class Bank { private Transaction transaction; ... public void remove(Account account) { transaction.begin(…); try { // remove account logic transaction.end(…) } catch (…) { transaction.rollBack(…); } } ...}

Page 29: 1 Deriving Refactorings for AspectJ Leonardo Cole Paulo Borba (lcn,phmb@cin.ufpe.br) Informatics Center Federal University of Pernambuco Brazil

29

Applying laws Add before-execution Add after-execution returning Add after-execution throwing

public class Bank { private Transaction transaction; ... public void remove(Account account) { // remove account logic } ...}aspect TransactionAspect {…}