Title: An Overview of AspectJ
1An Overview of AspectJ
- Arthored by Gregor Kiczales, Erik Hilsdale, Jim
Hugunin, Mik Kersten, Jeffrey Palm and William G.
Griswold - Presented By Nanyan Jiang
2This 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
3What 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
4Problems
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
5Consequences 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
6Aspect Oriented Programming
- Characteristics explicitly capturing
crosscutting structure as aspect - Implementation using programmable weaving
process at build or runtime
7Design 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
8AspectJ 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
9Dynamic 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
10Key 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
11The 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(
)
12The 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(
)
13Joint 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
14Pointcut
- 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())
15Advice
- 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
16The 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()
17The 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()
18Advantages of programming with AspectJ
- Advantage
- The structure of concerns are made explicitly
- Separation of crosscutting concerns
- Reduce redundancy
- Evolution is easier
- Easy to understand
19A simple figure editor
Figure
2
20Join 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
21join point terminology
a Line
dispatch
method execution join points
method call join points
22the 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))
23after 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
24Aspect
- 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
25aspects crosscut classes
- aspect modularity cuts across class modularity
Figure
2
MoveTracking
26What 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
27Advantages 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
28Conclusion 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?