面向对象技术 - 南京大学计算机科学与技术系

Download Report

Transcript 面向对象技术 - 南京大学计算机科学与技术系

1
Aspect Oriented Programming
面向方面的编程
Institute of Computer Software
Nanjing University
2015/7/18
摘要
2




Background and Motivation
AOP
AspectJ
Summary
Institute of Computer Software
Nanjing University
2015/7/18
摘要
3




Background and Motivation
AOP
AspectJ
Summary
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
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
2015/7/18
Modularity in Reality
6
fits in two boxes
URL
pattern
matching
Pretty Good
Modularity
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
History of Programming Paradigms
8

In the beginning: Programming in whichever way.
 Monolithic
programs
Institute of Computer Software
Nanjing University
2015/7/18
History of Programming Paradigms
9

Structured Programming
 Functional
decomposition
Modularity
Institute of Computer Software
Nanjing University
2015/7/18
History of Programming Paradigms
10

Object-Oriented Programming
 Encapsulation
& Inheritance
Modularity
Institute of Computer Software
Nanjing University
2015/7/18
Nice things of OOP
11


Modular structure
Reuse
 Class
 Design
patterns
 Framework
Institute of Computer Software
Nanjing University
2015/7/18
Limitations of OOP
12
Some things cannot be modeled well in object hierarchies!!
Institute of Computer Software
Nanjing University
2015/7/18
A motivating example: Tracing
13
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
2015/7/18
Java API Example
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);
Same code repeated
}
16 times in java.io.File
return exits0();
}
public boolean canRead(){
SecurityManager security = System.getSecurityManager();
if(security!=null){
security.checkRead(path);
}
return canRead0();
}
Institute of Computer Software
Nanjing University
2015/7/18
Crossing cutting concerns
15
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
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
2015/7/18
Separation of Concerns (SoC)
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
2015/7/18
Approaches to SoC
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
2015/7/18
摘要
20




Background and Motivation
AOP
AspectJ
Summary
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
Two central problems AOP tries to solve
22

Code tangling

 One
module, many
concerns
Code scattering

One concern,
many modules
Example:
logging
Institute of Computer Software
Nanjing University
2015/7/18
Two central problems AOP tries to solve
23
Institute of Computer Software
Nanjing University
2015/7/18
AOP approach to SoC
24
Institute of Computer Software
Nanjing University
2015/7/18
Prism Analogy
25
Implement each
concern separately
Crosscutting concerns
Weave concerns
together
Institute of Computer Software
Nanjing University
2015/7/18
Basic Mechanisms of AOP
26
Aspect
Advice
Pointcut
Weaving
Institute of Computer Software
Nanjing University
2015/7/18
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

AspectJ is the leading implementation of AOP that
extends Java’s OOP model
Institute of Computer Software
Nanjing University
2015/7/18
摘要
28




Background and Motivation
AOP
AspectJ
Summary
Institute of Computer Software
Nanjing University
2015/7/18
What is AspectJ?
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: AspectJ 1.6.10
2015/7/18
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
2015/7/18
AspectJ Compilation Process
31
Application .java or jar files
modules
weaving
Application
System
ajc: javac extension
Aspects
.aj files
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
Concepts in AspectJ
33





Join Points 连接点
Pointcut 切入点
Advice 通知
Aspect 方面
Introduce 引入
Institute of Computer Software
Nanjing University
2015/7/18
High level View of AspectJ
34
AspectJ
Advice
advice body
pointcut
join point
Java Program
Institute of Computer Software
Nanjing University
2015/7/18
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){
setX(_x+dx);
setY(_y+dy);
}
Method call
join point
Institute of Computer Software
Nanjing University
2015/7/18
More Join Points
36
public void setX(int x){
_x = x;
}
Method execution
join point
Field set join point
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
Pointcuts
38

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
2015/7/18
Named Pointcuts
39


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)) ||
or
execution (void Point.setY(int));
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
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
Type of advice
Pointcut it applies to
after() returning(): move() {
//code here runs after each move
}
Institute of Computer Software
Nanjing University
2015/7/18
Advice Types in AspectJ
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
2015/7/18
Aspects
44

Example:
Aspect HistoryUpdating{
pointcut move():
execution(voidPoint.setX(int)) ||
execution(voidPoint.setY(int));
after() returning: move() {
//code here runs after each move
}
}
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
How it works
46

Short answer: bytecode modification
Institute of Computer Software
Nanjing University
2015/7/18
A first example
47
Institute of Computer Software
Nanjing University
2015/7/18
What it does
48
Sample Code
Institute of Computer Software
Nanjing University
2015/7/18
AspectJ’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
2015/7/18
Introduction examples
50




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
2015/7/18
Some Advanced Topics
51

Aspect Extension (Inheritance)
 example
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
Performance impact
53

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
Institute of Computer Software
Nanjing University
2015/7/18
AspectJ 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
2015/7/18
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
2015/7/18
Other Java AOP sightings
56



AspectWerkz: 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
J2EE without EJB
AspectJ2EE
JasCo
…
Institute of Computer Software
Nanjing University
2015/7/18
Other Aspect-Oriented Languages
57







AspectC
AspectC++
Aspect.Net
Lightweight Python AOP
AspectL(Lisp)
AspectML
AspectPHP
Institute of Computer Software
Nanjing University
2015/7/18
Middleware & System Level Applications
58







Security
Transaction
Persistence
Mobility
Realtime& Embedded Systems
OS
……
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
AOSD
60
Institute of Computer Software
Nanjing University
2015/7/18
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
2015/7/18
Where to Get More Information
62

The AspectJ website
 http://eclipse.org/aspectj

AOSD website
 http://www.aosd.net

Books
 Mastering
AspectJ
 AspectJ in Action

Google 
Institute of Computer Software
Nanjing University
2015/7/18
作业(本次作业不用提交)
63

运行demo程序,研究AspectJ的语法、编译过程
 注意:先安装Eclipse

AJDT插件
思考为什么说AOP不会取代OOP,而是对OOP
的补充?
Institute of Computer Software
Nanjing University
2015/7/18