Aspect-Oriented Programming and Aspect-J TDDD05 Ola Leifer Most - - PowerPoint PPT Presentation

aspect oriented programming and aspect j
SMART_READER_LITE
LIVE PREVIEW

Aspect-Oriented Programming and Aspect-J TDDD05 Ola Leifer Most - - PowerPoint PPT Presentation

Aspect-Oriented Programming and Aspect-J TDDD05 Ola Leifer Most slides courtesy of Jens Gustafsson and Mikhail Chalabine Outline: Aspect-Oriented Programming New concepts introduced Crosscutting concern Aspect Dynamic aspect


slide-1
SLIDE 1

Aspect-Oriented Programming and Aspect-J

TDDD05 Ola Leifer

Most slides courtesy of Jens Gustafsson and Mikhail Chalabine

slide-2
SLIDE 2

2

Outline: Aspect-Oriented Programming

New concepts introduced

Crosscutting concern Aspect Dynamic aspect weaving Static aspect weaving Join point Dynamic join point model Static join point model

Pros and cons Case study: Aspect-J

slide-3
SLIDE 3

3

Recall: Reifcation, Refection etc.

Reifcation Refection

Introspection Introcession

Supported in standard Java AOP, Invasive Composition

slide-4
SLIDE 4

4

Object-Oriented Programming …

Objects model the real world

Data and operations combined Encapsulation Objects are self contained

Separation of concerns ?

slide-5
SLIDE 5

5

Example (1)

slide-6
SLIDE 6

6

Example (2)

slide-7
SLIDE 7

7

Example (3)

slide-8
SLIDE 8

8

What is Crosscutting

Code in objects (components, programs)

not directly related to the core functionality

User authentication Persistence Timing

Mixing of concerns leads to

Code scattering Code tangling

slide-9
SLIDE 9

9

Problems: Intermixed Concerns

Correctness

Understandability Testability

Maintenance

Find code Change it consistently No help from OO tools

Reuse

slide-10
SLIDE 10

10

Case Study: Apache Tomcat

Concern: XML Parsing

From org.apache.tomcat Source fles

slide-11
SLIDE 11

11

Case Study (2): Apache Tomcat

Concern: URL Pattern Matching

slide-12
SLIDE 12

12

Case Study (3): Apache Tomcat

Concern: Logging

slide-13
SLIDE 13

13

Aspect-Oriented Programming

Aspect = Implementation of a crosscutting concern Components and component language Aspects and aspect language Does not replace OOP Code does not have to be OO based

slide-14
SLIDE 14

14

Aspect Weaving

slide-15
SLIDE 15

15

Back to the Examples

slide-16
SLIDE 16

16

Weave on Demand

A weaving rule

(code execution pattern  execution modifcation)

slide-17
SLIDE 17

17

Weaving, Example

slide-18
SLIDE 18

18

Weaving Time

Preprocessor Compile time Link time Load time Run time

slide-19
SLIDE 19

19

New Concepts

(using Aspect-J terminology)

Weaving Aspect (= weaving rule)

Join point Pointcut Advice

slide-20
SLIDE 20

20

Join Point

Static join point model (Invasive Composition)

A location in (a component) code

where a concern crosscuts

Example: A method or class defnition

Dynamic join point model (AspectJ)

A well-defned point in the program fow Example: A call to a method

slide-21
SLIDE 21

21

Pointcut

A pointcut is a predicate that matches join points

The “pattern” part of a weaving rule Is a predicate that matches join points Picks out certain join points Exposes parameters at join points

Example

The balanceAltered

pointcut picks out each join point that

is a call to either the deposit()

  • r the withdraw()

method

  • f an Account

class

slide-22
SLIDE 22

22

Pointcut, Further Examples

call ( void SomeClass.make*(..) )

picks out each join point that's a call to a void method defned

  • n SomeClass whose name begins with "make“ regardless
  • f the method's parameters

call ( public * SomeClass.* (..) )

picks out each call to SomeClass public methods

cflow ( somePointcut )

picks out each pointcut that occurs in the dynamic context of

the join points picked out by somePointcut

pointcuts in the control fow,

e.g., in a chain of method calls

slide-23
SLIDE 23

23

Advice

The modifcation part of a weaving rule Code executed at a pointcut

join point reached joint point matched

slide-24
SLIDE 24

24

Aspect

The unit of modularity for a crosscutting concern

Implements join points, pointcuts, advice

slide-25
SLIDE 25

25

So far we have …

Agreed that

tangled, scattered code that appears as a result of mixing different crosscutting concerns in (OO) programs is a problem

Sketched a feasible solution - AOP Introduced

Join points Pointcuts Advice Aspects Weaving

Tools?

slide-26
SLIDE 26

26

AspectJ

Xerox Palo Alto Research Center Gregor Kiczales, 1997 Goal: Make AOP available to developers

Open Source Tool integration Eclipse

Java with aspect support Current focus: industry acceptance

slide-27
SLIDE 27

27

Join Points in AspectJ

Method call execution Constructor call execution Field get Field set Exception handler execution Class/object initialization

slide-28
SLIDE 28

28

Patterns as Regular Expressions

Match any type: * Match 0 or more characters: * Match 0 or more parameters: (..) All subclasses: Person+ Call: call (private void Person.set*(*) Call: call (* * *.*(*)) Call: call (* * *.*(..))

slide-29
SLIDE 29

29

Logical Operators

Match all constructor-based instantiations of subclasses of the

Person

class:

slide-30
SLIDE 30

30

Pointcut Example

Match all attempts to retrieve the balance

variable of the

Account

class:

slide-31
SLIDE 31

31

Exposing Context in Pointcuts (1)

Matching with parameters

AspectJ gives code access to some part of the context of

the join point (parts of the matched pattern)

Two ways

Methods Designators

slide-32
SLIDE 32

32

Exposing Context in Pointcuts (2)

thisJoinPoint

class and its methods

Designators

State-based: this, target, args Control Flow-based: cflow, cflowbelow Class-initialization: staticinitialization Program Text-based: withincode, within Dynamic Property-based: If, adviceexecution

slide-33
SLIDE 33

33

Exposing Context in Pointcuts (3)

Methods on thisJoinPoint

getThis() getTarget() getArgs() getSignature() getSourceLocation() getKind() toString() toShortString() toLongString()

slide-34
SLIDE 34

34

slide-35
SLIDE 35

35

Designators (1)

Execution

Matches execution of a method or constructor

Call

Matches calls to a method

Initialization

Matches execution of the frst constructor

Handler

Matches exceptions

Get

Matches the reference to a class attribute

Set

Matches the assignment to a class attribute

slide-36
SLIDE 36

36

Designators (2)

This

Returns the target object of a join point

  • r limits the scope of join point

Target

Returns the object associated with a particular join point

  • r limits the scope of a join point by using a class type

Args

Exposes the arguments to a join point

  • r limits the scope of the pointcut
slide-37
SLIDE 37

37

Designators (3)

Cfow

Returns join points in the execution fow of another join

point

Cfowbelow

Returns join points in the execution fow of another join

point but including the current join point

Staticinitialization

Matches the execution of a class's static initialization

slide-38
SLIDE 38

38

Designators (4)

Withincode

Matches within a method or a constructor

Within

Matches within a specifc type (class)

If

Allows a dynamic condition to be part of a pointcut

Adviceexecution

Matches on advice join points

Preinitialization

Matches pre-initialization join points

slide-39
SLIDE 39

39

One more Exposing Context Example

slide-40
SLIDE 40

40

Exposing Context, Comment

Prefer designators over method calls Higher cost of refection associated with get*

slide-41
SLIDE 41

41

Advice

Before After

Unqualifed After returning After throwing

Around

slide-42
SLIDE 42

42

BEFORE Advice Example

slide-43
SLIDE 43

43

AFTER Advice Example

slide-44
SLIDE 44

44

AFTER RETURNING Advice Example

slide-45
SLIDE 45

45

AFTER THROWING Advice Example

slide-46
SLIDE 46

46

AROUND Advice Example

slide-47
SLIDE 47

47

Inter-Type Declarations

slide-48
SLIDE 48

48

Inter-Type Declarations

Add members

methods constructors felds

Add concrete implementations to interfaces Declare that types extend new types Declare that types implement new interfaces

slide-49
SLIDE 49

49

Other AOP Languages

PostSharp (C#) Pytilities (Python) Lisp (Lisp) Ruby (Ruby)

slide-50
SLIDE 50

50

Possible Applications

Resource pooling connections Caching Authentication Design by contract Wait cursor for slow operations Inversion of control Runtime evolution Consistent exception management

(Byte) code size reduction 