Your SlideShare is downloading. ×
0
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Aspect-Oriented Programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Aspect-Oriented Programming

2,410

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,410
On Slideshare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
72
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Aspect-Oriented Programming Andrey Bratukhin, 2011
  • 2. Contents <ul><li>Introduction
  • 3. Main conceptions in AOP
  • 4. AspectJ as one of implementation
  • 5. Pointcuts in AspectJ
  • 6. Advice in AspectJ
  • 7. Inter-type declaration
  • 8. Weaving
  • 9. Bad practicies
  • 10. Conclusion </li></ul>
  • 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. 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. 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. Tangled code
  • 15. Dispersed code Module #1 Module #1 Module #1 Security API calls Security module
  • 16. Extract common concerns Module #1 Module #1 Module #1 Automated integration of calls Security module Security aspect
  • 17. Main concepts of AOP <ul><li>JoinPoint are points in a running program where additional behavior can be usefully joined.
  • 18. Pointcut is a way to specify (or quantify) join points.
  • 19. Advice is the code to run at a join point.
  • 20. Inter-type decoration provide a way to express crosscutting concerns affecting the structure of modules.
  • 21. Weaving is the process of applying aspects to code. </li></ul>
  • 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. Join points <ul><li>Method or constructor execution
  • 24. Method or constructor call
  • 25. Read/write field
  • 26. Handling of exceptions
  • 27. Initialization of class and objects </li></ul>
  • 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. Advice <ul><li>before(Formals) – runs before each join point.
  • 30. after(Formals) returning – runs after each join point that returns normally.
  • 31. after(Formals) throwing – runs after each join point that throws a Throwable.
  • 32. after(Formals) – runs after each join point regardless of whether it returns normally or throws a Throwable.
  • 33. Type arround(Formals) – runs in place of each join point. </li></ul>
  • 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. Weaving <ul><li>Source-to-source weaving
  • 36. Post-compile weaving
  • 37. Load-time weaving
  • 38. Dynamic Proxy </li></ul>
  • 39. Bad practicies <ul><li>Aspect for single class or method
  • 40. Business logic strongly depends on aspects
  • 41. Duplicating of functionality of runtime environment (EJB, OSGi and etc.)
  • 42. Aspect hides valuable functionality of application
  • 43. No unit or integration tests </li></ul>
  • 44. Alternatives <ul><li>Frameworks
  • 45. Runtime environments
  • 46. Codegeneration
  • 47. Design patterns: </li><ul><li>Observer
  • 48. Chain of responsibility
  • 49. Decorator
  • 50. Proxy
  • 51. etc. </li></ul></ul>

×