Aspect-Oriented Programming with Aspectj

aspect Observing { protected interface Subject { } protected interface Observer { } public void addObserver(Subject s, Observer o) { ... } public void removeObserver(Subject s, Observer o) { ... } public static List getObservers(Subject s) { ... } abstract pointcut changes(Subject s); after(Subject s): changes(s) { Iterator iter = getObservers(s).iterator(); while ( iter.hasNext() ) { notifyObserver(s, ((Observer)iter.next())); } } abstract void notifyObserver(Subject s, Observer o); } Aspect-Oriented Programming with AspectJ (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 83 this is the concrete reuse aspect DisplayUpdating extends Observing { declare parents: FigureElement implements Subject; declare parents: Display implements Observer; pointcut changes(Subject s): target(s) && (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))); void notifyObserver(Subject s, Observer o) { ((Display)o).update(s); } } DisplayUpdating v7 (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 84 design invariants aspect FactoryEnforcement { pointcut newFigElt(): call(FigureElement.new(..)); pointcut inFactory(): within(Point Figure.make*(..)); pointcut illegalNewFigElt(): newFigElt() && !inFactory(); declare error: illegalNewFigElt(): "Must call factory method to create figure elements."; } Aspect-Oriented Programming with AspectJ (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 85 summary dispatch advice before after around inter-type decls Type.field Type.method() declare warning error parents reflection thisJoinPoint thisJoinPointStaticPart pointcuts -primitivecall execution handler get set initialization this target within withincode cflow cflowbelow -user-definedpointcut declaration abstract overriding join points method & constructor call execution field get set exception handler execution initialization aspects crosscutting type (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 86 AspectJ language language mechanisms: crosscutting in the code mechanisms AspectJ provides problem structure examples: crosscutting in the design, and how to use AspectJ to capture that where we have been... ... and where we are going Aspect-Oriented Programming with AspectJ (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 87 using aspects • present examples of aspects in design – intuitions for identifying aspects • present implementations in AspectJ – how the language support can help – putting AspectJ into practice • discuss style issues – objects vs. aspects • when are aspects appropriate? (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 88 example • simple tracing – exposes join points and uses very simple advice • an unpluggable aspect – core program functionality is unaffected by the aspect plug & play tracing Aspect-Oriented Programming with AspectJ (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. (c) Copyright 1998-2002 Palo Alto Research Center Incorporated. All Rights Reserved. 89 tracing without AspectJ