63
面面面面面面面 Aspect Oriented Programming 22/10/26 Institute of Computer Software Nanjing University 1

面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Embed Size (px)

Citation preview

Page 1: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

面向方面的编程

Aspect Oriented Programming

23/4/21Institute of Computer Software

Nanjing University

1

Page 2: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

摘要 Background and Motivation AOP AspectJ Summary

23/4/21Institute of Computer Software

Nanjing University

2

Page 3: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

摘要 Background and Motivation AOP AspectJ Summary

23/4/21Institute of Computer Software

Nanjing University

3

Page 4: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

4

Page 5: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Modularity in Reality

Code from org.apache.tomcat

23/4/21Institute of Computer Software

Nanjing University

5

XML

Parsing

Red shows relevant lines of code

Nicely fits in one box

Good Modularity

Page 6: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Modularity in Reality

23/4/21Institute of Computer Software

Nanjing University

6

URL pattern matching

Pretty Good Modularity

fits in two boxes

Page 7: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Modularity in Reality

23/4/21Institute of Computer Software

Nanjing University

7

Logging

Red shows lines of code that handle logging

Not in just one place Not even in a small number of places

Bad Modularity

Code tangling and scattering

Page 8: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

History of Programming Paradigms

In the beginning: Programming in whichever way. Monolithic programs

23/4/21Institute of Computer Software

Nanjing University

8

Page 9: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

History of Programming Paradigms

Structured Programming Functional decomposition

23/4/21Institute of Computer Software

Nanjing University

9

Modularity

Page 10: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

History of Programming Paradigms

Object-Oriented Programming Encapsulation & Inheritance

23/4/21Institute of Computer Software

Nanjing University

10

Modularity

Page 11: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Nice things of OOP

Modular structure Reuse

Class Design patterns Framework

23/4/21Institute of Computer Software

Nanjing University

11

Page 12: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Limitations of OOP

23/4/21Institute of Computer Software

Nanjing University

12

Some things cannot be modeled well in object hierarchies!!

Page 13: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

A motivating example: Tracing

23/4/21Institute of Computer Software

Nanjing University

13

class Point{ void set (int x, int y){ TraceSupport.traceEntry(“Point.set”); this.x =x; this.y=y; TraceSupport.traceExit(“Point.set”); }}

Page 14: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Java API Example

23/4/21Institute of Computer Software

Nanjing University

14

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

Page 15: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Crossing cutting concerns

23/4/21Institute of Computer Software

Nanjing University

15

Page 16: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

What is a concern?

A particular goal, concept, or area of interest (requirement)

A software system contains: Business (application) logic concerns System-level concerns

23/4/21Institute of Computer Software

Nanjing University

16

Page 17: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Crosscutting Concerns

Crosscutting is how to characterize a concern than spans multiple units of OO modularity

Crosscutting concerns resist modularization using normal OO construct

23/4/21Institute of Computer Software

Nnjing University

17

Page 18: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

18

Page 19: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Approaches to SoC

Composition Filters Multi-dimensional Separation of Concern

s Adaptive Programming Aspect-Oriented Programming

Was developed at Xerox PARC (施乐公司 帕洛阿尔托研究中心)

(Related) Meta-Object Protocol (MOP) Reflective programming, meta-object

protocol23/4/21

Institute of Computer SoftwareNanjing University

19

Page 20: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

摘要 Background and Motivation AOP AspectJ Summary

23/4/21Institute of Computer Software

Nanjing University

20

Page 21: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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”

23/4/21Institute of Computer Software

Nanjing University

21

Page 22: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Two central problems AOP tries to solve

Code tangling One module,

many concerns

23/4/21Institute of Computer Software

Nanjing University

22

Code scattering One concern,

many modules

Example:

logging

Page 23: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Two central problems AOP tries to solve

23/4/21Institute of Computer Software

Nanjing University

23

Page 24: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

AOP approach to SoC

23/4/21Institute of Computer Software

Nanjing University

24

Page 25: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Prism Analogy

23/4/21Institute of Computer Software

Nanjing University

25

Crosscutting concerns

Implement each concern separately

Weave concerns together

Page 26: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Basic Mechanisms of AOP

23/4/21Institute of Computer Software

Nanjing University

26

Aspect

Pointcut

Advice

Weaving

Page 27: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Summary so far

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 AspectJ is the leading implementation of

AOP that extends Java’s OOP model

23/4/21Institute of Computer Software

Nanjing University

27

Page 28: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

摘要 Background and Motivation AOP AspectJ Summary

23/4/21Institute of Computer Software

Nanjing University

28

Page 29: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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/defa

ult.html Launched in 1998

Transferred to Eclipse.org in 2002 http://www.eclipse.org/aspectj/

Latest version: AspectJ 1.6.1023/4/21

29

Page 30: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

30

Page 31: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

AspectJ Compilation Process

23/4/21Institute of Computer Software

Nanjing University

31

weavingApplication

System

Applicationmodules

.java or jar files

Aspects.aj files

ajc: javac extension

Page 32: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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)

23/4/21Institute of Computer Software

Nanjing University

32

Page 33: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Concepts in AspectJ

Join Points 连接点 Pointcut 切入点 Advice 通知 Aspect 方面 Introduce 引入

23/4/21Institute of Computer Software

Nanjing University

33

Page 34: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

High level View of AspectJ

23/4/21Institute of Computer Software

Nanjing University

34

Java Program

AspectJ

Advice

pointcut advice body

join point

Page 35: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Join Points Model

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

Method call:

23/4/21Institute of Computer Software

Nanjing University

35

Public void move(int dx, int dy){

setX(_x+dx);

setY(_y+dy);

}

Method call join point

Page 36: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

More Join Points

public void setX(int x){ _x = x;}

23/4/21Institute of Computer Software

Nanjing University

36

Method execution join point

Field set join point

Page 37: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

All Join Points

method & constructor execution method & constructor call field get & set exception handler execution static & dynamic initialization dynamic joinpoints (cflow, cflowbelow)

23/4/21Institute of Computer Software

Nanjing University

37

Page 38: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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*());

23/4/21Institute of Computer Software

Nanjing University

38

Page 39: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

39

pointcut move():

execution (void Point.setX(int)) ||

execution (void Point.setY(int));

Name and parameters

Executions of methods with the specified sig.

or

Page 40: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

More on Pointcuts

Basic pointcuts and pointcuts composition

Pointcuts can be composed as boolean expressions with “&&”, “||” and “!”

Pointcuts can have arguments

23/4/21Institute of Computer Software

Nanjing University

40

Page 41: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

41

Page 42: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Advice

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

23/4/21Institute of Computer Software

Nanjing University

42

after() returning(): move() {

//code here runs after each move

}

Type of advice Pointcut it applies to

Page 43: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

43

Page 44: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Aspects

Aspect HistoryUpdating{

pointcut move():

execution(voidPoint.setX(int)) ||

execution(voidPoint.setY(int));

after() returning: move() {

//code here runs after each move

}

}

23/4/21Institute of Computer Software

Nanjing University

44

Example:

Page 45: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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 ….

23/4/21Institute of Computer Software

Nanjing University

45

Page 46: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

How it works

Short answer: bytecode modification

23/4/21Institute of Computer Software

Nanjing University

46

Page 47: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

A first example

23/4/21Institute of Computer Software

Nanjing University

47

Page 48: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

What it does

23/4/21Institute of Computer Software

Nanjing University

48

Sample Code

Page 49: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

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

23/4/21Institute of Computer Software

Nanjing University

49

Page 50: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Introduction examples

public int foo.bar(int x); private int foo.counter; declare parents: mammal extends

animal; declare parents: MyThread implements

MyThreadInterface;

23/4/21Institute of Computer Software

Nanjing University

50

Page 51: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Some Advanced Topics

Aspect Extension (Inheritance) example

23/4/21Institute of Computer Software

Nanjing University

51

Page 52: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Some Advanced Topics

Aspect Creation (Association) Aspect instance

Single (static) vs. Multiple (dynamic) Default: Singleton, created when program

begins Object (Class) association:

perthis, pertarget Controlflow association

Percflow, percflowbelow Aspect & Advice Precedence & Interactions

23/4/21Institute of Computer Software

Nanjing University

52

Page 53: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Performance impact

See “Advice Weaving in AspectJ” “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

23/4/21Institute of Computer Software

Nanjing University

53

Page 54: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

AspectJ Terminology

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

23/4/21Institute of Computer Software

Nanjing University

54

Page 55: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Dynamic AOP Systems

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.)

23/4/21Institute of Computer Software

Nanjing University

55

Page 56: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Other Java AOP sightings

AspectWerkz: load-time/run-time weaving, AOP constructs defined using javadoctags

BEA: AOP framework for Weblogic JBoss AOP framework

AOP with interceptors Spring framework

Limited AOP with proxies J2EE without EJB

AspectJ2EE JasCo …

23/4/21Institute of Computer Software

Nanjing University

56

Page 57: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Other Aspect-Oriented Languages

AspectC AspectC++ Aspect.Net Lightweight Python AOP AspectL(Lisp) AspectML AspectPHP

23/4/21Institute of Computer Software

Nanjing University

57

Page 58: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Middleware & System Level Applications

Security Transaction Persistence Mobility Realtime& Embedded Systems OS ……

23/4/21Institute of Computer Software

Nanjing University

58

Page 59: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

AOSD

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.

23/4/21Institute of Computer Software

Nanjing University

59

Page 60: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

AOSD

23/4/21Institute of Computer Software

Nanjing University

60

Page 61: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

A fast development area

AOP Aspect-Oriented Programming

AOSD Aspect-Oriented Software Design

(Development) AORE (AOR)

Aspect-Oriented Requirements Engineering AOM

Aspect Oriented Modeling

23/4/21Institute of Computer Software

Nanjing University

61

Page 62: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

Where to Get More Information

The AspectJ website http://eclipse.org/aspectj

AOSD website http://www.aosd.net

Books Mastering AspectJ AspectJ in Action

Google

23/4/21Institute of Computer Software

Nanjing University

62

Page 63: 面向方面的编程 Aspect Oriented Programming 2015-10-4 Institute of Computer Software Nanjing University 1

作业(本次作业不用提交) 运行 demo 程序,研究 AspectJ 的语法、编译

过程 注意:先安装 Eclipse AJDT 插件

思考为什么说 AOP 不会取代 OOP ,而是对OOP 的补充?

23/4/21Institute of Computer Software

Nanjing University

63