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

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

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

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

Aspect-oriented Programming

Prof. Dr. Ralf LämmelUniversität Koblenz-LandauSoftware 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.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Elevator pitchSuppose 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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Logistics

Recommended working environment for AspectJRecent EclipseAJDT (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.

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

Simple examples(demos)

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

(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

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

(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

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

AOP language concepts based on a

bigger example

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

operations that move elements

factory methodsDisplay

*

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 figure editor

See copyright noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

A simple figure 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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

A crosscutting concern: figure updating

• Figures are collections of figure elements.• The latter move frequently.• Displays show figures.• Displays need to be updated when moves happen.

See copyright noticeelsewhere in this deck.

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

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

dispatch

a method call

returning or throwinga method execution

returning or throwinga method execution

myObject.moveBy(2, 2)

myObject.p1.moveBy(2, 2)

See copyright noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Pick up values at join pointExample: 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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Pick up values at join pointExample: 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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 AspectJb)Classes not compiled by AspectJTwo weaving approaches:a) Generated bytecode contains aspects. b)Preexisting bytecode is transformed.

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

beforeafter

around

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Pick up values at join pointExample: 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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Context of around advice

For each around advice with the signatureReturnType around(T1 arg1, T2 arg2, …)

there is a special method with the signatureReturnType proceed(T1, T2, …)

available only in around advice.

Meaning: “run what would have ran if this advice had not been defined”

See copyright noticeelsewhere in this deck.

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

cflow

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

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 noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

compile-time advice

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

(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 noticeelsewhere in this deck.

new is rejected

We use wildcards.

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

(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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

“compile-time advice” as an improvementCompile 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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

“compile-time advice” as an improvementCompile 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 noticeelsewhere in this deck.

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

Display updating withcompile-time factory check

DEMO

See package figures

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

inter-type declarations

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

(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 noticeelsewhere in this deck.

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

(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 problemwith inter-type declarations

DEMO

See package expressions

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

(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)

http://101companies.org/wiki/Contribution:aspectJ

DEMO

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

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

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

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

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

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

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

(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 noticeelsewhere in this deck.

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

(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 multi-part wild card

See copyright noticeelsewhere in this deck.

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

(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 noticeelsewhere in this deck.

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

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

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

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