An Overview of AspectJ - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

An Overview of AspectJ

Description:

pointcuts advice member declarations. Implementation -- aspect weaving: ... Order of advice in aspect. Sub-aspects have higher precedence (more specific) ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 29
Provided by: nany3
Category:

less

Transcript and Presenter's Notes

Title: An Overview of AspectJ


1
An Overview of AspectJ
  • Arthored by Gregor Kiczales, Erik Hilsdale, Jim
    Hugunin, Mik Kersten, Jeffrey Palm and William G.
    Griswold
  • Presented By Nanyan Jiang

2
This talk is about
  • What is Aspect Oriented Programming?
  • Crosscutting Concerns as Aspect
  • AspectJ to enable Aspect Oriented Programming as
    a seamless extension to Java
  • Design assumptions
  • New Constructs to Model Crosscutting Concerns
  • Example
  • Advantages and Limitations
  • Conclusions

3
What is AOP Module vs. Crosscutting Concerns
  • Good Modular Design
  • Separation of (Functional) Concerns, such as
  • Procedure programming in C functions
  • Object-oriented programming in Java classes
  • Other
  • What about Crosscutting Concerns?
  • Error handling, synchronization, logging,
    security, debugging, etc.
  • Conceptual module scattered into many modules
    of an applications

4
Problems
logging is not modularized
Crosscutting concerns
  • logging in org.apache.tomcat
  • red shows lines of code that handle logging
  • not in just one place
  • not even in a small number of places

Adapted from Mik Kersten, Palo Alto Research
Center Aspect.org
5
Consequences of crosscutting code
  • Repeated code
  • Same fragment of code in many places
  • Difficult to change
  • Have to find all places
  • Consistent change
  • Difficult to reason
  • No explicit structure
  • Lose the big picture due to the tangling

6
Aspect Oriented Programming
  • Characteristics explicitly capturing
    crosscutting structure as aspect
  • Implementation using programmable weaving
    process at build or runtime

7
Design Constraints of AspectJ
  • Upward compatibility
  • Java ? AspectJ
  • Platform compatibility
  • Legal AspectJ programs run on standard JVM
  • Tool compatibility
  • Integrate into IDEs, doc tools, design tools, etc
  • Programmer compatibility
  • AspectJ should feel like Java

8
AspectJ is
  • A general-purpose Aspect Oriented Language
  • An integrated and compatible extension to Java
  • Enabling two kinds of crosscutting
  • Dynamic focus of the talk
  • Static

9
Dynamic Static Crosscutting
  • Dynamic
  • Aspects run at certain points of execution
  • Inject your own code to add, modify behavior
  • Static
  • Define new operations on existing types
  • Changes static type signature of program
  • Will not be discussing this today

10
Key Dynamic Constructs and Concepts
At classes
  • Join point
  • Abstract, well-defined point in execution
  • Pointcut
  • Collection of join points values at points
  • Advice
  • Extra behavior at join points method-like
  • Aspect
  • pointcuts advice member declarations
  • Implementation -- aspect weaving
  • Coordination of objects and aspects

With aspects
Classes aspects
11
The first program Hello! and Hello!
A Class
An Aspect
  • public class Hello
  • void greeting()
  • System.out.println(Hello!)
  • public static void main(String args)
  • new Hello.greeting()

public aspect GreetingAspect before()
call(void Hello.greeting()) System.out.println(
)
12
The first program Hello!
advice
Pointcut
A Class
An Aspect
Join point
  • public class Hello
  • void greeting()
  • System.out.println(Hello!)
  • public static void main(String args)
  • new Hello.greeting()

public aspect GreetingAspect before()
call(void Hello.greeting()) System.out.println(
)
13
Joint Point Model in AspectJ
  • Well-defined points in the program flow
  • Kinds of joint point (JP)
  • Call JP method call or constructor call (caller)
  • Reception JP method call reception or
    constructor call reception (callee, before the
    execution)
  • Execution JP Method execution of constructor
    execution
  • Callee
  • Field get
  • Field set
  • Exception handler execution
  • Class initialization
  • Object initialization

14
Pointcut
  • A language construct
  • Picks out certain join points (and values)
    qualification
  • Composition
  • call(void Hello.greeting())
  • call(void Hello.greeting()) target(f)
  • call(Hello.(..))
  • call(.greeting())

15
Advice
  • A language construct
  • Code to be executed at certain join points
  • before, after or around
  • An Example
  • before() call(void Hello.greeting())
  • System.out.print(gt)

around
Most specific first
before
associated with join points
computation
after returning/ after throwing
Least specific first
after
16
The first program Hello!
A Class
An Aspect
  • public class Hi
  • void greeting()
  • System.out.println(Hi!)
  • public static void main(String args)
  • new Hi.greeting()

public aspect GreetingAspect before()
call(void Hello.greeting()) System.out.println(
)
public aspect GreetingAspect before()
call(void .greeting()) System.out.println()

17
The first program after weaving (simplified view)
  • Public class Hello
  • void greeting() System.out.println(Hello!)
  • public static void main(String args)
  • Hello dummy new Hello()
  • System.out.println()
  • dummy.greeting()

public aspect HelloAspect before() call(void
Hello.greeting()) System.out.print()
18
Advantages of programming with AspectJ
  • Advantage
  • The structure of concerns are made explicitly
  • Separation of crosscutting concerns
  • Reduce redundancy
  • Evolution is easier
  • Easy to understand

19
A simple figure editor

Figure
2
20
Join points
key points in dynamic call graph
Point pt1 new Point(0, 0) Point pt2 new
Point(4, 4) Line ln1 new Line(pt1,
pt2) ln1.incrXY(2, 2)
a Line
dispatch
a Point
dispatch
returning or throwing
returning or throwing
returning or throwing
21
join point terminology
a Line
dispatch
method execution join points
method call join points
22
the pointcut construct
names certain join points
each time there is a ltvoid Line.setP1(Point)gtor
ltvoid Line.setP2(Point)gt method call
pointcut move() call(void
Line.setP1(Point)) call(void
Line.setP2(Point))
23
after advice
action to take aftercomputation under join points
after advice runson the way back out
pointcut move() call(void
Line.setP1(Point)) call(void
Line.setP2(Point)) after() returning move()
ltcode here runs after each movegt
aspect MoveTracking
24
Aspect
  • A language construct
  • The unit of modularity for crosscutting concerns
  • May contain pointcuts, advice, etc.

an aspect defines a special class that can
crosscut other classes
aspect MoveTracking pointcut move()
call(void Line.setP1(Point)) call(void
Line.setP2(Point)) after() returning move()
ltcode here runs after each movegt
25
aspects crosscut classes
  • aspect modularity cuts across class modularity


Figure
2
MoveTracking
26
What if multiple advices for the same JP?
  • Aspect precedence
  • Only for concrete aspects
  • Order of advice in aspect
  • Sub-aspects have higher precedence (more
    specific)
  • All other cases undefined
  • E.g. conceptually and semantically independent
    aspects define advice that apply at the same join
    point

27
Advantages and Limitations
  • Advantage
  • The structure of concerns are made explicitly
  • Separation of crosscutting concerns
  • Reduce redundancy
  • Evolution is easier
  • Easy to understand
  • Limitations
  • No sequence, no order, poor composition
  • Aspect to aspect interactions?
  • Is it possible to have crosscutting concerns over
    aspects? Aspect of aspects higher order AOP

28
Conclusion Crosscutting in AspectJ
  • AspectJ is a small, well-integrated extension to
    Java
  • AspectJ modularizes crosscutting concerns
  • That is, code for one aspect of the program (such
    as tracing) is collected together in one place
  • The AspectJ compiler is free and open source
  • AspectJ works with Eclipse, JBuilder, Forté,
    probably others
  • Best online reference http//www.eclipse.org/aspe
    ctj/
  • Questions?
Write a Comment
User Comments (0)
About PowerShow.com