Introduction to Aspect-Oriented Software Development · 2005. 10. 26. · “Foundations of AOP for...
Transcript of Introduction to Aspect-Oriented Software Development · 2005. 10. 26. · “Foundations of AOP for...
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
1/24
Introduction toAspect-Oriented Software
Development
Bruno Harbulot
ESNW, the University of Manchesterhttp://www.cs.man.ac.uk/~harbulob/
ELF Developers' Forum – Manchester - October 2005
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
2/24
Presentation outline
● Problem: code-tangling● Concepts of Aspect-Oriented Programming● AOP Tools
– AspectJ
● Applications: Design pattern example● Conclusions
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
3/24
Problem presentation
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
4/24
good modularity
● URL pattern matching in org.apache.tomcat– red shows relevant lines of code– nicely fits in two boxes (using inheritance)
URL pattern matching
(c) Copyright 1998-2002 Palo Alto Research Center Incroporated. All rights reserved.
Sour
ce:
http
://w
ww
.ecl
ipse
.org
/asp
ectj
/tea
chin
g.ph
p
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
5/24
problems like…
● logging in org.apache.tomcat– red shows lines of code that handle logging – not in just one place– not even in a small number of places
logging is not modularized
(c) Copyright 1998-2002 Palo Alto Research Center Incroporated. All rights reserved.
Sour
ce:
http
://w
ww
.ecl
ipse
.org
/asp
ectj
/tea
chin
g.ph
p
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
6/24
Conceptsof
Aspect-OrientedProgramming
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
7/24
Crosscutting concerns
● Concern: “specific requirement or consideration that must be addressed in order to satisfy the overall system goal” [Lad03]
● Designing software: separating concerns into units such as procedures, classes, methods, libraries, etc.
● Two concerns crosscut each other when their relation implies tangled code.
● Crosscutting concern: concern that crosscuts the main purpose of a unit, or that is spanned across multiple units.
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
8/24
Motivation forAspect-Oriented Programming
● Programming paradigm for encapsulating crosscutting concerns.
● AOP builds on top of other programming paradigms: object-oriented, procedural or functional. It does not supplant them.
● Encapsulate crosscutting concerns into aspects.
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
9/24
Concepts of AOP (I)
● Aspect: unit encapsulating a crosscutting concern.● Join point: point in the execution of a program
where an aspect might intervene.● “[...] whenever condition C arises, perform action
A” [Fil05]
– Pointcut: expression of a subset of join points (condition C)
– Advice: piece of code for action A.– Pointcuts and advice encapsulated into aspects.
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
10/24
Concepts of AOP (II)
● AOP is not about “patching” pieces of code.
● AOP is about performing an action systematically upon recognition of a behaviour in the code.
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
11/24
Concepts of AOP (III)
● Weaving
Base units
Aspects
Weaver Application
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
12/24
Aspect-OrientedProgramming
Tools
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
13/24
AOP Tools
● (Java-based tools)● Languages:
– AspectJ (now merged with AspectWerkz)
● Frameworks (mainly for J2EE applications):– JAC– Jboss AOP
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
14/24
AspectJ
● Aspect-Oriented extension to Java.● Aspect language (new constructs for aspects).
● Produces standard Java bytecode.● Weaves into class files.
.class.java
.aj
Weaver .class
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
15/24
AspectJ example: pre-condition
/* Java code */class Test { public int val ;
/* ... */}
/* AspectJ code */aspect Precondition { public static final int MAX_VALUE = 2000 ; before(int newval): set (int Test.val) && args (newval) { if (newval > MAX_VALUE) throw new RuntimeException(“Invalid value”); }}
Piece of advice Pointcut
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
16/24
AspectJ example: persistence
aspect DatabaseAspect { pointcut transactionalMethods (): execution (/* pattern for transactional methods */) ;
before(): transactionalMethods () { initialiseDatabase() ; }
after() returning: transactionalMethods() { commitTransaction() ; }
after() throwing: transactionalMethods() { rollbackTransaction() ; }}
(Soares et. al. Implementing distribution and persistence aspects with AspectJ.)
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
17/24
AspectJ – Pointcuts
● Pointcuts define where to intervene● Expressed from primitive pointcuts:
– call/execution(<Method signature>)– set/get(<Field signature>)– cflow(<Pointcut>)– args, target, this
● pointcut setvalue(int val):call(public void set*(int)) && args(val);
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
18/24
Application:Design patterns
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
19/24
Observer pattern (I)
● In Java, explicit “addObserver” and “notify” embedded the observed class.
● “Observable” feature tangled with the main purpose of the class.
● public void process() { /* Do something */ notifyObservers();}
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
20/24
Observer pattern (II)
● In AspectJ, possible to decouple the “observable” from the main purpose of the class.
● after(): execution(* *.process(..)) { /* notify or perform an action */}
● The “observer” and “observable” can be encapsulated into a single aspect.
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
21/24
Conclusions
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
22/24
Benefits and pitfalls
● Benefits: clearer decomposition of the roles (more reusability)
● Pitfalls:– Learning curve to comprehend the concepts
(eased by Java environment)– Need for tools to understand the overall
behaviour of the application (Eclipse AJDT)http://www.eclipse.org/ajdt/(available for other IDEs as well)
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
23/24
References (I)
● AOSD website: http://www.aosd.net/
● AspectJ: http://www.eclipse.org/aspectj/
● “Foundations of AOP for J2EE Development”by R. Pawlak et al., ISBN: 1590595076
● “Eclipse AspectJ” by A. Colyer et al.,ISBN: 0321245873
● [Lad03] “AspectJ in Action” by R. Laddad,ISBN: 1930110936
● [Fil05] “Aspect-Oriented Software Development” by R. Filman et al., ISBN: 0321219767
Bru
no H
arbulo
t – E
LF D
evel
oper
s' F
oru
m –
Man
ches
ter
- 19/1
0/0
5
24/24
References (II)
● “Design Pattern Implementation in Java and AspectJ”, by J. Hannemann and G. Kiczales., OOSPLA 2002.
● “Deriving Refactorings for AspectJ”, by L. Cole et al., AOSD 2005.
● “Towards a Catalog of Aspect-Oriented Refactorings”, by M. Monteiro et al., AOSD 2005.