Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

24
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

description

Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January. Outline. The need for Aspect-Oriented Programming (AOP) An introduction to AspectJ AspectJ Demos Adopting AOP Further information. The 1-to-1 idea: Good modularity. - PowerPoint PPT Presentation

Transcript of Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Page 1: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming In Eclipse

®

Aspect-Oriented Programming in Eclipsewith AspectJ

Dr Helen Hawkins and Sian January

Page 2: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Outline

The need for Aspect-Oriented Programming (AOP)

An introduction to AspectJ

AspectJ Demos

Adopting AOP

Further information

Page 3: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

The 1-to-1 idea: Good modularity

One requirement in your design maps to one module in your implementation

For example – Chess Piece, Chess Board, Player

Clear, simple, direct mapping

Modules are easy to add

easy to remove

easy to maintain

Page 4: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

The 1-to-1 idea: Good modularity

Socket Creation in Apache Tomcat

Red shows the relevant lines of code

Nicely fits into one package – 3 classes

Page 5: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

When things go wrong…

Logging in Tomcat

Scattered and tangled throughout all the packages

Bad Modularity – 1-to-n mapping

Page 6: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

The trouble with 1-to-n

Redundant code Same fragment of code in many places

e.g. trace.entry(); trace.exit();

Difficult to understand Mapping from the requirement is unclear

Structure is not explicit

Difficult to change Have to find all the code involved

And be sure to change it consistently

And be sure not to break it by accident

Page 7: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

n-to-1 and n-to-n

A consequence of the 1-to-n problem

Modules are no longer well defined

Page 8: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Code View of n-n

try { if (!removed) entityBean.ejbPassivate(); setState( POOLED );} catch (RemoteException ex ) { FFDCEngine.processException( ex,”EBean.passivate()”,”237”, this); destroy(); throw ex;} finally { if (!removed && statisticsCollector != null) { statisticsCollector. recordPassivation(); } removed = false; beanPool.put( this ); if (Logger.isEnabled) { Logger.exit(tc,”passivate”); }}

try { if (!removed) entityBean.ejbPassivate(); setState( POOLED );} catch (RemoteException ex ) { destroy(); throw ex;} finally { removed = false; beanPool.put( this );}

Example: Code to handle EJB Entity bean passivation

Tangled Core Logic

Page 9: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Aspect-Oriented Software Development …

Recognises crosscutting concerns:Are inherent in any complex system

Have a clear purpose

Have a natural structure

Captures crosscutting concerns explicitly: In a modular way

With linguistic and tool support

Page 10: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

AspectJ

An aspect oriented programming (AOP) language

Java language extension

Divides system into core concerns (classes)

crosscutting concerns (aspects)

Broad IDE supportEclipse, Emacs, JBuilder, NetBeans, …

Page 11: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

AOP Concepts

Join points

Pointcuts

Advice

Aspects

Page 12: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Join Points

Events in the execution of a program

Types of Join Points: Method & Constructor call

Method & Constructor execution

• advice execution Field get & set

Exception handler execution

Static & dynamic initialization

Page 13: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

What’s a Pointcut?

Pointcut Picks out join points in a particular system

e.g. call of method ‘foo()’, set of field ‘x’, constructor for object ‘Fred’

Can also expose context from the matched join point

Page 14: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Types of Pointcuts

Event based - well defined execution points, e.g. method calls, executions field gets / sets exception handling object and class initialisation

Scoping - only select join points within a certain scope, e.g. within a set of packages, within the implementation of a method, in the control flow of some event (e.g. downstream of an unsecured call)

Context matching - expose context at the join point, e.g. identity of the caller or target object exception being handled value being put into a field

Page 15: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

What’s Advice?

Java code to execute when conditions of a pointcut are met.

Can be parameterized to process context exposed by the pointcut

Page 16: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Types of Advice

before() Executes before the join point

after() throwing/returning Executes after the join point, execution can be made

conditional on how join point exits (i.e. with/without exception)

around() Executes ‘instead of’ the join point – has a choice about

whether to invoke the original logic

Page 17: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

What is an Aspect?

Aspects are...

At the design level … concerns that crosscut

At the implementation level … a programming construct

Aspect = pointcut + advice

Page 18: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Demos – AJDT

AspectJ Development Tools (AJDT) for EclipseOpen Source

Developed in Hursley

Partnership with AspectJ team

http://www.eclipse.org/ajdt

Page 19: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Demos

Demo 1: A Simple Figure Editor

Demo 2: Web Services Invocation Framework

Page 20: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Web Services Invocation Framework (WSIF)

Middleware component Simple Java API for invoking web services, no matter

how or where they are provided

Released to Apache But IBM wants a version tightly coupled to IBM’s normal

‘qualities of service’

• IBM tracing/monitoring/management

How do we manage this?

Page 21: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Exploring Re-Use: The WSIF Story

org.apache.wsif

WebSphere RAS

WebSphere FFDC

WebSphere PMI

+

WSIF for Open Source Community

Composition WSIF for WebSphere

Page 22: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Applications of AOP

Problem determination Logging, FFDC, performance monitoring

Architectural rule enforcement Contracts, encapsulation, separation (no “up calls”)

Other concerns Security, transactions, persistence, caching/pooling, locking

Open source integration

Page 23: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Adopting AOPreward

time & confidence

auxiliary /infrastructure

core /business

AO Analysis,AO Design,AO Strategy

explorationenforcement

Page 24: Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January

Aspect-Oriented Programming in Eclipse

Further information AspectJ home page: http://www.eclipse.org/aspectj

AJDT home page: http://www.eclipse.org/ajdt

Pick up a book …

Email us: Helen Hawkins: [email protected]

Sian January: [email protected]