AspectJ <ul><li>First production-quality AOP-technology </li></ul><ul><li>Allows specifying crosscutting concerns as separ...
a simple figure editor p.  operations that move elements Display * 2 Point -x: int -y: int +getX() +getY() +setX(int) +set...
a simple figure editor p.  class  Line implements FigureElement{ private  Point p1, p2; Point getP1() {  return  p1; } Poi...
join points p.  a Line a Point returning or throwing dispatch dispatch key points in dynamic call graph returning or throw...
join point terminology <ul><li>several kinds of join points </li></ul><ul><ul><li>method & constructor call </li></ul></ul...
join point terminology: p.  all join points on this slide are within the control flow of this join point imagine  l.setXY(...
primitive pointcuts <ul><li>a pointcut is a kind of predicate on join points that: </li></ul><ul><ul><li>can match or not ...
pointcut composition p.  whenever a Line receives a    “ void  setP1(Point)” or “ void  setP2(Point)” method call call( vo...
Pointcuts <ul><li>call(voidFigureElement.setXY(int,int))  || </li></ul><ul><li>call(void Point.setX(int))    || </li></ul>...
named pointcuts <ul><li>defined using the pointcut construct </li></ul><ul><li>can be used in the same way as primitive po...
Name-based and property-based pointcuts <ul><li>All example above are name-based pointcuts </li></ul><ul><li>Property-base...
Example primitive pointcuts <ul><li>when a particular method body executes </li></ul><ul><ul><li>execution(void Point.setX...
Example pointcuts <ul><li>Using wildcards </li></ul><ul><ul><li>execution(* *(..)) </li></ul></ul><ul><ul><li>call(* set(....
Advice <ul><li>before before proceeding at join point </li></ul><ul><li>after returning a value </li></ul><ul><li>after th...
Exposing context in pointcuts <ul><li>Pointcut can explicitly expose certain values </li></ul><ul><li>Advice can use these...
Exposing context in named pointcuts <ul><li>Named pointcuts may have parameters </li></ul><ul><li>When the pointcut is use...
explaining parameters… <ul><li>value is ‘pulled’ </li></ul><ul><ul><li>right to left across ‘ : ’  left side : right side ...
Aspects <ul><li>Wrap up pointcuts and advice in a modular unit </li></ul><ul><li>Is very much like a class, can have metho...
Inter-type declarations <ul><li>Aspects may declare members and fields that cut across multiple existing classes </li></ul...
Examples <ul><li>Development aspects </li></ul><ul><ul><li>Instrumental during development of a Java application </li></ul...
Tracing <ul><li>Prints a message at specified method calls </li></ul><ul><li>Thisjoinpoint is a special variable that is b...
Profiling and Logging <ul><li>Counts the number of calls to the rotate method on a line  </li></ul><ul><li>Counts the numb...
Pre- and post- conditions <ul><li>Checks whether the x and y coordinates of a point stay within give bounderies </li></ul>...
Contract enforcement <ul><li>Identifies a method call that in a correct program should not exist </li></ul><ul><li>Enforce...
Contract enforcement <ul><li>In this example the compiler can signal the error </li></ul>p.  aspect RegistrationProtection...
Change monitoring <ul><li>Supports the code that refreshes the display when a figure element moved </li></ul><ul><li>Whith...
Context passing <ul><li>Captures calls to the factory methods of figure elements within the control flow of all calls to m...
Providing consistent behavior <ul><li>Enshures that all public methods of a given package log any Error they throw to thei...
wildcarding in pointcuts p.  target(Point) target(graphics.geom.Point) target(graphics.geom.*) any type in graphics.geom t...
other primitive pointcuts p.  this(<type name>) within(<type name>) withincode(<method/constructor signature>) any join po...
other primitive pointcuts p.  execution(void Point.setX(int)) method/constructor execution join points (actual running met...
Inheritance <ul><li>Pointcuts can be abstract (only name and arguments) </li></ul><ul><li>An aspect with only abstract poi...
Aspect Reuse <ul><li>AspectJ forces two mechanisms: </li></ul><ul><ul><li>Advice reuse has to be done by extending abstrac...
Upcoming SlideShare
Loading in...5
×

Introduction to AspectJ

2,553

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,553
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
111
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Introduction to AspectJ

    1. 1. AspectJ <ul><li>First production-quality AOP-technology </li></ul><ul><li>Allows specifying crosscutting concerns as separate entities: Aspects </li></ul><ul><li>Introduces: </li></ul><ul><ul><li>Join point : some point in the execution of an application </li></ul></ul><ul><ul><li>Pointcut : a set of logically related join points </li></ul></ul><ul><ul><li>Advice : some behavior that should become active whenever a join point is encountered </li></ul></ul><ul><ul><li>Weaving : a technology for bringing aspects and base code together </li></ul></ul>p.
    2. 2. a simple figure editor p. operations that move elements Display * 2 Point -x: int -y: int +getX() +getY() +setX(int) +setY(int) Line -p1:Point -p2: Point +getP1() +getP2() +setP1(Point) +setP2(Point) factory methods Figure <<factory>> +makePoint(..) +makeLine(..) FigureElement +setXY(int, int) +draw()
    3. 3. a simple figure editor p. 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 setXY( int x, int y) {…} } 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 setXY( int x, int y){…} }
    4. 4. join points p. a Line a Point returning or throwing dispatch dispatch key points in dynamic call graph returning or throwing returning or throwing imagine l.setXY(2, 2) a method call a method execution a method execution
    5. 5. join point terminology <ul><li>several kinds of join points </li></ul><ul><ul><li>method & constructor call </li></ul></ul><ul><ul><li>method & constructor execution </li></ul></ul><ul><ul><li>field get & set </li></ul></ul><ul><ul><li>returning normally from a method call </li></ul></ul><ul><ul><li>returning from a method call by throwing an error </li></ul></ul><ul><ul><li>exception handler execution </li></ul></ul><ul><ul><li>static & dynamic initialization </li></ul></ul>p. a Line dispatch method call join points method execution join points
    6. 6. join point terminology: p. all join points on this slide are within the control flow of this join point imagine l.setXY(2, 2) a Point a Line a Point
    7. 7. primitive pointcuts <ul><li>a pointcut is a kind of predicate on join points that: </li></ul><ul><ul><li>can match or not match any given join point and </li></ul></ul><ul><ul><li>optionally, can pull out some of the values at that join point </li></ul></ul><ul><li>example: call( void Line.setP1(Point)) </li></ul>p. “ a means of identifying join points” matches if the join point is a method call with this signature
    8. 8. pointcut composition p. whenever a Line receives a “ void setP1(Point)” or “ void setP2(Point)” method call call( void Line.setP1(Point)) || call( void Line.setP2(Point)); pointcuts compose like predicates, using &&, || and ! or a “void Line.setP2(Point)” call a “void Line.setP1(Point)” call
    9. 9. Pointcuts <ul><li>call(voidFigureElement.setXY(int,int)) || </li></ul><ul><li>call(void Point.setX(int)) || </li></ul><ul><li>call(void Point.setY(int)) || </li></ul><ul><li>call(void Line.setP1(Point)) || </li></ul><ul><li>call(void Line.setP2(Point)); </li></ul>p. This pointcut captures all the join points where a FigureElement moves <ul><li>Can crosscut types </li></ul><ul><li>Can use interface signatures </li></ul>
    10. 10. named pointcuts <ul><li>defined using the pointcut construct </li></ul><ul><li>can be used in the same way as primitive pointcuts </li></ul>p. pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point )); name parameters more on parameters and how pointcut can expose values at join points in a few slides
    11. 11. Name-based and property-based pointcuts <ul><li>All example above are name-based pointcuts </li></ul><ul><li>Property-based pointcuts specify a pointcut in term of properties of methods other than their name </li></ul>p. call(void Figure.make*(..)) call(public * Figure.* (..)) cflow(move()) wildcard special primitive pointcut
    12. 12. Example primitive pointcuts <ul><li>when a particular method body executes </li></ul><ul><ul><li>execution(void Point.setX(int)) </li></ul></ul><ul><li>when a method is called </li></ul><ul><ul><li>call(void Point.setX(int)) </li></ul></ul><ul><li>when an exception handler executes </li></ul><ul><ul><li>handler(ArrayOutOfBoundsException) </li></ul></ul><ul><li>when the object currently executing (i.e. this ) is of type SomeType </li></ul><ul><ul><li>this(SomeType) </li></ul></ul><ul><li>when the target object is of type SomeType </li></ul><ul><ul><li>target(SomeType) </li></ul></ul><ul><li>when the executing code belongs to class MyClass </li></ul><ul><ul><li>within(MyClass) </li></ul></ul><ul><li>when the join point is in the control flow of a call to a Test 's no-argument main method </li></ul><ul><ul><li>cflow(call(void Test.main())) </li></ul></ul>p.
    13. 13. Example pointcuts <ul><li>Using wildcards </li></ul><ul><ul><li>execution(* *(..)) </li></ul></ul><ul><ul><li>call(* set(..)) </li></ul></ul><ul><li>Select elements based on types </li></ul><ul><ul><li>execution(int *()) </li></ul></ul><ul><ul><li>call(* setY(long)) </li></ul></ul><ul><ul><li>call(* Point.setY(int)) </li></ul></ul><ul><ul><li>call(*.new(int, int) </li></ul></ul><ul><li>Composed pointcuts </li></ul><ul><ul><li>target(Point) && call(int *()) </li></ul></ul><ul><ul><li>call(* *(..)) && (within(Line) || within(Point)) </li></ul></ul><ul><ul><li>within(*) && execution(*.new(int)) </li></ul></ul><ul><ul><li>!this(Point) && call(int *(..)) </li></ul></ul><ul><li>Bases on modifiers and negation of modifiers </li></ul><ul><ul><li>call(public * *(..)) </li></ul></ul><ul><ul><li>execution(public !static * *(..)) </li></ul></ul>p.
    14. 14. Advice <ul><li>before before proceeding at join point </li></ul><ul><li>after returning a value </li></ul><ul><li>after throwing an exception </li></ul><ul><li>after returning either way </li></ul><ul><li>around on arrival at join point gets explicit control over when&if program proceeds </li></ul>p. before(): move() { System.out.println(&quot;about to move&quot;); } after() returning: move() { System.out.println(&quot;just successfully moved&quot;); }
    15. 15. Exposing context in pointcuts <ul><li>Pointcut can explicitly expose certain values </li></ul><ul><li>Advice can use these values </li></ul>p. after(FigureElement fe , int x , int y ) returning: ...SomePointcut... { System.out.println(fe + &quot; moved to (&quot; + x + &quot;, &quot; + y + &quot;)&quot;);} after(FigureElement fe, int x, int y) returning: call(void FigureElement.setXY(int, int)) && target (fe) && args (x, y) { System.out.println(fe + &quot; moved to (&quot; + x + &quot;, &quot; + y + &quot;)&quot;);} Advice declares and use parameter list Pointcuts publish values
    16. 16. Exposing context in named pointcuts <ul><li>Named pointcuts may have parameters </li></ul><ul><li>When the pointcut is used it publishes its parameters by name </li></ul>p. pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) && target(fe) && args(x, y); after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + &quot; moved to (&quot; + x + &quot;, &quot; + y + &quot;).&quot;);} pointcut parameter list Pointcuts publish context
    17. 17. explaining parameters… <ul><li>value is ‘pulled’ </li></ul><ul><ul><li>right to left across ‘ : ’ left side : right side </li></ul></ul><ul><ul><li>from pointcuts to user-defined pointcuts </li></ul></ul><ul><ul><li>from pointcuts to advice, and then advice body </li></ul></ul>p. pointcut move(Line l): target(l) && (call( void Line.setP1(Point)) || call( void Line.setP2(Point))); after (Line line): move(line) { <line is bound to the line> }
    18. 18. Aspects <ul><li>Wrap up pointcuts and advice in a modular unit </li></ul><ul><li>Is very much like a class, can have methods, fields and initialisers </li></ul><ul><li>Instantation is under the control of AspectJ </li></ul><ul><li>By default an aspect is a singleton, only one aspect instance is created </li></ul>p. aspect Logging { OutputStream logStream = System.err; before(): move() { logStream.println(&quot;about to move&quot;); } }
    19. 19. Inter-type declarations <ul><li>Aspects may declare members and fields that cut across multiple existing classes </li></ul>p. aspect PointObserving { private Vector Point.observers = new Vector(); public static void addObserver(Point p, Screen s) { p.observers.add(s); } public static void removeObserver(Point p, Screen s) { p.observers.remove(s); } pointcut changes(Point p): target(p) && call(void Point.set*(int)); after(Point p): changes(p) { Iterator iter = p.observers.iterator(); while ( iter.hasNext() ) { updateObserver(p, (Screen)iter.next()); }} static void updateObserver(Point p, Screen s) { s.display(p); }}
    20. 20. Examples <ul><li>Development aspects </li></ul><ul><ul><li>Instrumental during development of a Java application </li></ul></ul><ul><ul><li>Easily removed from production builds </li></ul></ul><ul><ul><li>Tracing, profiling&logging, checking pre- and post-conditions, contract enforcement </li></ul></ul><ul><li>Production aspects </li></ul><ul><ul><li>To be used in both development and production </li></ul></ul><ul><ul><li>Change monitoring, context passing, providing consistent behavior </li></ul></ul>p.
    21. 21. Tracing <ul><li>Prints a message at specified method calls </li></ul><ul><li>Thisjoinpoint is a special variable that is bound to an object that describes the current joinpoint </li></ul>p. aspect SimpleTracing { pointcut tracedCall(): call(void FigureElement.draw(GraphicsContext)); before(): tracedCall() { System.out.println(&quot;Entering: &quot; + thisJoinPoint ); } }
    22. 22. Profiling and Logging <ul><li>Counts the number of calls to the rotate method on a line </li></ul><ul><li>Counts the number of calls to the set methods of a point that happen within the control flow of those calls to rotate </li></ul>p. aspect SetsInRotateCounting { int rotateCount = 0; int setCount = 0; before(): call(void Line.rotate(double)) { rotateCount++; } before(): call(void Point.set*(int)) && cflow(call(void Line.rotate(double))) { setCount++; } }
    23. 23. Pre- and post- conditions <ul><li>Checks whether the x and y coordinates of a point stay within give bounderies </li></ul>p. aspect PointBoundsChecking { pointcut setX(int x): (call(void FigureElement.setXY(int, int)) && args(x, *)) || (call(void Point.setX(int)) && args(x)); pointcut setY(int y): (call(void FigureElement.setXY(int, int)) && args(*, y)) || (call(void Point.setY(int)) && args(y)); before(int x): setX(x) { if ( x < MIN_X || x > MAX_X ) throw new IllegalArgumentException(&quot;x is out of bounds.&quot;); } before(int y): setY(y) { if ( y < MIN_Y || y > MAX_Y ) throw new IllegalArgumentException(&quot;y is out of bounds.&quot;); }}
    24. 24. Contract enforcement <ul><li>Identifies a method call that in a correct program should not exist </li></ul><ul><li>Enforces the constraint that only the well-known factory methods can add an element to the registry of figure elements </li></ul>p. aspect RegistrationProtection { pointcut register(): call(void Registry.register(FigureElement)); pointcut canRegister(): withincode(static * FigureElement.make*(..)); before(): register() && !canRegister() { throw new IllegalAccessException(&quot;Illegal call &quot; + thisJoinPoint); } }
    25. 25. Contract enforcement <ul><li>In this example the compiler can signal the error </li></ul>p. aspect RegistrationProtection { pointcut register(): call(void Registry.register(FigureElement)); pointcut canRegister(): withincode(static * FigureElement.make*(..)); declare error : register() && !canRegister(): &quot;Illegal call&quot;} }
    26. 26. Change monitoring <ul><li>Supports the code that refreshes the display when a figure element moved </li></ul><ul><li>Whithout AOP every method that updates the position of a figure element should manipulate the dirty bit (or call refresh) </li></ul>p. aspect MoveTracking { private static boolean dirty = false; public static boolean testAndClear() { boolean result = dirty; dirty = false; return result; } pointcut move(): call(void FigureElement.setXY(int, int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)) || call(void Point.setX(int)) || call(void Point.setY(int)); after() returning: move() { dirty = true; } }
    27. 27. Context passing <ul><li>Captures calls to the factory methods of figure elements within the control flow of all calls to methods of a particular client and runs after advice that allows this client to pass context to the new object </li></ul><ul><li>Comes in the place of an extra parameter in all methods from the client method down to the factory methods to pass that context </li></ul>p. aspect ColorControl { pointcut CCClientCflow(ColorControllingClient client): cflow(call(* * (..)) && target(client)); pointcut make(): call(FigureElement Figure.make*(..)); after (ColorControllingClient c) returning (FigureElement fe): make() && CCClientCflow(c) { fe.setColor(c.colorFor(fe)); } }
    28. 28. Providing consistent behavior <ul><li>Enshures that all public methods of a given package log any Error they throw to their caller </li></ul><ul><li>The cflow primitive can be used to avoid logging an exception twice when a method of the package calls another public method of the package </li></ul>p. aspect PublicErrorLogging { Log log = new Log(); pointcut publicMethodCall(): call(public * com.bigboxco.*.*(..)); after() throwing (Error e): publicMethodCall() { log.write(e); } } after() throwing (Error e): publicMethodCall() && !cflow(publicMethodCall()) { log.write(e); }
    29. 29. wildcarding in pointcuts p. 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.getX()) call( void Point.getY()) call( void Point.get*()) call( void get*()) any getter call(Point. new ( int, int )) call( new (..)) any constructor “ *” is wild card “ ..” is multi-part wild card
    30. 30. other primitive pointcuts p. this(<type name>) within(<type name>) withincode(<method/constructor signature>) 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 method or constructor get( int Point.x) set( int Point.x) field reference or assignment join points
    31. 31. other primitive pointcuts p. 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) cflow( pointcut designator ) all join points within the dynamic control flow of any join point in pointcut designator cflowbelow( pointcut designator ) all join points within the dynamic control flow below any join point in pointcut designator
    32. 32. Inheritance <ul><li>Pointcuts can be abstract (only name and arguments) </li></ul><ul><li>An aspect with only abstract pointcuts is called abstract. </li></ul><ul><li>Aspects can extend classes and other abstract aspects. </li></ul><ul><ul><li>AspectJ does not allow inheritance of concrete aspects! </li></ul></ul>p.
    33. 33. Aspect Reuse <ul><li>AspectJ forces two mechanisms: </li></ul><ul><ul><li>Advice reuse has to be done by extending abstract aspects (with concrete advice) </li></ul></ul><ul><ul><li>Poincut reuse has to be done by delegation </li></ul></ul><ul><li>This is an unfortunate consequence of: </li></ul><ul><ul><li>Unnamed advice </li></ul></ul><ul><ul><li>Abstract aspect inheritance limitation </li></ul></ul>p.
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×