Software Composition Paradigms: Sommersemester 2015
Software Composition Paradigms: Sommersemester 2015
Sommersemester 2015
Radu Muschevici
Software Engineering Group, Department of Computer Science
2015-05-12
1 / 38
Stateful Traits
2 / 38
modifyX
ClassA
getX
setX
required
methods
ClassB
ClassC
getX
setX
getX
setX
getX
setX
Use of trait
Stateful Traits
Fields are private to the scope of the trait that denes them.
T1
c := C new.
c setXT1: 1.
c setXT2: 2.
c setX: 3.
T2
{ Now:
c getXT1 = 1
c getXT2 = 2
c getX = 3 }
x
C
getXT1
setXT1:
x
getX
setX:
x
getXT2
setXT2:
4 / 38
x
getXT1
setXT1:
sum
T2
@@{ xFromT2 -> x }
x
getXT2
setXT2:
c := C new.
c setXT1: 1.
c setXT2: 2.
{ Now:
c getXT1 = 1
c getXT2 = 2
c sum = 3 }
sum
^ xFromT1 + xFromT2
5 / 38
Merging Fields
Can never have two different variables of the same name in the
same scope.
T1
C
@@{w -> x}
c := C new.
c setW: 3.
x
getX
setX:
getW
setW:
T2
@@{w -> y}
y
getY
setY:
{ Now:
c getX = 3
c getY = 3
c getW = 3 }
Beyond Inheritance:
Aspect-Oriented
Programming (AOP)
7 / 38
Motivation
Software Engineering Goals
Promote reuse
Facilitate evolution
Separation of Concerns
The principle of dividing a program into distinct features with as little
overlap in functionality as possible
Separation of concerns is achieved through mechanism of software
decomposition and composition (modularisation).
Problems:
Cross-cutting Concern
Behavior that cuts across the typical divisions of responsibility.
Examples: logging, security, persistence
9 / 38
10 / 38
11 / 38
12 / 38
Logging in org.apache.tomcat
In a modular way,
16 / 38
17 / 38
Obliviousness
Quantications hold over programs that are oblivious to these
quantied statements: P is not aware of A.
In other words, aspects refer to core classes, but classes do not refer
to aspects.
[Filman and Friedman 2000]
18 / 38
Static Crosscutting
Dynamic Crosscutting
19 / 38
http://jastadd.org/
20 / 38
Decl
...
Annotation
Parameter
...
...
Interface
...
Constructor
...
FieldDecl
...
MethodImpl
...
ClassDecl
List getAnnotations()
List getParameters()
List getInterfaces()
Boolean hasConstructor()
List getConstructor()
List getFieldDecls()
List getMethodImpls()
21 / 38
https://eclipse.org/aspectj/
23 / 38
}
25 / 38
26 / 38
27 / 38
Program state
many more3
Recall quantication:
In programs P, whenever condition C arises, perform action A.
Advice
before(int amount) :
call(void Account.deposit(int)) && args(amount) { /* code */ }
when to insert the code: before, after or around each join point
Around advice runs in place of the join point it operates over. It must
be declared with a return type (like a method):
void around(int amount) :
call(void Account.deposit(int)) {
println("Depositing temporarily unavailable");
}
30 / 38
Aspect
Aspect
An aspect is a code unit that encapsulates advice and pointcuts.
An aspect is a advice
code unit
that encapsulates advice and pointcuts.
parameter
pointcuts
aspect Logging {
Logger logger = new Logger();
before(int amount) :
call(void Account.deposit(int)) && args(amount) {
logger.log("deposit amount: " + amount);
advice kind
}
advice body
before(int amount) :
call(boolean Account.withdraw(int)) && args(amount) {
logger.log("withdraw amount: " + amount);
}
}
31 / 38
Weaving
General principle
Merge base program and aspects into an application that only contains
base language constructs.
Example: AspectJ
Merge Java program and AspectJ aspects into standard Java
bytecode.
32 / 38
AOP Benets
33 / 38
34 / 38
AOP Criticism
applies to:
class Account {
void deposit(int amount) {...}
}
Download link:
http://dl.acm.org/citation.cfm?id=1167514
37 / 38
References I
Filman, Robert E. and Daniel P. Friedman (2000). Aspect-Oriented
Programming is Quantication and Obliviousness. In:
Aspect-Oriented Software Development. Addison-Wesley.
Kiczales, Gregor, Erik Hilsdale, Jim Hugunin, Mik Kersten,
Jeffrey Palm, and William Griswold (2001). An Overview of
AspectJ. In: European Conference on Object-Oriented
Programming. Vol. 2072. LNCS. Springer, pp. 327354.
Steimann, Friedrich (2006). The Paradoxical Success of
Aspect-oriented Programming. In: ACM Conference on
Object-Oriented Programming Systems, Languages, and
Applications. OOPSLA 06. ACM Press, pp. 481497.
Tarr, Peri, Harold Ossher, William Harrison, and Stanley M. Sutton Jr.
(1999). N Degrees of Separation: Multi-dimensional Separation of
Concerns. In: International Conference on Software Engineering.
ICSE 99. IEEE Press, pp. 107119.
38 / 38