1 Aspect Oriented Programming Institute of Computer Software

  • Slides: 63
Download presentation
1 Aspect Oriented Programming 面向方面的编程 Institute of Computer Software Nanjing University 2021/12/11

1 Aspect Oriented Programming 面向方面的编程 Institute of Computer Software Nanjing University 2021/12/11

摘要 2 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing

摘要 2 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing University 2021/12/11

摘要 3 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing

摘要 3 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing University 2021/12/11

Background and Motivation 4 Where OOP has brought us Reusability of components Modularity Less

Background and Motivation 4 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 2021/12/11

Modularity in Reality 5 Code from org. apache. tomcat Red shows relevant lines of

Modularity in Reality 5 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 2021/12/11

Modularity in Reality 6 fits in two boxes URL pattern matching Pretty Good Modularity

Modularity in Reality 6 fits in two boxes URL pattern matching Pretty Good Modularity Institute of Computer Software Nanjing University 2021/12/11

Modularity in Reality 7 Code tangling and scattering Red shows lines of code that

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

History of Programming Paradigms 8 In the beginning: Programming in whichever way. Monolithic programs

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

History of Programming Paradigms 9 Structured Programming Functional decomposition Modularity Institute of Computer Software

History of Programming Paradigms 9 Structured Programming Functional decomposition Modularity Institute of Computer Software Nanjing University 2021/12/11

History of Programming Paradigms 10 Object-Oriented Programming Encapsulation & Inheritance Modularity Institute of Computer

History of Programming Paradigms 10 Object-Oriented Programming Encapsulation & Inheritance Modularity Institute of Computer Software Nanjing University 2021/12/11

Nice things of OOP 11 Modular structure Reuse Class Design patterns Framework Institute of

Nice things of OOP 11 Modular structure Reuse Class Design patterns Framework Institute of Computer Software Nanjing University 2021/12/11

Limitations of OOP 12 Some things cannot be modeled well in object hierarchies!! Institute

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

A motivating example: Tracing 13 class Point{ void set (int x, int y){ Trace.

A motivating example: Tracing 13 class Point{ void set (int x, int y){ Trace. Support. trace. Entry(“Point. set”); this. x =x; this. y=y; Trace. Support. trace. Exit(“Point. set”); } } Institute of Computer Software Nanjing University 2021/12/11

Java API Example 14 package java. io; public class File implements java. io. Serializable{

Java API Example 14 package java. io; public class File implements java. io. Serializable{ private String path; … public boolean exists(){ Security. Manager security = System. get. Security. Manager(); if(security!=null){ security. check. Read(path); Same code repeated } 16 times in java. io. File return exits 0(); } public boolean can. Read(){ Security. Manager security = System. get. Security. Manager(); if(security!=null){ security. check. Read(path); } return can. Read 0(); } Institute of Computer Software Nanjing University 2021/12/11

Crossing cutting concerns 15 Institute of Computer Software Nanjing University 2021/12/11

Crossing cutting concerns 15 Institute of Computer Software Nanjing University 2021/12/11

What is a concern? 16 A particular goal, concept, or area of interest (requirement)

What is a concern? 16 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 2021/12/11

Crosscutting Concerns 17 Crosscutting is how to characterize a concern than spans multiple units

Crosscutting Concerns 17 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 2021/12/11

Separation of Concerns (So. C) 18 Object-Oriented Programming separates and encapsulates some concerns Others

Separation of Concerns (So. C) 18 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 2021/12/11

Approaches to So. C 19 Composition Filters Multi-dimensional Separation of Concerns Adaptive Programming Aspect-Oriented

Approaches to So. C 19 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 2021/12/11

摘要 20 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing

摘要 20 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing University 2021/12/11

The AOP Idea 21 Many cross-cuts aren’t random! They serve specific purposes They have

The AOP Idea 21 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 2021/12/11

Two central problems AOP tries to solve 22 Code tangling l One module, many

Two central problems AOP tries to solve 22 Code tangling l One module, many concerns Code scattering l One concern, many modules Example: logging Institute of Computer Software Nanjing University 2021/12/11

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

Two central problems AOP tries to solve 23 Institute of Computer Software Nanjing University 2021/12/11

AOP approach to So. C 24 Institute of Computer Software Nanjing University 2021/12/11

AOP approach to So. C 24 Institute of Computer Software Nanjing University 2021/12/11

Prism Analogy 25 Implement each concern separately Crosscutting concerns Weave concerns together Institute of

Prism Analogy 25 Implement each concern separately Crosscutting concerns Weave concerns together Institute of Computer Software Nanjing University 2021/12/11

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

Basic Mechanisms of AOP 26 Aspect Advice Pointcut Weaving Institute of Computer Software Nanjing University 2021/12/11

Summary so far 27 AOP is a software development technique that complements and extends

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

摘要 28 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing

摘要 28 Background and Motivation AOP Aspect. J Summary Institute of Computer Software Nanjing University 2021/12/11

What is Aspect. J? 29 A simple and practical aspect-oriented extension to Java A

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

Design assumptions 30 Real Community Users Writing aspects Reading aspects Idioms How effective for

Design assumptions 30 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 2021/12/11

Aspect. J Compilation Process 31 Application. java or jar files modules weaving Application System

Aspect. J Compilation Process 31 Application. java or jar files modules weaving Application System ajc: javac extension Aspects . aj files Institute of Computer Software Nanjing University 2021/12/11

Dynamic VS Static crosscutting 32 Dynamic crosscutting define additional behavior to run at certain

Dynamic VS Static crosscutting 32 Dynamic crosscutting define additional behavior to run at certain welldefined 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 2021/12/11

Concepts in Aspect. J 33 Join Points 连接点 Pointcut 切入点 Advice 通知 Aspect 方面

Concepts in Aspect. J 33 Join Points 连接点 Pointcut 切入点 Advice 通知 Aspect 方面 Introduce 引入 Institute of Computer Software Nanjing University 2021/12/11

High level View of Aspect. J 34 Aspect. J Advice advice body pointcut join

High level View of Aspect. J 34 Aspect. J Advice advice body pointcut join point Java Program Institute of Computer Software Nanjing University 2021/12/11

Join Points Model 35 Join point is a well-defined point in a program’s execution

Join Points Model 35 Join point is a well-defined point in a program’s execution Method call: Public void move(int dx, int dy){ set. X(_x+dx); set. Y(_y+dy); } Method call join point Institute of Computer Software Nanjing University 2021/12/11

More Join Points 36 public void set. X(int x){ _x = x; } Method

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

All Join Points 37 method & constructor execution method & constructor call field get

All Join Points 37 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 2021/12/11

Pointcuts 38 Pointcut: Predicate on join points: selecting a collection of joinpoints Example: call

Pointcuts 38 Pointcut: Predicate on join points: selecting a collection of joinpoints Example: call (void Point. set. X(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 my. Package. . *Point. set*()); Institute of Computer Software Nanjing University 2021/12/11

Named Pointcuts 39 Can be a Named set of join points Capture all the

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

More on Pointcuts 40 Basic pointcuts and pointcuts composition Pointcuts can be composed as

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

Pointcut Designators 41 call, execution (method or constructor) get, set (field) within (type), withincode(method

Pointcut Designators 41 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

Advice 42 Code that runs before, after, or around (instead of) a join point

Advice 42 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 2021/12/11

Advice Types in Aspect. J 43 Before advice: runs at the moment join point

Advice Types in Aspect. J 43 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 2021/12/11

Aspects 44 l Example: Aspect History. Updating{ pointcut move(): execution(void. Point. set. X(int)) ||

Aspects 44 l Example: Aspect History. Updating{ pointcut move(): execution(void. Point. set. X(int)) || execution(void. Point. set. Y(int)); after() returning: move() { //code here runs after each move } } Institute of Computer Software Nanjing University 2021/12/11

Aspects 45 Mix everything we’ve seen up to now and put it one or

Aspects 45 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 2021/12/11

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

How it works 46 Short answer: bytecode modification Institute of Computer Software Nanjing University 2021/12/11

A first example 47 Institute of Computer Software Nanjing University 2021/12/11

A first example 47 Institute of Computer Software Nanjing University 2021/12/11

What it does 48 Sample Code Institute of Computer Software Nanjing University 2021/12/11

What it does 48 Sample Code Institute of Computer Software Nanjing University 2021/12/11

Aspect. J’s Introductions 49 An introduction is an aspect member that allows to Add

Aspect. J’s Introductions 49 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 2021/12/11

Introduction examples 50 public int foo. bar(int x); private int foo. counter; declare parents:

Introduction examples 50 public int foo. bar(int x); private int foo. counter; declare parents: mammal extends animal; declare parents: My. Thread implements My. Thread. Interface; Institute of Computer Software Nanjing University 2021/12/11

Some Advanced Topics 51 Aspect Extension (Inheritance) example Institute of Computer Software Nanjing University

Some Advanced Topics 51 Aspect Extension (Inheritance) example Institute of Computer Software Nanjing University 2021/12/11

Some Advanced Topics 52 Aspect Creation (Association) Aspect instance Single (static) vs. Multiple (dynamic)

Some Advanced Topics 52 Aspect Creation (Association) Aspect instance Single (static) vs. Multiple (dynamic) Default: Singleton, created when program begins Object (Class) association: perthis, pertarget Controlflow Percflow, association percflowbelow Aspect & Advice Precedence & Interactions Institute of Computer Software Nanjing University 2021/12/11

Performance impact 53 See “Advice Weaving in Aspect. J” “The implementation of advice weaving

Performance impact 53 See “Advice Weaving in Aspect. J” “The implementation of advice weaving introduces very little performance overhead when compared to the same functionality coded by hand. ” Does make it easy to create performance destroying code… But allows powerful optimizations Institute of Computer Software Nanjing University 2021/12/11

Aspect. J Terminology 54 a join point is a well-defined point in the program

Aspect. J Terminology 54 a join point is a well-defined point in the program flow a pointcut is a group of join points advice is code that is executed at a pointcut introduction modifies the members of a class and the relationships between classes a compile time declaration introduces a compile time warning or error upon detection of certain usage patterns an aspect is a module for handling crosscutting concerns Aspects are defined in terms of pointcuts, advice, and introduction Aspects are reusable and inheritable Institute of Computer Software Nanjing University 2021/12/11

Dynamic AOP Systems 55 Creating aspects at run-time Specifying pointcuts at run-time Dynamic code

Dynamic AOP Systems 55 Creating aspects at run-time Specifying pointcuts at run-time Dynamic code translation (or some special feature of the run-time environment) Enabling/disabling aspects at run-time Pointcuts are dynamic: it can be decided only at run-time whether advices have to be executed or not (based on run-time values, call -stack, etc. ) Institute of Computer Software Nanjing University 2021/12/11

Other Java AOP sightings 56 Aspect. Werkz: load-time/run-time weaving, AOP constructs defined using javadoctags

Other Java AOP sightings 56 Aspect. Werkz: load-time/run-time weaving, AOP constructs defined using javadoctags BEA: AOP framework for Weblogic JBoss AOP framework Spring framework AOP with interceptors Limited AOP with proxies J 2 EE without EJB Aspect. J 2 EE Jas. Co … Institute of Computer Software Nanjing University 2021/12/11

Other Aspect-Oriented Languages 57 Aspect. C++ Aspect. Net Lightweight Python AOP Aspect. L(Lisp) Aspect.

Other Aspect-Oriented Languages 57 Aspect. C++ Aspect. Net Lightweight Python AOP Aspect. L(Lisp) Aspect. ML Aspect. PHP Institute of Computer Software Nanjing University 2021/12/11

Middleware & System Level Applications 58 Security Transaction Persistence Mobility Realtime& Embedded Systems OS

Middleware & System Level Applications 58 Security Transaction Persistence Mobility Realtime& Embedded Systems OS …… Institute of Computer Software Nanjing University 2021/12/11

AOSD 59 AOSD is a promising approach to encapsulate and modularize crosscutting concerns AOSD

AOSD 59 AOSD is a promising approach to encapsulate and modularize crosscutting concerns AOSD is not a substitute of the OO paradigm but an extension to it. Institute of Computer Software Nanjing University 2021/12/11

AOSD 60 Institute of Computer Software Nanjing University 2021/12/11

AOSD 60 Institute of Computer Software Nanjing University 2021/12/11

A fast development area 61 AOP Aspect-Oriented AOSD Aspect-Oriented Software Design (Development) AORE (AOR)

A fast development area 61 AOP Aspect-Oriented AOSD Aspect-Oriented Software Design (Development) AORE (AOR) Aspect-Oriented Programming Requirements Engineering AOM Aspect Oriented Modeling Institute of Computer Software Nanjing University 2021/12/11

Where to Get More Information 62 The Aspect. J website http: //eclipse. org/aspectj AOSD

Where to Get More Information 62 The Aspect. J website http: //eclipse. org/aspectj AOSD website http: //www. aosd. net Books Mastering Aspect. J in Action Google Institute of Computer Software Nanjing University 2021/12/11

作业(本次作业不用提交) 63 运行demo程序,研究Aspect. J的语法、编译过 程 注意:先安装Eclipse AJDT插件 思考为什么说AOP不会取代OOP,而是对OOP 的补充? Institute of Computer Software Nanjing

作业(本次作业不用提交) 63 运行demo程序,研究Aspect. J的语法、编译过 程 注意:先安装Eclipse AJDT插件 思考为什么说AOP不会取代OOP,而是对OOP 的补充? Institute of Computer Software Nanjing University 2021/12/11