1 / 63

Aspect Oriented Programming

Aspect Oriented Programming. 面向方面的编程. 摘要. Background and Motivation AOP AspectJ Summary. 摘要. Background and Motivation AOP AspectJ Summary. Background and Motivation. Where OOP has brought us Reusability of components Modularity Less complex implementation

xenos
Download Presentation

Aspect Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aspect Oriented Programming 面向方面的编程 Institute of Computer Software Nanjing University

  2. 摘要 • Background and Motivation • AOP • AspectJ • Summary Institute of Computer Software Nanjing University

  3. 摘要 • Background and Motivation • AOP • AspectJ • Summary Institute of Computer Software Nanjing University

  4. Background and Motivation • Where OOP has brought us • Reusability of components • Modularity • Less complex implementation • Reduced cost of maintenance • …… • Modularity is a universal advancement over structured programming that leads to clearer and better understood software Institute of Computer Software Nanjing University

  5. Modularity in Reality • Code from org.apache.tomcat Red shows relevant lines of code XML Parsing Good Modularity Nicely fits in one box Institute of Computer Software Nanjing University

  6. Modularity in Reality fits in two boxes URL pattern matching Pretty Good Modularity Institute of Computer Software Nanjing University

  7. Modularity in Reality Red shows lines of code that handle logging Code tangling and scattering Logging Bad Modularity Not even in a small number of places Not in just one place Institute of Computer Software Nanjing University

  8. History of Programming Paradigms • In the beginning: Programming in whichever way. • Monolithic programs Institute of Computer Software Nanjing University

  9. History of Programming Paradigms • Structured Programming • Functional decomposition Modularity Institute of Computer Software Nanjing University

  10. History of Programming Paradigms • Object-Oriented Programming • Encapsulation & Inheritance Modularity Institute of Computer Software Nanjing University

  11. Nice things of OOP • Modular structure • Reuse • Class • Design patterns • Framework Institute of Computer Software Nanjing University

  12. Limitations of OOP Some things cannot be modeled well in object hierarchies!! Institute of Computer Software Nanjing University

  13. A motivating example: Tracing class Point{ void set (int x, int y){ TraceSupport.traceEntry(“Point.set”); this.x =x; this.y=y; TraceSupport.traceExit(“Point.set”); } } Institute of Computer Software Nanjing University

  14. Java API Example package java.io; public class File implements java.io.Serializable{ private String path; … public boolean exists(){ SecurityManager security = System.getSecurityManager(); if(security!=null){ security.checkRead(path); } return exits0(); } public boolean canRead(){ SecurityManager security = System.getSecurityManager(); if(security!=null){ security.checkRead(path); } return canRead0(); } Same code repeated 16 times in java.io.File Institute of Computer Software Nanjing University

  15. Crossing cutting concerns Institute of Computer Software Nanjing University

  16. What is a concern? • A particular goal, concept, or area of interest (requirement) • A software system contains: • Business (application) logic concerns • System-level concerns Institute of Computer Software Nanjing University

  17. Crosscutting Concerns • Crosscutting is how to characterize a concern than spans multiple units of OO modularity • Crosscutting concerns resist modularization using normal OO construct Institute of Computer Software Nnjing University

  18. Separation of Concerns (SoC) • Object-Oriented Programming separates and encapsulates some concerns • Others end up tangled and scattered • Basic problem • N dimensions of concerns • 1 dimension of implementation structure • Tyranny decomposition Institute of Computer Software Nanjing University

  19. Approaches to SoC • Composition Filters • Multi-dimensional Separation of Concerns • Adaptive Programming • Aspect-Oriented Programming • Was developed at Xerox PARC (施乐公司 帕洛阿尔托研究中心) • (Related) Meta-Object Protocol (MOP) • Reflective programming, meta-object protocol Institute of Computer Software Nanjing University

  20. 摘要 • Background and Motivation • AOP • AspectJ • Summary Institute of Computer Software Nanjing University

  21. The AOP Idea • Many cross-cuts aren’t random! • They serve specific purposes • They have well-defined structure • So…let’s capture the cross-cutting structure in a modular way to better support for “Separation of Concerns” Institute of Computer Software Nanjing University

  22. Two central problems AOP tries to solve • Code tangling • One module, many concerns • Code scattering • One concern, many modules Example: logging Institute of Computer Software Nanjing University

  23. Two central problems AOP tries to solve Institute of Computer Software Nanjing University

  24. AOP approach to SoC Institute of Computer Software Nanjing University

  25. Prism Analogy Implement each concern separately Weave concerns together Crosscutting concerns Institute of Computer Software Nanjing University

  26. Basic Mechanisms of AOP Aspect Advice Pointcut Weaving Institute of Computer Software Nanjing University

  27. Summary so far • AOP is a software development technique thatcomplementsand extendsOOP • Provides new and powerful ways to modularize “crosscutting concerns” • Crosscutting concerns: Behavior that cuts across class boundaries • AspectJ is the leading implementation of AOP that extends Java’s OOP model Institute of Computer Software Nanjing University

  28. 摘要 • Background and Motivation • AOP • AspectJ • Summary Institute of Computer Software Nanjing University

  29. What is AspectJ? • A simple and practical aspect-oriented extension to Java • A general purpose AO language • Based on over ten years of research at Xerox PARC • http://www.parc.com/research/projects/aspectj/default.html • Launched in 1998 • Transferred to Eclipse.org in 2002 • http://www.eclipse.org/aspectj/ • Latest version: AspectJ 1.6.12

  30. Design assumptions • Real Community Users • Writing aspects • Reading aspects • Idioms • How effective for concerns • Modular, reusable and easy to develop and maintain • Java compatibility • Upward compatibility • Platform compatibility • Tool compatibility • Programmer Compatibility Institute of Computer Software Nanjing University

  31. .java or jar files Application modules Aspects .aj files AspectJ Compilation Process Application System weaving ajc: javac extension Institute of Computer Software Nanjing University

  32. Dynamic VS Static crosscutting • Dynamic crosscutting • define additional behavior to run at certain well-defined points in the execution of the program • Static crosscutting • modify the static structure of a program (e.g., adding new methods, implementing new interfaces, modifying the class hierarchy) Institute of Computer Software Nanjing University

  33. Concepts in AspectJ • Join Points 连接点 • Pointcut 切入点 • Advice 通知 • Aspect 方面 • Introduce 引入 Institute of Computer Software Nanjing University

  34. High level View of AspectJ AspectJ Advice advice body pointcut join point Java Program Institute of Computer Software Nanjing University

  35. Join Points Model • Join point is a well-defined point in a program’s execution • Method call: public void move(int dx, int dy){ setX(_x+dx); setY(_y+dy); } Method call join point Institute of Computer Software Nanjing University

  36. More Join Points public void setX(int x){ _x = x; } Method execution join point Field set join point Institute of Computer Software Nanjing University

  37. All Join Points • method & constructor execution • method & constructor call • field get & set • exception handler execution • static & dynamic initialization • dynamic joinpoints (cflow, cflowbelow) Institute of Computer Software Nanjing University

  38. Pointcuts • Pointcut: • Predicate on join points: selecting a collection of joinpoints • Example: call (void Point.setX(int)) • Wildcard characters are allowed. The following pointcut matches any method call to the methods of the class Point whose names begin with “set” • call (void myPackage..*Point.set*()); Institute of Computer Software Nanjing University

  39. Named Pointcuts • Can be a Named set of join points • Capture all the executions of the • <void Point.setX(int)> or <void Point.setY(int)> method Name and parameters Executions of methods with the specified sig. pointcut move(): execution (void Point.setX(int)) || execution (void Point.setY(int)); or Institute of Computer Software Nanjing University

  40. More on Pointcuts • Basic pointcuts and pointcuts composition • Pointcuts can be composed as boolean expressions with “&&”, “||” and “!” • Pointcuts can have arguments Institute of Computer Software Nanjing University

  41. Pointcut Designators • call, execution (method or constructor) • get, set (field) • within (type), withincode(method or constructor) • this, target (type or id) • args(type or id list) • cflow, cflowbelow(pointcut) • handler, throwing (exception type) • combine with ||, && and ! • use *, + and .. wildcards • bind arguments (autoboxing!), this, target

  42. Advice • Code that runs before, after, or around (instead of) a join point Type of advice Pointcut it applies to after() returning(): move() { //code here runs after each move } Institute of Computer Software Nanjing University

  43. Advice Types in AspectJ • Before advice: runs at the moment join point is reached, before method runs • After advice: runs at the moment control returns through join point, just after method • After, after returning, after throwing • Around advice: runs when join point is reached and has control over whether method itself runs at all Institute of Computer Software Nanjing University

  44. Aspects Aspect HistoryUpdating{ pointcut move(): execution(voidPoint.setX(int)) || execution(voidPoint.setY(int)); after() returning: move() { //code here runs after each move } } • Example: Institute of Computer Software Nanjing University

  45. Aspects • Mix everything we’ve seen up to now and put it one or more modular units called Aspects. • Looks a lot like a class! • Can contain pointcuts, advice declarations, methods, variables …. Institute of Computer Software Nanjing University

  46. How it works • Short answer: bytecode modification Institute of Computer Software Nanjing University

  47. A first example Institute of Computer Software Nanjing University

  48. What it does Sample Code Institute of Computer Software Nanjing University

  49. AspectJ’s Introductions • An introduction is an aspect member that allows to • Add methods to an existing class • Add field to an existing class • Extend an existing class with another • Implement an interface in an existing class • Convert checked exceptions into unchecked exceptions Institute of Computer Software Nanjing University

  50. Introduction examples • public int foo.bar(int x); • private int foo.counter; • declare parents: mammal extends animal; • declare parents: MyThread implements MyThreadInterface; Institute of Computer Software Nanjing University

More Related