You are on page 1of 24

Introduction to

Aspect-Oriented Software
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

Development

Bruno Harbulot
ESNW, the University of Manchester
http://www.cs.man.ac.uk/~harbulob/

ELF Developers' Forum – Manchester - October 2005


1/24
Presentation outline

Problem: code-tangling
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● Concepts of Aspect-Oriented Programming


● AOP Tools
– AspectJ
● Applications: Design pattern example
● Conclusions

2/24
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

3/24
Problem presentation
good modularity
URL pattern matching
Source: http://www.eclipse.org/aspectj/teaching.php
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● URL pattern matching in org.apache.tomcat


– red shows relevant lines of code
– nicely fits in two boxes (using inheritance)
(c) Copyright 1998-2002 Palo Alto Research Center Incroporated. All rights reserved.

4/24
problems like… logging is not modularized
Source: http://www.eclipse.org/aspectj/teaching.php
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05


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
(c) Copyright 1998-2002 Palo Alto Research Center Incroporated. All rights reserved.

5/24
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

6/24
of
Concepts

Programming
Aspect-Oriented
Crosscutting concerns

Concern: “specific requirement or consideration


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

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.
7/24
Motivation for
Aspect-Oriented Programming
Programming paradigm for encapsulating
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

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.

8/24
Concepts of AOP (I)

Aspect: unit encapsulating a crosscutting concern.


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05


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.

9/24
Concepts of AOP (II)

AOP is not about “patching” pieces of code.


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● AOP is about performing an action


systematically upon recognition of a
behaviour in the code.

10/24
Concepts of AOP (III)

Weaving
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

Base units

Weaver Application

Aspects

11/24
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

12/24
Tools
Programming
Aspect-Oriented
AOP Tools

(Java-based tools)
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● Languages:
– AspectJ (now merged with AspectWerkz)
● Frameworks (mainly for J2EE applications):
– JAC
– Jboss AOP

13/24
AspectJ

Aspect-Oriented extension to Java.


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● Aspect language (new constructs for aspects).


● Produces standard Java bytecode.
● Weaves into class files.
.class
.java
Weaver .class
.aj

14/24
AspectJ example: pre-condition
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

/* Java code */
class Test {
public int val ;

/* ... */
}

/* AspectJ code */ Piece of advice Pointcut


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”);
}
}

15/24
AspectJ example: persistence
aspect DatabaseAspect {
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

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.)

16/24
AspectJ – Pointcuts

Pointcuts define where to intervene


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

● 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);

17/24
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

18/24
Application:
Design patterns
Observer pattern (I)

In Java, explicit “addObserver” and “notify”


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

embedded the observed class.


● “Observable” feature tangled with the main
purpose of the class.
● public void process() {
/* Do something */
notifyObservers();
}

19/24
Observer pattern (II)

In AspectJ, possible to decouple the


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

“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.

20/24
Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

21/24
Conclusions
Benefits and pitfalls

Benefits: clearer decomposition of the roles


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

(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)

22/24
References (I)

AOSD website: http://www.aosd.net/


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05


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
23/24
References (II)

“Design Pattern Implementation in Java and


Bruno Harbulot – ELF Developers' Forum – Manchester - 19/10/05

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.

24/24

You might also like