SlideShare a Scribd company logo
1 of 63
Download to read offline
Aspect-oriented
Programming
Prof. Dr. Ralf Lämmel
Universität Koblenz-Landau
Software Languages Team
https://github.com/101companies/101repo/tree/master/languages/AspectJ/aspectJSamples
Non-101samples available here:
http://101companies.org/wiki/
Contribution:aspectJ
See special
copyright message
at the end of the
slide deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Elevator pitch
Suppose you need to implement some tracing (aka logging)
functionality in your app. Chances are that code needs to be
scattered allover the app; see below. Can we do better than this.
Yes, use AOP!
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
class Point {
void set(int x, int y) {
TraceSupport.traceEntry("Point.set");
this.x = x; this.y = y;
TraceSupport.traceExit("Point.set");
}
}
class TraceSupport {
static int TRACELEVEL = 0;
static protected PrintStream stream = null;
static protected int callDepth = -1;
static void init(PrintStream _s) {stream=_s;}
static void traceEntry(String str) {
if (TRACELEVEL == 0) return;
callDepth++;
printEntering(str);
}
static void traceExit(String str) {
if (TRACELEVEL == 0) return;
callDepth--;
printExiting(str);
}
}
TraceSupport
Many classes (objects)
interact with the trace
facility.
Tracing without AOP
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Logging is a crosscutting concern.
These concerns don’t fit into traditional modules.
Other examples:
• Error handling
• Synchronization
• Security
• Power management
• Memory management
• Performance optimizations
Costs of tangled code:
• Difficult to understand
• Difficult to change
• Increases with size of system
• Increases maintenance costs
• Very difficult to get rid of, if at all
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Aspect-Oriented Programming
• Crosscutting concerns
– … are inherent to complex systems.
– … serve important purposes.
– … have a natural structure.
– … can be captured in new kinds of modules.
– … require designated language and tool support.
An aspect is a well-modularized crosscutting concern.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Tracing with AOP
414
aspect PointTracing {
pointcut trace():
within(com.bigboxco.boxes.*) &&
execution(* *(..));
before(): trace() {
TraceSupport.traceEntry(tjp);
}
after(): trace() {
TraceSupport.traceExit(tjp);
}
}
class Point {
void set(int x, int y) {
this.x = x; this.y = y;
}
}
The classes (objects) do not to
contain code that anticipates the
interaction with the trace facility.
An aspect in the
AspectJ language
– which is a Java
extension
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Aspects – benefits in design and coding
• Objects no longer responsible for using the trace facility.
• Trace aspect encapsulates that tracing responsibility.
• Changing the tracing concern affects one module.
• Removing tracing from the design is trivial.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
AspectJ – an implementation of AOP
• Well-integrated extension to Java
• A general-purpose AOP language
• Compiles to JVM-compatible .class files
• Semantics relies on load-time weaving
• IDE support for Eclipse et al.: AJDT
• Freely available implementation
• Invented in the 90-ties at XEROX Parc
• Now maintained and used by IBM
• Other major AOP implementations for Java:
– JBoss
– AspectWerkz
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Logistics
Recommended working environment for AspectJ
Recent Eclipse
AJDT (includes AspectJ compiler)
Use Eclipse UPDATE to install AJDT.
Eclipse/AspectJ tips:
Make sure weaving is enabled.
Make use of IDE hints to study weaving.
Simple examples
(demos)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Scenario
• Report when “main” method is called.
• Report when execution is completed.
“Hello World” of AspectJ
DEMO
See package helloworld
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Scenario
• Refuse negative withdrawal.
• Refuse negative deposits.
• Refuse negative balance.
A safer account class
420
DEMO
See package accounts
AOP language
concepts based on a
bigger example
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
operations that move elements
factory methods
Display
*
2Point
getX()
getY()
setX(int)
setY(int)
moveBy(int, int)
Line
getP1()
getP2()
setP1(Point)
setP2(Point)
moveBy(int, int)
Figure
makePoint(..)
makeLine(..)
FigureElement
moveBy(int, int)
A simple gure editor
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A simple gure editor
class Line implements FigureElement{
private Point p1, p2;
Point getP1() { return p1; }
Point getP2() { return p2; }
void setP1(Point p1) { this.p1 = p1; }
void setP2(Point p2) { this.p2 = p2; }
void moveBy(int dx, int dy) { ... }
}
class Point implements FigureElement {
private int x = 0, y = 0;
int getX() { return x; }
int getY() { return y; }
void setX(int x) { this.x = x; }
void setY(int y) { this.y = y; }
void moveBy(int dx, int dy) { ... }
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A crosscutting concern: gure updating
• Figures are collections of figure elements.
• The latter move frequently.
• Displays show figures.
• Displays need to be updated when moves happen.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Join points along the execution of method call
(Move a line, and in turns its points)
a Line
a Point
returning or throwing
dispatch
dispatch
a method call
returning or throwing
a method execution
returning or throwing
a method execution
myObject.moveBy(2, 2)
myObject.p1.moveBy(2, 2)
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Join-point terminology
• Several kinds of join points
– method & constructor call
– method & constructor execution
– field get & set
– exception handler execution
– static & dynamic initialization
a Line
dispatch
method call
join points
method
execution join
points
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
The language construct
“pointcut”
• Pointcuts provide a means to identify join points.
• A pointcut is a kind of predicate on join points that:
– can match or not match any given join point, and
– optionally, pulls out some of the values at that join point.
• Example:
call(void Line.setP1(Point))
• Meaning:
Matches if the join point is a method call with this signature.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pointcuts compose like predicates, using &&, || and !.
Example:
Meaning:
Composition of pointcuts
Matches whenever a Line receives a
“void setP1(Point)” or “void setP2(Point)” method call.
a “void Line.setP2(Point)” call
or
a “void Line.setP1(Point)” call
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Anonymous and named pointcuts
• Pointcuts can be named – for reuse.
• Example:
pointcut move():
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
After advice:
actions to be taken after computation at join point
pointcut move():
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));
after() returning: move() {
<code here runs after each move>
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A simple aspect for
display updating
aspect DisplayUpdating {
pointcut move():
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));
after() returning: move() {
Display.update();
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Tangled code without an aspect
• Update calls are tangled through the code.
• Discipline of updating must be checked by full inspection.
• BTW, “weaving” effectively results in this code.
class Line {
private Point p1, p2;
Point getP1() { return p1; }
Point getP2() { return p2; }
void setP1(Point p1) {
this.p1 = p1;
Display.update();
}
void setP2(Point p2) {
this.p2 = p2;
Display.update();
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pointcuts – some elaborations
• Crosscutting multiple classes
• Crosscutting based on interface signatures
pointcut move():
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int));
pointcut move():
call(void FigureElement.moveBy(int, int)) ||
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int));
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pick up values at join point
Example: the target of a join point
Variable fe is bound to type by pointcut declaration.
Variable fe is bound to value at join point.
Advice can access value.
pointcut move(FigureElement fe):
target(fe) &&
(call(void FigureElement.moveBy(int, int)) ||
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int)));
after(FigureElement fe) returning: move(fe) {
<fe is bound to the figure element>
}
parameter
mechanism
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pick up values at join point
Example: the arguments of an intercepted method
Variables x and y are bound to type by pointcut declaration.
Variables x and y are bound to values at join point.
Advice can access arguments.
pointcut move(int x, int y):
args(x, y) &&
call(void FigureElement.moveBy(int, int)
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Recall: the earlier aspect
for display updating
aspect DisplayUpdating {
pointcut move():
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));
after() returning: move() {
Display.update();
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A revision of the aspect
for display updating
aspect DisplayUpdating {
pointcut move(FigureElement fe):
target(fe) &&
(call(void FigureElement.moveBy(int, int)) ||
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int)));
after(FigureElement fe) returning: move(fe) {
Display.update(fe);
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Intermediate summary
• A design concern is crosscutting (CC) if:
– involves several objects or operations, and
– implemented w/o AOP leads to distant code locations
• doing the same thing
• doing a coordinated single thing
• Expected benefits of aspectual modularization of CC:
– Good modularity, even in the presence of crosscutting concerns
• less tangled code, more natural code, smaller code
• easier to maintain and to evolve
• easier to reason about, debug, change
• more reusable
• more possibilities for generalization, plug and play
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pointcuts cont’d
call(void Point.setX(int))
method/constructor call join points (actual method call)
execution(void Point.setX(int))
method/constructor execution join points (actual running method)
initialization(Point)
object initialization join points
staticinitialization(Point)
class initialization join points (as the class is loaded)
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pointcuts cont’d
• Field reference (“right value”)
Example:
get( int Point.x )
Meaning: match if field x of type int of an object of class
Point is referenced in right-value manner.
• Assignment (“left value”)
Example:
set( int Point.x )
Meaning: match if field x of type int of an object of class
Point is referenced in left-value manner.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pointcuts cont’d
this( TypeName )
within( TypeName )
withincode( MemberSignature )
any join point at which
currently executing object is an instance of type name
currently executing code is contained within type name
currently executing code is specified methods or constructors
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
AspectJ -- How does it work?
Two kinds of classes:
a) Classes compiled by AspectJ
b)Classes not compiled by AspectJ
Two weaving approaches:
a) Generated bytecode contains aspects.
b)Preexisting bytecode is transformed.
before
after
around
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Controlling actions at join point
• before before proceeding at join point
• after returning a value at join point
• after throwing a throwable at join point
• after returning at join point either way
• around on arrival at join point gets explicit
control over when&if computation proceeds
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
An example to illustrate before/after/around
“Contract checking”
• Pre-conditions
–check whether parameter is valid
• Post-conditions
–check whether values were set
• Condition enforcement
–force parameters to be valid
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Before advice: assert preconditions
aspect BoundPointPreCondition {
before(int newX):
call(void Point.setX(int)) && args(newX) {
assert newX >= MIN_X;
assert newX <= MAX_X;
}
before(int newY):
call(void Point.setY(int)) && args(newY) {
assert newY >= MIN_Y;
assert newY <= MAX_Y;
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Pick up values at join point
Example: the arguments of an matched call
before(int newX):
call(void Point.setX(int)) && args(newX) {
assert newX >= MIN_X;
assert newX <= MAX_X;
}
Pointcut parameter,
as used previously,
see “target”.
Bind parameter to
arguments of
matched call
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
After advice: assert postconditions
aspect BoundPointPostCondition {
after(Point p, int newX) returning:
call(void Point.setX(int)) &&
target(p) && args(newX) {
assert p.getX() == newX;
}
after(Point p, int newY) returning:
call(void Point.setY(int)) &&
target(p) && args(newY) {
assert p.getY() == newY;
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Around advice: make calls valid
aspect BoundPointEnforcement {
void around(int newX):
call(void Point.setX(int)) && args(newX) {
proceed( clip(newX, MIN_X, MAX_X) );
}
void around(int newY):
call(void Point.setY(int)) && args(newY) {
proceed( clip(newY, MIN_Y, MAX_Y) );
}
private int clip(int val, int min, int max) {
return Math.max(min, Math.min(max, val));
}
}
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Context of around advice
For each around advice with the signature
ReturnType around(T1 arg1, T2 arg2, …)
there is a special method with the signature
ReturnType proceed(T1, T2, …)
available only in around advice.
Meaning: “run what would have ran if this advice had not been defined”
See copyright notice
elsewhere in this deck.
cflow
Control-flow-related pointcuts
cflow( Pointcut )
all join points in the dynamic control flow of any join
point picked out by Pointcut
cflowbelow( Pointcut )
all join points in the dynamic control flow below any
join point picked out by Pointcut
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A revision of the aspect for
display updating
aspect DisplayUpdating {
pointcut move(FigureElement fe):
target(fe) &&
(call(void FigureElement.moveBy(int, int)) ||
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int)));
pointcut topLevelMove(FigureElement fe):
move(fe) && !cflowbelow(move(FigureElement));
after(FigureElement fe) returning: topLevelMove(fe) {
Display.update(fe);
}
}
See copyright notice
elsewhere in this deck.
compile-time
advice
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Another example:
Enforce factory methods makePoint and ...Line
class Figure {
public Line makeLine(Line p1, Line p2) { new Line... }
public Point makePoint(int x, int y) { new Point... }
...
}
aspect FactoryEnforcement {
pointcut illegalNewFigureElement():
(call(Point.new(..)) || call(Line.new(..)))
&& !withincode(* Figure.make*(..));
before(): illegalNewFigureElement() {
throw new Error("Use factory method instead.");
}
}
See copyright notice
elsewhere in this deck.
new is
rejected
We use
wildcards.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
“Compile-time advice”
• Normally pointcuts define points in the actual execution of
the program to be affected by advice.
• We can also define a pointcut just to produce a compile-
time action (typically an error) for any code location that
matches with the pointcut.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
“compile-time advice” as an improvement
Compile error if factory methods are bypassed
class Figure {
public Line makeLine(Line p1, Line p2) { new Line... }
public Point makePoint(int x, int y) { new Point... }
...
}
aspect FactoryEnforcement {
pointcut illegalNewFigureElement():
(call(Point.new(..)) || call(Line.new(..)))
&& !withincode(* Figure.make*(..));
declare error: illegalNewFigureElement():
"Use factory method instead.";
}
}
Special kind
of advice
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
“compile-time advice” as an improvement
Compile error if factory methods are bypassed
class Figure {
public Line makeLine(Line p1, Line p2) { new Line... }
public Point makePoint(int x, int y) { new Point... }
...
}
aspect FactoryEnforcement {
pointcut illegalNewFigureElement():
call(FigureElement+.new(..))
&& !withincode(* Figure.make*(..));
declare error: illegalNewFigureElement():
"Use factory method instead.";
}
}
Use
subtyping
wildcard
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Display updating with
compile-time factory check
DEMO
See package gures
inter-type
declarations
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Inter-type declarations
• An orthogonal concept compared to advice.
• Purpose: add members to existing types:
– (static and instance) fields
– (static and instance) methods, virtual ones included
– …
• Syntax: prefix member name by target class:
For instance:
public int Point.numbersOfPoints;
• New members are visible in aspect only.
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Scenario
• An object model for an expression language.
• The classes do not support any operations.
• Add operations for evaluation and pretty-printing.
Solving the expression problem
with inter-type declarations
DEMO
See package expressions
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
http://101companies.org/wiki/
Contribution:aspectJ
DEMO
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Summary
• More seriously:
• AOP helps avoiding code tangling.
• AOP helps with code modularization.
• AOP supports modularization of crosscutting concerns.
• Less seriously:
• AOP is an intriguing way to break encapsulation.
• AOP is all about tracing and logging.
• AOP has been obsoleted in Cobol.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Byte-code transformation based
on API for byte-code engineering
(A technique for AOP language implementation)
• Approach:
–Load .class file
–Use API to analyze and transform byte code.
–Then, either:
• Load result into JVM, or
• Save result into same or different .class file.
• BTW:
• This is a form of meta-programming!
• This is borderline reflection.
(We are using Java to transform byte code.)
Not covered in the
lecture. This is for
those interested.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Scenario
• Add timing code to a method in an existing .class file.
• Update .class file itself; transformation is effective upon load.
This is demo-only subject. We don’t go any deeper into it.
DEMO
Byte-code engineering
See package bcel
Not covered in the
lecture. This is for
those interested.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Language extension – summary
• Pointcuts
– Pick out join points (“points in the execution of the program”) and
values at those points (arguments, results, exceptions)
• Advice
– Additional action to take at join points in a pointcut
• Before
• After (returning or throwing)
• Around
• Inter-type declarations (aka “open classes”)
• Aspect
– A modular unit of crosscutting behavior
– A generalization of the class form of modular unit
– Comprised of declarations for:
• Advice
• Inter-types
• Pointcuts
• Fields, constructors, and methods
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Wildcards in pointcuts
• target(Point)
• target(graphics.geom.Point)
• target(graphics.geom.*) any type in graphics.geom
• target(graphics..*) any type in any sub-package of graphics
• call(void Point.setX(int))
• call(public * Point.*(..)) any public method on Point
• call(public * *(..)) any public method on any type
• call(void Point.setX(int))
• call(void Point.setY(*))
• call(void Point.set*(*))
• call(void set*(*)) any setter
• call(Point.new(int, int))
• call(new(..)) any constructor
“*”is
wild
card
“..”is
m
ulti-partwild
card
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Other AspectJ concepts
• Precedence among multiple aspects
• Pointcuts:
–Initialization
–Exceptions
• Reflection for join point
• Abstract pointcuts
• Aspect inheritance
• Subtyping constraints
• ...
See copyright notice
elsewhere in this deck.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Further reading
• AspectJ/AOP tutorials
[1] Intro at eclipse.org
[2] The seminal “AspectJ overview”
[3] “I want my AOP” (provides sources, too)
[4] “The Paradoxical Success of AOP”
• AspectJ sample code:
– Install AspectJ and go to doc/examples directory
Examples are included in the suite for the present lecture.
– Some additional pointers:
• http://www.aspectprogrammer.org/eclipseaspectj/
• http://mail.eclipse.org/aspectj/sample-code.html
• http://www.cs.wustl.edu/~mdeters/seminar/fall2002/notes/code/
• http://stderr.org/doc/aspectj-doc/examples/
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Copyright notice
This slide deck adopts a considerable amount of
slides from the following deck: © 2004 “Aspect-
Oriented Programming with AspectJ™” by Julie
Waterhouse, Mik Kersten, eclipse.org/aspectj,
IBM, UBC; http://kerstens.org/mik/publications/
aspectj-tutorial-oopsla2004.ppt. That deck has served
as an OOPSLA 2004 tutorial. Most of the slides
showed up in many other decks by the key
representatives of AspectJ. If you derive any work
from the present slide deck, you must keep the
copyright notice in tact. All remaining copyright, if
any, is with Ralf Lämmel.

More Related Content

What's hot

Towards an SMT-based approach for Quantitative Information Flow
Towards an SMT-based approach for Quantitative Information FlowTowards an SMT-based approach for Quantitative Information Flow
Towards an SMT-based approach for Quantitative Information FlowQuoc-Sang Phan
 
OpenFOAM for beginners: Hands-on training
OpenFOAM for beginners: Hands-on trainingOpenFOAM for beginners: Hands-on training
OpenFOAM for beginners: Hands-on trainingJibran Haider
 
Intro to Functional Reactive Programming In Scala
Intro to Functional Reactive Programming In ScalaIntro to Functional Reactive Programming In Scala
Intro to Functional Reactive Programming In ScalaDiego Alonso
 
Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21alowblow
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreterRoberto Nogueira
 
Automated Repair - ISSTA Summer School
Automated Repair - ISSTA Summer SchoolAutomated Repair - ISSTA Summer School
Automated Repair - ISSTA Summer SchoolAbhik Roychoudhury
 
OpenFOAM Training v5-1-en
OpenFOAM Training v5-1-enOpenFOAM Training v5-1-en
OpenFOAM Training v5-1-enCyprien Soulaine
 
Hidden Truths in Dead Software Paths
Hidden Truths in Dead Software PathsHidden Truths in Dead Software Paths
Hidden Truths in Dead Software PathsBen Hermann
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...Frank Nielsen
 
Dot Call interface
Dot Call interfaceDot Call interface
Dot Call interfaceHao Chai
 
EclipseCon Eu 2015 - Breathe life into your Designer!
EclipseCon Eu 2015 - Breathe life into your Designer!EclipseCon Eu 2015 - Breathe life into your Designer!
EclipseCon Eu 2015 - Breathe life into your Designer!melbats
 
C programming session 02
C programming session 02C programming session 02
C programming session 02AjayBahoriya
 

What's hot (17)

Towards an SMT-based approach for Quantitative Information Flow
Towards an SMT-based approach for Quantitative Information FlowTowards an SMT-based approach for Quantitative Information Flow
Towards an SMT-based approach for Quantitative Information Flow
 
OpenFOAM for beginners: Hands-on training
OpenFOAM for beginners: Hands-on trainingOpenFOAM for beginners: Hands-on training
OpenFOAM for beginners: Hands-on training
 
Intro to Functional Reactive Programming In Scala
Intro to Functional Reactive Programming In ScalaIntro to Functional Reactive Programming In Scala
Intro to Functional Reactive Programming In Scala
 
Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21Programming c sharp 3rd ch6 7 21
Programming c sharp 3rd ch6 7 21
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreter
 
Automated Repair - ISSTA Summer School
Automated Repair - ISSTA Summer SchoolAutomated Repair - ISSTA Summer School
Automated Repair - ISSTA Summer School
 
Lec8
Lec8Lec8
Lec8
 
OpenFOAM Training v5-1-en
OpenFOAM Training v5-1-enOpenFOAM Training v5-1-en
OpenFOAM Training v5-1-en
 
C programming
C programmingC programming
C programming
 
Abhik-Satish-dagstuhl
Abhik-Satish-dagstuhlAbhik-Satish-dagstuhl
Abhik-Satish-dagstuhl
 
Hidden Truths in Dead Software Paths
Hidden Truths in Dead Software PathsHidden Truths in Dead Software Paths
Hidden Truths in Dead Software Paths
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
 
C# programs
C# programsC# programs
C# programs
 
Dot Call interface
Dot Call interfaceDot Call interface
Dot Call interface
 
Syntutic
SyntuticSyntutic
Syntutic
 
EclipseCon Eu 2015 - Breathe life into your Designer!
EclipseCon Eu 2015 - Breathe life into your Designer!EclipseCon Eu 2015 - Breathe life into your Designer!
EclipseCon Eu 2015 - Breathe life into your Designer!
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 

Viewers also liked

Aspect-Oriented Programming
Aspect-Oriented ProgrammingAspect-Oriented Programming
Aspect-Oriented ProgrammingAndrey Bratukhin
 
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJSeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJTed Leung
 
Aspect oriented architecture
Aspect oriented architecture Aspect oriented architecture
Aspect oriented architecture tigneb
 
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Yan Cui
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingAnumod Kumar
 
Your first step by step tutorial for oracle SOA
Your first step by step tutorial for oracle SOAYour first step by step tutorial for oracle SOA
Your first step by step tutorial for oracle SOAhalimelnagar
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-SideReza Rahman
 

Viewers also liked (7)

Aspect-Oriented Programming
Aspect-Oriented ProgrammingAspect-Oriented Programming
Aspect-Oriented Programming
 
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJSeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
 
Aspect oriented architecture
Aspect oriented architecture Aspect oriented architecture
Aspect oriented architecture
 
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Your first step by step tutorial for oracle SOA
Your first step by step tutorial for oracle SOAYour first step by step tutorial for oracle SOA
Your first step by step tutorial for oracle SOA
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
 

Similar to Aspect-oriented programming with AspectJ (as part of the the PTT lecture)

Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Ralf Laemmel
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Ralf Laemmel
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Ralf Laemmel
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)Ralf Laemmel
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Ralf Laemmel
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.pptbanti43
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Whats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersWhats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersRainer Stropek
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Ralf Laemmel
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Ralf Laemmel
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Raffi Khatchadourian
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Need to make a horizontal change across 100+ microservices? No worries, Sheph...
Need to make a horizontal change across 100+ microservices? No worries, Sheph...Need to make a horizontal change across 100+ microservices? No worries, Sheph...
Need to make a horizontal change across 100+ microservices? No worries, Sheph...Aori Nevo, PhD
 

Similar to Aspect-oriented programming with AspectJ (as part of the the PTT lecture) (20)

Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)Selected design patterns (as part of the the PTT lecture)
Selected design patterns (as part of the the PTT lecture)
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
 
designpatterns_blair_upe.ppt
designpatterns_blair_upe.pptdesignpatterns_blair_upe.ppt
designpatterns_blair_upe.ppt
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Whats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersWhats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ Developers
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)
 
Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)Remote method invocation (as part of the the PTT lecture)
Remote method invocation (as part of the the PTT lecture)
 
Javascript
JavascriptJavascript
Javascript
 
Sync considered unethical
Sync considered unethicalSync considered unethical
Sync considered unethical
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
React native
React nativeReact native
React native
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Need to make a horizontal change across 100+ microservices? No worries, Sheph...
Need to make a horizontal change across 100+ microservices? No worries, Sheph...Need to make a horizontal change across 100+ microservices? No worries, Sheph...
Need to make a horizontal change across 100+ microservices? No worries, Sheph...
 

More from Ralf Laemmel

Keynote at-icpc-2020
Keynote at-icpc-2020Keynote at-icpc-2020
Keynote at-icpc-2020Ralf Laemmel
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structuresRalf Laemmel
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scaleRalf Laemmel
 
An introduction on language processing
An introduction on language processingAn introduction on language processing
An introduction on language processingRalf Laemmel
 
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Ralf Laemmel
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Ralf Laemmel
 
XML data binding
XML data bindingXML data binding
XML data bindingRalf Laemmel
 

More from Ralf Laemmel (7)

Keynote at-icpc-2020
Keynote at-icpc-2020Keynote at-icpc-2020
Keynote at-icpc-2020
 
Functional data structures
Functional data structuresFunctional data structures
Functional data structures
 
Modeling software systems at a macroscopic scale
Modeling software systems  at a macroscopic scaleModeling software systems  at a macroscopic scale
Modeling software systems at a macroscopic scale
 
An introduction on language processing
An introduction on language processingAn introduction on language processing
An introduction on language processing
 
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
Surfacing ‘101’ in a Linked Data manner as presented at SATToSE 2013
 
Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)Database programming including O/R mapping (as part of the the PTT lecture)
Database programming including O/R mapping (as part of the the PTT lecture)
 
XML data binding
XML data bindingXML data binding
XML data binding
 

Recently uploaded

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 

Recently uploaded (20)

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 

Aspect-oriented programming with AspectJ (as part of the the PTT lecture)

  • 1. Aspect-oriented Programming Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team https://github.com/101companies/101repo/tree/master/languages/AspectJ/aspectJSamples Non-101samples available here: http://101companies.org/wiki/ Contribution:aspectJ See special copyright message at the end of the slide deck.
  • 2. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Elevator pitch Suppose you need to implement some tracing (aka logging) functionality in your app. Chances are that code needs to be scattered allover the app; see below. Can we do better than this. Yes, use AOP! See copyright notice elsewhere in this deck.
  • 3. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) class Point { void set(int x, int y) { TraceSupport.traceEntry("Point.set"); this.x = x; this.y = y; TraceSupport.traceExit("Point.set"); } } class TraceSupport { static int TRACELEVEL = 0; static protected PrintStream stream = null; static protected int callDepth = -1; static void init(PrintStream _s) {stream=_s;} static void traceEntry(String str) { if (TRACELEVEL == 0) return; callDepth++; printEntering(str); } static void traceExit(String str) { if (TRACELEVEL == 0) return; callDepth--; printExiting(str); } } TraceSupport Many classes (objects) interact with the trace facility. Tracing without AOP See copyright notice elsewhere in this deck.
  • 4. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Logging is a crosscutting concern. These concerns don’t fit into traditional modules. Other examples: • Error handling • Synchronization • Security • Power management • Memory management • Performance optimizations Costs of tangled code: • Difficult to understand • Difficult to change • Increases with size of system • Increases maintenance costs • Very difficult to get rid of, if at all See copyright notice elsewhere in this deck.
  • 5. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Aspect-Oriented Programming • Crosscutting concerns – … are inherent to complex systems. – … serve important purposes. – … have a natural structure. – … can be captured in new kinds of modules. – … require designated language and tool support. An aspect is a well-modularized crosscutting concern. See copyright notice elsewhere in this deck.
  • 6. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Tracing with AOP 414 aspect PointTracing { pointcut trace(): within(com.bigboxco.boxes.*) && execution(* *(..)); before(): trace() { TraceSupport.traceEntry(tjp); } after(): trace() { TraceSupport.traceExit(tjp); } } class Point { void set(int x, int y) { this.x = x; this.y = y; } } The classes (objects) do not to contain code that anticipates the interaction with the trace facility. An aspect in the AspectJ language – which is a Java extension See copyright notice elsewhere in this deck.
  • 7. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Aspects – benefits in design and coding • Objects no longer responsible for using the trace facility. • Trace aspect encapsulates that tracing responsibility. • Changing the tracing concern affects one module. • Removing tracing from the design is trivial. See copyright notice elsewhere in this deck.
  • 8. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) AspectJ – an implementation of AOP • Well-integrated extension to Java • A general-purpose AOP language • Compiles to JVM-compatible .class files • Semantics relies on load-time weaving • IDE support for Eclipse et al.: AJDT • Freely available implementation • Invented in the 90-ties at XEROX Parc • Now maintained and used by IBM • Other major AOP implementations for Java: – JBoss – AspectWerkz See copyright notice elsewhere in this deck.
  • 9. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Logistics Recommended working environment for AspectJ Recent Eclipse AJDT (includes AspectJ compiler) Use Eclipse UPDATE to install AJDT. Eclipse/AspectJ tips: Make sure weaving is enabled. Make use of IDE hints to study weaving.
  • 11. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Scenario • Report when “main” method is called. • Report when execution is completed. “Hello World” of AspectJ DEMO See package helloworld
  • 12. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Scenario • Refuse negative withdrawal. • Refuse negative deposits. • Refuse negative balance. A safer account class 420 DEMO See package accounts
  • 13. AOP language concepts based on a bigger example
  • 14. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) operations that move elements factory methods Display * 2Point getX() getY() setX(int) setY(int) moveBy(int, int) Line getP1() getP2() setP1(Point) setP2(Point) moveBy(int, int) Figure makePoint(..) makeLine(..) FigureElement moveBy(int, int) A simple gure editor See copyright notice elsewhere in this deck.
  • 15. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A simple gure editor class Line implements FigureElement{ private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; } void setP2(Point p2) { this.p2 = p2; } void moveBy(int dx, int dy) { ... } } class Point implements FigureElement { private int x = 0, y = 0; int getX() { return x; } int getY() { return y; } void setX(int x) { this.x = x; } void setY(int y) { this.y = y; } void moveBy(int dx, int dy) { ... } } See copyright notice elsewhere in this deck.
  • 16. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A crosscutting concern: gure updating • Figures are collections of figure elements. • The latter move frequently. • Displays show figures. • Displays need to be updated when moves happen. See copyright notice elsewhere in this deck.
  • 17. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Join points along the execution of method call (Move a line, and in turns its points) a Line a Point returning or throwing dispatch dispatch a method call returning or throwing a method execution returning or throwing a method execution myObject.moveBy(2, 2) myObject.p1.moveBy(2, 2) See copyright notice elsewhere in this deck.
  • 18. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Join-point terminology • Several kinds of join points – method & constructor call – method & constructor execution – field get & set – exception handler execution – static & dynamic initialization a Line dispatch method call join points method execution join points See copyright notice elsewhere in this deck.
  • 19. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) The language construct “pointcut” • Pointcuts provide a means to identify join points. • A pointcut is a kind of predicate on join points that: – can match or not match any given join point, and – optionally, pulls out some of the values at that join point. • Example: call(void Line.setP1(Point)) • Meaning: Matches if the join point is a method call with this signature. See copyright notice elsewhere in this deck.
  • 20. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pointcuts compose like predicates, using &&, || and !. Example: Meaning: Composition of pointcuts Matches whenever a Line receives a “void setP1(Point)” or “void setP2(Point)” method call. a “void Line.setP2(Point)” call or a “void Line.setP1(Point)” call call(void Line.setP1(Point)) || call(void Line.setP2(Point)); See copyright notice elsewhere in this deck.
  • 21. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Anonymous and named pointcuts • Pointcuts can be named – for reuse. • Example: pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)); See copyright notice elsewhere in this deck.
  • 22. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) After advice: actions to be taken after computation at join point pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)); after() returning: move() { <code here runs after each move> } See copyright notice elsewhere in this deck.
  • 23. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A simple aspect for display updating aspect DisplayUpdating { pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)); after() returning: move() { Display.update(); } } See copyright notice elsewhere in this deck.
  • 24. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Tangled code without an aspect • Update calls are tangled through the code. • Discipline of updating must be checked by full inspection. • BTW, “weaving” effectively results in this code. class Line { private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; Display.update(); } void setP2(Point p2) { this.p2 = p2; Display.update(); } } See copyright notice elsewhere in this deck.
  • 25. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pointcuts – some elaborations • Crosscutting multiple classes • Crosscutting based on interface signatures pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int)); pointcut move(): call(void FigureElement.moveBy(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int)); See copyright notice elsewhere in this deck.
  • 26. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pick up values at join point Example: the target of a join point Variable fe is bound to type by pointcut declaration. Variable fe is bound to value at join point. Advice can access value. pointcut move(FigureElement fe): target(fe) && (call(void FigureElement.moveBy(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int))); after(FigureElement fe) returning: move(fe) { <fe is bound to the figure element> } parameter mechanism See copyright notice elsewhere in this deck.
  • 27. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pick up values at join point Example: the arguments of an intercepted method Variables x and y are bound to type by pointcut declaration. Variables x and y are bound to values at join point. Advice can access arguments. pointcut move(int x, int y): args(x, y) && call(void FigureElement.moveBy(int, int) See copyright notice elsewhere in this deck.
  • 28. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Recall: the earlier aspect for display updating aspect DisplayUpdating { pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point)); after() returning: move() { Display.update(); } } See copyright notice elsewhere in this deck.
  • 29. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A revision of the aspect for display updating aspect DisplayUpdating { pointcut move(FigureElement fe): target(fe) && (call(void FigureElement.moveBy(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int))); after(FigureElement fe) returning: move(fe) { Display.update(fe); } } See copyright notice elsewhere in this deck.
  • 30. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Intermediate summary • A design concern is crosscutting (CC) if: – involves several objects or operations, and – implemented w/o AOP leads to distant code locations • doing the same thing • doing a coordinated single thing • Expected benefits of aspectual modularization of CC: – Good modularity, even in the presence of crosscutting concerns • less tangled code, more natural code, smaller code • easier to maintain and to evolve • easier to reason about, debug, change • more reusable • more possibilities for generalization, plug and play See copyright notice elsewhere in this deck.
  • 31. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pointcuts cont’d call(void Point.setX(int)) method/constructor call join points (actual method call) execution(void Point.setX(int)) method/constructor execution join points (actual running method) initialization(Point) object initialization join points staticinitialization(Point) class initialization join points (as the class is loaded) See copyright notice elsewhere in this deck.
  • 32. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pointcuts cont’d • Field reference (“right value”) Example: get( int Point.x ) Meaning: match if field x of type int of an object of class Point is referenced in right-value manner. • Assignment (“left value”) Example: set( int Point.x ) Meaning: match if field x of type int of an object of class Point is referenced in left-value manner. See copyright notice elsewhere in this deck.
  • 33. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pointcuts cont’d this( TypeName ) within( TypeName ) withincode( MemberSignature ) any join point at which currently executing object is an instance of type name currently executing code is contained within type name currently executing code is specified methods or constructors See copyright notice elsewhere in this deck.
  • 34. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) AspectJ -- How does it work? Two kinds of classes: a) Classes compiled by AspectJ b)Classes not compiled by AspectJ Two weaving approaches: a) Generated bytecode contains aspects. b)Preexisting bytecode is transformed.
  • 36. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Controlling actions at join point • before before proceeding at join point • after returning a value at join point • after throwing a throwable at join point • after returning at join point either way • around on arrival at join point gets explicit control over when&if computation proceeds See copyright notice elsewhere in this deck.
  • 37. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) An example to illustrate before/after/around “Contract checking” • Pre-conditions –check whether parameter is valid • Post-conditions –check whether values were set • Condition enforcement –force parameters to be valid See copyright notice elsewhere in this deck.
  • 38. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Before advice: assert preconditions aspect BoundPointPreCondition { before(int newX): call(void Point.setX(int)) && args(newX) { assert newX >= MIN_X; assert newX <= MAX_X; } before(int newY): call(void Point.setY(int)) && args(newY) { assert newY >= MIN_Y; assert newY <= MAX_Y; } } See copyright notice elsewhere in this deck.
  • 39. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Pick up values at join point Example: the arguments of an matched call before(int newX): call(void Point.setX(int)) && args(newX) { assert newX >= MIN_X; assert newX <= MAX_X; } Pointcut parameter, as used previously, see “target”. Bind parameter to arguments of matched call See copyright notice elsewhere in this deck.
  • 40. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) After advice: assert postconditions aspect BoundPointPostCondition { after(Point p, int newX) returning: call(void Point.setX(int)) && target(p) && args(newX) { assert p.getX() == newX; } after(Point p, int newY) returning: call(void Point.setY(int)) && target(p) && args(newY) { assert p.getY() == newY; } } See copyright notice elsewhere in this deck.
  • 41. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Around advice: make calls valid aspect BoundPointEnforcement { void around(int newX): call(void Point.setX(int)) && args(newX) { proceed( clip(newX, MIN_X, MAX_X) ); } void around(int newY): call(void Point.setY(int)) && args(newY) { proceed( clip(newY, MIN_Y, MAX_Y) ); } private int clip(int val, int min, int max) { return Math.max(min, Math.min(max, val)); } } See copyright notice elsewhere in this deck.
  • 42. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Context of around advice For each around advice with the signature ReturnType around(T1 arg1, T2 arg2, …) there is a special method with the signature ReturnType proceed(T1, T2, …) available only in around advice. Meaning: “run what would have ran if this advice had not been defined” See copyright notice elsewhere in this deck.
  • 43. cflow
  • 44. Control-flow-related pointcuts cflow( Pointcut ) all join points in the dynamic control flow of any join point picked out by Pointcut cflowbelow( Pointcut ) all join points in the dynamic control flow below any join point picked out by Pointcut See copyright notice elsewhere in this deck.
  • 45. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A revision of the aspect for display updating aspect DisplayUpdating { pointcut move(FigureElement fe): target(fe) && (call(void FigureElement.moveBy(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int))); pointcut topLevelMove(FigureElement fe): move(fe) && !cflowbelow(move(FigureElement)); after(FigureElement fe) returning: topLevelMove(fe) { Display.update(fe); } } See copyright notice elsewhere in this deck.
  • 47. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Another example: Enforce factory methods makePoint and ...Line class Figure { public Line makeLine(Line p1, Line p2) { new Line... } public Point makePoint(int x, int y) { new Point... } ... } aspect FactoryEnforcement { pointcut illegalNewFigureElement(): (call(Point.new(..)) || call(Line.new(..))) && !withincode(* Figure.make*(..)); before(): illegalNewFigureElement() { throw new Error("Use factory method instead."); } } See copyright notice elsewhere in this deck. new is rejected We use wildcards.
  • 48. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) “Compile-time advice” • Normally pointcuts define points in the actual execution of the program to be affected by advice. • We can also define a pointcut just to produce a compile- time action (typically an error) for any code location that matches with the pointcut. See copyright notice elsewhere in this deck.
  • 49. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) “compile-time advice” as an improvement Compile error if factory methods are bypassed class Figure { public Line makeLine(Line p1, Line p2) { new Line... } public Point makePoint(int x, int y) { new Point... } ... } aspect FactoryEnforcement { pointcut illegalNewFigureElement(): (call(Point.new(..)) || call(Line.new(..))) && !withincode(* Figure.make*(..)); declare error: illegalNewFigureElement(): "Use factory method instead."; } } Special kind of advice See copyright notice elsewhere in this deck.
  • 50. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) “compile-time advice” as an improvement Compile error if factory methods are bypassed class Figure { public Line makeLine(Line p1, Line p2) { new Line... } public Point makePoint(int x, int y) { new Point... } ... } aspect FactoryEnforcement { pointcut illegalNewFigureElement(): call(FigureElement+.new(..)) && !withincode(* Figure.make*(..)); declare error: illegalNewFigureElement(): "Use factory method instead."; } } Use subtyping wildcard See copyright notice elsewhere in this deck.
  • 51. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Display updating with compile-time factory check DEMO See package gures
  • 53. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Inter-type declarations • An orthogonal concept compared to advice. • Purpose: add members to existing types: – (static and instance) fields – (static and instance) methods, virtual ones included – … • Syntax: prefix member name by target class: For instance: public int Point.numbersOfPoints; • New members are visible in aspect only. See copyright notice elsewhere in this deck.
  • 54. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Scenario • An object model for an expression language. • The classes do not support any operations. • Add operations for evaluation and pretty-printing. Solving the expression problem with inter-type declarations DEMO See package expressions
  • 55. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) http://101companies.org/wiki/ Contribution:aspectJ DEMO
  • 56. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Summary • More seriously: • AOP helps avoiding code tangling. • AOP helps with code modularization. • AOP supports modularization of crosscutting concerns. • Less seriously: • AOP is an intriguing way to break encapsulation. • AOP is all about tracing and logging. • AOP has been obsoleted in Cobol.
  • 57. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Byte-code transformation based on API for byte-code engineering (A technique for AOP language implementation) • Approach: –Load .class file –Use API to analyze and transform byte code. –Then, either: • Load result into JVM, or • Save result into same or different .class file. • BTW: • This is a form of meta-programming! • This is borderline reflection. (We are using Java to transform byte code.) Not covered in the lecture. This is for those interested.
  • 58. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Scenario • Add timing code to a method in an existing .class file. • Update .class file itself; transformation is effective upon load. This is demo-only subject. We don’t go any deeper into it. DEMO Byte-code engineering See package bcel Not covered in the lecture. This is for those interested.
  • 59. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Language extension – summary • Pointcuts – Pick out join points (“points in the execution of the program”) and values at those points (arguments, results, exceptions) • Advice – Additional action to take at join points in a pointcut • Before • After (returning or throwing) • Around • Inter-type declarations (aka “open classes”) • Aspect – A modular unit of crosscutting behavior – A generalization of the class form of modular unit – Comprised of declarations for: • Advice • Inter-types • Pointcuts • Fields, constructors, and methods See copyright notice elsewhere in this deck.
  • 60. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Wildcards in pointcuts • target(Point) • target(graphics.geom.Point) • target(graphics.geom.*) any type in graphics.geom • target(graphics..*) any type in any sub-package of graphics • call(void Point.setX(int)) • call(public * Point.*(..)) any public method on Point • call(public * *(..)) any public method on any type • call(void Point.setX(int)) • call(void Point.setY(*)) • call(void Point.set*(*)) • call(void set*(*)) any setter • call(Point.new(int, int)) • call(new(..)) any constructor “*”is wild card “..”is m ulti-partwild card See copyright notice elsewhere in this deck.
  • 61. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Other AspectJ concepts • Precedence among multiple aspects • Pointcuts: –Initialization –Exceptions • Reflection for join point • Abstract pointcuts • Aspect inheritance • Subtyping constraints • ... See copyright notice elsewhere in this deck.
  • 62. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Further reading • AspectJ/AOP tutorials [1] Intro at eclipse.org [2] The seminal “AspectJ overview” [3] “I want my AOP” (provides sources, too) [4] “The Paradoxical Success of AOP” • AspectJ sample code: – Install AspectJ and go to doc/examples directory Examples are included in the suite for the present lecture. – Some additional pointers: • http://www.aspectprogrammer.org/eclipseaspectj/ • http://mail.eclipse.org/aspectj/sample-code.html • http://www.cs.wustl.edu/~mdeters/seminar/fall2002/notes/code/ • http://stderr.org/doc/aspectj-doc/examples/
  • 63. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Copyright notice This slide deck adopts a considerable amount of slides from the following deck: Š 2004 “Aspect- Oriented Programming with AspectJ™” by Julie Waterhouse, Mik Kersten, eclipse.org/aspectj, IBM, UBC; http://kerstens.org/mik/publications/ aspectj-tutorial-oopsla2004.ppt. That deck has served as an OOPSLA 2004 tutorial. Most of the slides showed up in many other decks by the key representatives of AspectJ. If you derive any work from the present slide deck, you must keep the copyright notice in tact. All remaining copyright, if any, is with Ralf Lämmel.