Title: AspectOriented Software Development AOSD
1Aspect-Oriented Software Development (AOSD)
- Adam Batenin
- ab1_at_cs.bath.ac.uk
2The Next Two Lectures
- Lecture 1
- Definitions of aspects and aspect-orientation.
- Problems that AOSD helps to address.
- The programming language AspectJ.
- Lecture 2
- AspectJ continued.
- Other AOPLs.
3What is Aspect-Orientation?
- An approach to modularising concerns that are
difficult to modularise in existing programming
languages. - An aspect is a concern that cross-cuts the
modular structure of a program written in some
language.
Affects many modules at the same time,
possibly in the same way
4Concerns that mainstream PLs modularise well
Data Concerns (abstract data types)
Generalised Procedure
Functions and procedures
Also known as
5When is a concern cross-cutting?
Modelling Language like UML
Most aspect-oriented languages
Generalised Procedure Language
Real-time Constraints
Exception Handling
Class e.g. Stack
Design Patterns
Collaboration e.g. open account
Operation e.g Stack.pop
Product Family
Synchronisation
Persistence
Other?
6Why are cross-cutting concerns a problem?
- Scattering many modules contain code
responsible for implementing a concern. - Tangling a module contains code that is part of
many concerns.
7Example of Scattering and Tangling
Consider a Software Engineering Environment
application
Display a b 5
Syntax Check Syntax correct
Evaluate a 16
Logging intermediate b5 update a 16
a
b
5
8Example of Scattering and Tangling (cont.)
Concerns are implemented as methods on each
abstract syntax tree class
Expression Create() Get()/Set() Eval() Check() Dis
play()
Logger addEntry(..)
Each class calls the Logger after Get( )/Set( )
and other ops.
Literal Create() Get()/Set() Eval() Check() Displa
y()
BinaryOp Create() Get()/Set() Eval() Check() Displ
ay()
UnaryOp Create() Get()/Set() Eval() Check() Displa
y()
UnaryPlus Create() Get()/Set() Eval() Check() Disp
lay()
UnaryMinus Create() Get()/Set() Eval() Check() Dis
play()
Plus Create() Get()/Set() Eval() Check() Display()
Minus Create() Get()/Set() Eval() Check() Display(
)
9Scattering and Tangling
- Scattering Evaluate, Display, Syntax Check and
Logging concerns are scattered across the class
hierarchy making it hard to understand which
classes contribute to the realisation of each
one. - Tangling Each class contains operations or just
lines of code from one or more of Evaluate,
Display, Check Syntax or Logging, making it hard
to understand which operation or statement is
part of which concern.
10Aspect-Oriented Programming
- Implement the core functionality in the usual way
using a procedural or OO language. - Implement cross-cutting concerns in a modular way
as aspects. - Compile the core program with aspects into a
single program.
11The AspectJ Way
- The core program is written using Java.
- Aspects written in a language that is like Java
but with a few extra keywords. - AspectJ compiler (also known as a weaver)
compiles core program and aspects into standard
byte codes which run on a standard Java virtual
machine. - Latest release of AspectJ weaves both source code
and Java byte codes.
12Join Point Model
- The join point model is the interface between the
core program and aspects, and between aspects. - The set of available join points fundamentally
depends on the underlying language. - Actions of the aspect are stated in relation to
the join points.
13Tracing the Hello World! of AspectJ programs
- Trace the entry and exit from each operation in
the core program. - Without an AOPL wed have to insert traceEntry
and traceExit calls into each method we wanted to
trace.
5
class X void foo(int x)
1
2
Entering X.foo(int)
3
Exiting X.foo(int)
4
14pointcut name and parameters
pointcuts pick out the join points
events in the pointcut consisting of designators
- aspect Tracing
- pointcut anyMethod() execution( (..))
- before() anyMethod()
- System.out.println(Entering
- thisJoinPointStaticPart.getSignature())
-
- after() anyMethod()
- System.out.println(Exiting
- thisJoinPointStaticPart.getSignature())
-
-
advice to execute before or after a join point
15Anatomy of pointcuts designators
- execution(void Point.setX(int)) when a method
body executes. - call( .getY()) when a method is called.
- handler(ArrayOutOfBoundsException) when an
exception handler executes. - target(MyClass) when the target object (the
receiver) is of class MyClass. - cflow(void Test.main()) when the join point is
in the control flow of a Test objects
no-argument main method.
16Designator Composition
- It is possible to compose designators to capture
very specific join points - target(Point) call(int ()) a call to a
method on a Point object that returns an int and
takes no parameters, e.g. int getX() - Also can use ! (not) and (or).
- Pointcuts can be composed together also.
17Anatomy of pointcuts pointcut parameters
- The designators can bind objects as well as match
types and patterns - pointcut setter(Line l, Point p) target(l)
args(p) call(void setPoint1(Point)) -
Binds to the object receiving the call
Binds to the parameter
Parameters can be used in the body of advice
before(Line l, Point p) setter(Line l, Point p)
18Advice
- 3 kinds of advice
- before executes before the join point, e.g.
field update, event throwing, method call or
execute. - after executes after the join point.
- around runs instead of the join point
void around(Point p, int x) target(p)
args(x) call(void setX(int)) if
(p.assertX(x)) proceed(p, x)
p.releaseResources()
Go on with the call
19Static versus Dynamic Cross-cutting
- Before, after and around advice is dynamic. It
affects individual objects. - Introductions affect all instances of a class in
the same way it is static. With introductions - Add methods/fields to an existing class.
- Force a class to implement another interface.
- Create a subclass to an existing class.
20Summary
- Pointcuts describe join points at which something
happens. - Advice is executed at join points defined by
pointcuts. - Introductions change the definitions of existing
classes. - Aspects pointcuts advice introductions.
21 Example The Observer Design Pattern
- Defines a one-to-many dependency between objects
such that when one object changes, all its
dependents are notified and updated
automatically.
A bar chart view
a spreadsheet view
A pie chart view
A 50 B 30 C 20
Change notification Requests, modifications
22Why should Observer pattern be made an aspect?
- It is a pattern of behaviour in OO programs can
apply to many situations. - There is a standardised protocol that can be
implemented in a modular way with aspects. - Inheritance relationship between aspects can be
used to specialise the pattern to each
application.
23Another example of pattern application
Press a button to change colour of labels
Press once
Press a second time
Press a third time
The button is the subject and the labels are the
observers.
24Generic parts of the protocol
interface Subject void addObserver(Observer
obs) void removeObserver(Observer obs)
Vector getObservers() Object getData()
interface Observer void setSubject(Subject
s) Subject getSubject() void update()
25abstract aspect SubjectObserverProtocol
abstract pointcut stateChanges(Subject s)
after(Subject s) stateChanges(s) for (int
i 0 i lt s.getObservers().size() i)
((Observer)s.getObservers().elementAt(i)).update()
private Vector
Subject.observers new Vector() public void
Subject.addObserver(Observer obs)
observers.addElement(obs)
obs.setSubject(this) public void
Subject.removeObserver(Observer obs)
observers.removeElement(obs)
obs.setSubject(null) public Vector
Subject.getObservers() return observers
private Subject Observer.subject null
public void Observer.setSubject(Subject s)
subject s public Subject
Observer.getSubject() return subject
26SubjectObserverProtocol aspect
- The aspect
- Defines an abstract pointcut that extending
subjects can override. - Defines advice that should run after the join
points of the pointcut. - Defines methods by which observers can register
their interest in a subject.
27Application Classes
class Button extends java.awt.Button
Button(Display display) public void
click()
class ColorLabel extends Label
ColorLabel(Display display) final static
Color colors Color.red, Color.blue,
private int colorIndex 0 private int
cycleCount 0 void colorCycle()
28aspect SubjectObserverProtocolImpl extends
SubjectObserverProtocol declare parents
Button implements Subject public Object
Button.getData() return this declare
parents ColorLabel implements Observer
public void ColorLabel.update() colorCycle()
pointcut stateChanges(Subject s)
target(s) call(void Button.click())