Aspect-Oriented Programming Andrey Bratukhin, 2011
Contents <ul><li>Introduction
Main conceptions in AOP
AspectJ as one of implementation
Pointcuts in AspectJ
Advice in AspectJ
Inter-type declaration
Weaving
Bad practicies
Conclusion </li></ul>
Introduction <ul><li>Gregor Kiczales and colleagues at Xerox PARC developed the explicit concept of AOP, and followed this...
Aspect-oriented programming is a way of modularizing crosscutting concerns much like object-oriented programming is a way ...
Motivation void  transfer ( Account fromAcc, Account toAcc, int amount, User user, Logger logger ) throws  Exception  { lo...
Tangled code
Dispersed code Module #1 Module #1 Module #1 Security API calls Security module
Extract common concerns Module #1 Module #1 Module #1 Automated integration of calls Security module Security  aspect
Upcoming SlideShare
Loading in...5
×

Aspect-Oriented Programming

2,434

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,434
On Slideshare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
74
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Aspect-Oriented Programming

  1. 1. Aspect-Oriented Programming Andrey Bratukhin, 2011
  2. 2. Contents <ul><li>Introduction
  3. 3. Main conceptions in AOP
  4. 4. AspectJ as one of implementation
  5. 5. Pointcuts in AspectJ
  6. 6. Advice in AspectJ
  7. 7. Inter-type declaration
  8. 8. Weaving
  9. 9. Bad practicies
  10. 10. Conclusion </li></ul>
  11. 11. Introduction <ul><li>Gregor Kiczales and colleagues at Xerox PARC developed the explicit concept of AOP, and followed this with the AspectJ AOP extension to Java.
  12. 12. Aspect-oriented programming is a way of modularizing crosscutting concerns much like object-oriented programming is a way of modularizing common concerns. </li></ul>
  13. 13. Motivation void transfer ( Account fromAcc, Account toAcc, int amount, User user, Logger logger ) throws Exception { logger.info ( &quot;transferring money...&quot; ); if (! checkUserPermission ( user )){ logger.info ( &quot;User has no permission.&quot; ); throw new UnauthorizedUserException (); } if ( fromAcc.getBalance () < amount ) { logger.info ( &quot;Insufficient Funds, sorry&quot; ); throw new InsufficientFundsException (); } fromAcc.withdraw ( amount ); toAcc.deposit ( amount ); //get database connection //save transactions logger.info ( &quot;Successful transaction.&quot; ); }
  14. 14. Tangled code
  15. 15. Dispersed code Module #1 Module #1 Module #1 Security API calls Security module
  16. 16. Extract common concerns Module #1 Module #1 Module #1 Automated integration of calls Security module Security aspect
  17. 17. Main concepts of AOP <ul><li>JoinPoint are points in a running program where additional behavior can be usefully joined.
  18. 18. Pointcut is a way to specify (or quantify) join points.
  19. 19. Advice is the code to run at a join point.
  20. 20. Inter-type decoration provide a way to express crosscutting concerns affecting the structure of modules.
  21. 21. Weaving is the process of applying aspects to code. </li></ul>
  22. 22. AspectJ example public aspect SomeAspect { pointcut allMethodCalls () : call (* *.*(..)); before () : allMethodCalls () { Logger.log ( &quot;Before &quot; + thisJoinPoint .toLongString ()); } after () : allMethodCalls () { Logger.log ( &quot;After &quot; + thisJoinPoint .toLongString ()); } }
  23. 23. Join points <ul><li>Method or constructor execution
  24. 24. Method or constructor call
  25. 25. Read/write field
  26. 26. Handling of exceptions
  27. 27. Initialization of class and objects </li></ul>
  28. 28. Pointcuts Signature Example *, .., + call(public *.new(int)), call(public Account.new(..)), execution(public Account+.new(..)) &&, ||, ! call(* int Account.*(..)) || call(* double Account.*(..)) && !within(Account) call(Signature), execution(Signature) call(public *.new(int)), execution(public Account+.new(..)) get(Signature), set(Signature) get(* System.out), set(private Account.amount) handler(TypePattern) Handler(* Account) within(TypePattern), withincode(Signature) within(* Account), withincode(private Account.transfer(..)) staticinitialization(TypePattern) staticinitialization(* Account)
  29. 29. Advice <ul><li>before(Formals) – runs before each join point.
  30. 30. after(Formals) returning – runs after each join point that returns normally.
  31. 31. after(Formals) throwing – runs after each join point that throws a Throwable.
  32. 32. after(Formals) – runs after each join point regardless of whether it returns normally or throws a Throwable.
  33. 33. Type arround(Formals) – runs in place of each join point. </li></ul>
  34. 34. Inter-type declaration declare parents : Account implements Serializable ; declare @type : Account : @MyAnnotation ; private String Account.name ; private void Account.getName ( String name ) { this .name = name ; } private String Account.getName () { return name ; } declare warning : get (* System.out ) && ! within ( Logger ) : &quot;Use Logger.log()&quot; ;
  35. 35. Weaving <ul><li>Source-to-source weaving
  36. 36. Post-compile weaving
  37. 37. Load-time weaving
  38. 38. Dynamic Proxy </li></ul>
  39. 39. Bad practicies <ul><li>Aspect for single class or method
  40. 40. Business logic strongly depends on aspects
  41. 41. Duplicating of functionality of runtime environment (EJB, OSGi and etc.)
  42. 42. Aspect hides valuable functionality of application
  43. 43. No unit or integration tests </li></ul>
  44. 44. Alternatives <ul><li>Frameworks
  45. 45. Runtime environments
  46. 46. Codegeneration
  47. 47. Design patterns: </li><ul><li>Observer
  48. 48. Chain of responsibility
  49. 49. Decorator
  50. 50. Proxy
  51. 51. etc. </li></ul></ul>
  1. A particular slide catching your eye?

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

×