Title: The AspectJ Programming Language Part II: Advice and Static Crosscutting
1The AspectJ Programming LanguagePart II Advice
and Static Crosscutting
- CS 5382
- Spring 2009
- The AspectJ Programming Guide, available from
- http//www.eclipse.org/aspectj/doc/released/proggu
ide/index.html
2Outline
- Advice
- before, after, and around advice
- Static crosscutting
- Member introduction
- Modifying class hierarchy
- Compile-time warning and error
3Advice is
additional action to take at join points
- before before proceeding at join point
- after returning a value to join point
- after throwing a Throwable to join point
- after returning to join point either way
- around on arrival at join point gets explicit
control over when and if program - proceeds
4Meanings of Advice
setX()
return
5Anatomy of Advice
advice body
pointcut specification
advice declaration
before(Point p) call(void Point.setX(int))
target(p) System.out.println(Changing
x-coordinate of p) after(Point p)
call(void Point.setX(int)) target(p)
System.out.println(Changed x-coordinate of
p) int around() call(int Point.getX())
int x proceed() return x 10
6Advice Syntax
- AspectMemberDecl
- PointcutDeclaration AdviceDeclaration
- AdviceDeclaration
- StrictFPOpt AdviceType Pointcut Body
- StrictFPOpt strictfp
- AdviceType
- before ( Formals )
- after ( Formals )
- after ( Formals ) returning ResultOpt
- after ( Formals ) throwing ResultOpt
- Type around ( Formals ) ThrowsListOpt
- ResultOpt ( Formal )
- ThrowsListOpt throws TypeList
7Examples of Advice Types
- after (int x) returning
- after (int x) returning (int ret)
- after() throwing
- after() throwing (Exception e)
- int around(Point p) throws Exception
8Passing Context to Advice
- Value is pulled
- Right to left across left side
right side - From pointcut designator to user-defined pointcut
designator - From pointcut to advice
pointcut setOperation(Point p , int x )
call(void Point.set(int)) target( p )
args( x ) before(Point p , int x )
setOperation( p , x ) System.out.println(Be
fore setting p with x )
9Passing Context (Cont.)
after() returning (int x ) call(int
Point.get()) System.out.println(Setting
to x ) after() throwing
(InvalidArgumentException e ) call(void
Point.set(int)) System.out.println(Excepti
on e while executing
thisJoinPoint)
10Exercise
- Given the following interface, write an aspect to
monitor the rate - of service failures.
import java.rmi.RemoteException public interface
RemoteService Object getService() throws
RemoteException
11Around Advice
- Surrounds join points
- Bypass the execution of the join points or
- Execute the join points with the same or
different arguments
float around(float x) call(float
Math.sqrt(float) args(x) if (x lt 0)
return proceed(-x) else if (x 0)
return 0 else return proceed(x)
proceed with diff. argument
bypassing the join point
proceed with same argument
Proceed arguments should match the advices
parameters.
12Returning from Around Advice
- What if return types are different?
- Automatic wrapping and unwrapping by AspectJ
Object around() call( get()) Object
result proceed() System.out.println(Got
result) return result // assume l is of
type Line Point p l.getP1() int x p.getX()
Matches int Point.getX() int Point.getY() Point
Line.getP1() Point Line.getP2()
13Exercise
- Given the following interface, write an aspect
that automatically - retries MAX_RETRIES times if a service request
fails.
import java.rmi.RemoteException public interface
RemoteService int getIntService() throws
RemoteException String getStringService()
throws RemoteException
14Advice vs. Method
- Like methods,
- Follows access control rules to access members of
other types and aspects - Declares that it can throw checked exceptions
- Can refer to the aspect instance using this
- Unlike methods,
- Does not have a name
- Cannot be called directly
- Does not have an access specifier
- Has access to a few special variables
thisJoinPoint, thisJoinPointStaticPart,
thisEnclosingJoinPointStaticPart
15Exercise
- Given the following factorial method, write an
aspect to cache - the computed values for later use. To limit the
amount of caching, - cache the results for values passed on only to
nonrecursive calls.
public class Math public static long
factorial(int x) return x lt 1 ? 1 x
factorial(x -1)
16Static Crosscutting
- To affect the static structure in a crosscutting
manner, i.e., - Modifies the static structure of types and their
compile-time behavior - Four kinds
- Member introduction
- Modifying class hierarchy
- Introducing compile-time errors and warning
- Exception softening
17Member Introduction
- AspectMemberDecl
- IntroductionDecl
- IntroductionDecl ModifiersOpt Type
TypePattern.Id - ModifiersOpt Type TypePattern.Id Expression
- ModifiersOpt Type TypePattern.Id ( Formals )
Body - ModifiersOpt Type TypePattern.Id ( Formals )
- ModifiersOpt TypePattern.new ( Formals )
18Example Adding Color to Points
import java.awt.Color public aspect AddColor
private Color Point.color Color.WHITE
public Point.new(int x, int y, Color c)
this(x, y) color c public
void Point.setColor(Color c) color c
public Color Point.getColor()
return color
adding field
adding constructor
adding method
adding method
Q. What if ommit Point in private Color
Point.color Color.WHITE?
19Exercise
- Write an aspect to store the history of movements
of each - FigureElement object. (Hint In AspectJ, you can
also introduce fields - and methods to interfaces.)
20Modifying Class Hierarchy
- AspectMemberDecl
- DeclareParentDecl
- DeclareParentDecl
- declare parents TypePattern extends Type
- declare parents TypePattern implements
TypeList
aspect MakeDisplayable declare parents
Point implements Displayable public void
Point.draw(Graphics g) // draw this on g.
21Modifying Class Hierarchy (Cont.)
- Q. What happens if nothing matches the type
pattern? - Q. What if a class matching the type pattern
already has a superclass? - Q. What happen if the new interface demands
method that arent implemented?
22Static Error and Warning
- AspectMemberDecl
- DeclareErrorDecl DeclareWarningDecl
- DeclareErrorDecl declare error PointCut
String - DeclareErrorDecl declare warning PointCut
String
aspect CodeSegregation pointcut dbCode()
call( DriverManager.(..)) pointcut
badDbCode() dbCode() !within(db.)
pointcut reallyBadDbCode() badDbCode()
!within(servlets.) declare warning
badDbCode() Database code outside db
package! declare error reallyBadDbCode()
Database code is not permitted here! Q
What restrictions on the pointcuts are necessary
for this?