Aspect - Vanderbilt University

Download Report

Transcript Aspect - Vanderbilt University

Aspect-Oriented Programming
Dimple Kaul
ACCRE
Vanderbilt University
Nashville, Tennessee
http://www.dre.vanderbilt.edu/~dkaul/
1
Talk Outline









Problem Scenario
Aspect Oriented Programming
Aspect & Object Oriented Programming
Aspect Terminology
Examples
Installation of AJDT
First Steps
Conclusion
Questions
2
Problem Scenario (1/2)
Regular OO Java
class Foo {
public void foo () {
bar.doSomething ();
}
}
class Bar {
static void doSomething () {
baz.doSomething ();
}
}
class Baz {
static void doSomething () {
for (int i = 0; i < 100; i++) {
doSomething(i);
}
}
}
Logging code is inserted at
many places in the code
Adding new logging functionality
class Foo {
public void foo () {
logger.log ("Start -- Foo.foo()");
bar.doSomething ();
logger.log ("End -- Foo.foo()");
}
}
class Bar {
static void doSomething () {
logger.log ("Start -- Bar.doSomething()");
baz.doSomething ();
logger.log ("End -- Bar.doSomething()");
}
}
class Baz {
static void doSomething () {
logger.log ("Start -- Baz.doSomething()");
for (int i = 0; i < 100; i++) {
doSomething(i);
}
logger.log ("End -- Baz.doSomething()");
}
}
3
.
Problem Scenario (2/2)
Rewriting former logging example in Aspect Oriented
Programming way. Original code will not change….
aspect Logging {
pointcut log () : execution (void *.*()) ||
execution (static void Baz.doSomething()) ;
before(): log() {
Logger.log("Start
}
after(): log() {
Logger.log( “End
}
}
--" + thisJoinPoint.getSignature ());
This procedure is executed
when methods written as
"execution(X)" are
executed.
--" + thisJoinPoint.getSignature ());
"thisJoinPoint" object has an
information about the
method called.
4
Problem

Software systems consists of several concerns
For example: In a credit card processing

Primary Concern (System core)


processing of payment
Secondary Concern (System level)

authentication, security & logging etc

Object Oriented Programming provide good modularity for
primary concerns i.e., the main business logic

Adding secondary concerns (crosscutting concerns) with
primary concern result into a system which is difficult to
understand and evolve and result into code tangling &
scattering
5
Aspect Oriented Programming (1/2)
AOP shows great promise in specialization :

Functionality can often be changed without re-factoring code

No need for knowing “ahead of time” functionality


If a system does not have logging or exception handling
functionality
These concerns can be added anytime without even touching
original code

Components can be integrated incrementally

Implementations are easier to design, understand, &
maintain
6
Aspect Oriented Programming(2/2)

Supports the reuse of existing code by applying
new features in a controlled and localized way

AOP promises higher productivity, improved
quality, & better ability to implement newer
features

AOP has been implemented in different languages
(for example, C++, Smalltalk, C#, C, and Java)

AOP builds on top of other programming
paradigms: object-oriented, procedural or
functional
7
What is AOP?

Aspect-oriented programming (AOP) methodology
facilitates modularization of crosscutting concerns

Separation of concerns

breaking down of a program into distinct parts that
overlap in functionality as little as possible
“Untangle your code into cross-cutting, loosely coupled aspect”
8
AOP & OOP
Requirement
Class
Requirement is dependent
on multiple classes
Requirement
Class
Requirement
Class
Class
(a) Object Oriented Model
Requirement
Requirement
Requirement
Aspect
Aspect
Aspect
(b) Aspect Oriented Model
Each requirement can
have separate aspect
“[...] In programs P, whenever condition C arises, perform action9 A”
AOP & OOP Terminology
Object Oriented
Aspect Oriented
Class – code unit that
encapsulates methods &
attributes.
Aspect – code unit that
encapsulates pointcuts, advice,
& attributes.
Method signatures – define
the entry points for the
execution of method bodies.
Pointcut – define the set of
entry points (triggers) in which
advice is executed.
Method bodies –
implementations of the primary
concerns.
Advice – implementations of
the cross cutting concerns.
Compiler – converts source
code into object code.
Weaver – instruments code
(source or object) with advice.
Aspect Oriented Programming languages include AspectJ,
AspectC++, & JBOSS AOP
10
Aspect-Oriented Development
Concerns
Concern
Software
requirements identifier
OOP
AOP
Classes
Interfaces
Aspects
Executable
software
WEAVER
Existing
OOP Project
OOP
Classes
Interfaces
Concerns
New
Concerns
functionality
Concern
identifier
AOP
Executable
software
Aspects
WEAVER
11
Aspect Oriented Programming

Implications of tangling & scattering on
software design





Maintainability: Nightmare for huge systems
Poor traceability: simultaneous coding of many
concerns in a module breaks linkage between the
requirement & its implementation
Lower productivity: developer is paying too much
attention to peripheral issues rather than the business
logic
Less code reuse: cut-&-paste code between modules
is the lowest form of reuse and is more error prone
Harder re-factoring: changing requirements means
touching many modules for a single concern
12
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)
13
AOP Terminology
JoinPoints & Pointcut

JoinPoints:
Well-defined points in the execution of a program:
 Method call / execution
 Constructor call / execution
 Object initialization

Pointcuts:



A set of join point, plus, optionally, some of the values in
the execution context of those join points.
Can be composed using Boolean operators || , &&
Matched at runtime
For more detail see AspectJ Quick Reference Manual
14
AOP Terminology
JoinPoints & Pointcut contd…

Example:

pointcut set() : execution( * *.set*(..) ) && this(Point)


pointcut abc() : call ( public void MyClass.myMethod(..) )


Captures call of myMethod method of MyClass class with any
number of arguments
within ( org.package.* )


Captures all executions of any method that begins with set in
an object of type Point
Captures all join point where the associated code is defined
in the package “org.package.*”
withincode ( void Figure.move() )

Capture all join points where the associated code is defined
in the method void Figure.move()
“*” is wild card
“..” is multi-part wild card
15
AOP Terminology
Advice

It executes when a pointcut matches

before(): runs just prior to the join point
before(): log() {
Logger.log("Start
}

--" + thisJoinPoint.getSignature ());
after(): runs just after the join point
after(): log() {
Logger.log(("End
}
--" + thisJoinPoint.getSignature ());
16
AOP Terminology
Advice contd…

after() returning – runs after the method
returns normally
after() returning(int x) : call(int getX()) {
System.out.println("Returning int value " + x + " for
p = " + p);
}

after() throwing – runs after the method
throws an exception abruptly
after() throwing (SQLException ex) : inDataLayer() {
logException(ex);
}
17
AOP Terminology
Advice contd…

around(): runs before &/or after, with the
operation taking place via a call to proceed().
Note, if proceed is not called, then the advised
code is skipped.
pointcut log () : execution (void *.*()) ||
execution (static void Baz.doSomething()) ;
around(): log() {
Logger.log("Start --" + thisJoinPoint.getSignature ());
proceed();
Logger.log( “ End --" + thisJoinPoint.getSignature ());
}
18
AOP Terminology
Others…

Inter-type declaration: Allows to add method, fields or
interfaces to existing classes from within aspects
aspect VisitAspect {
Point.acceptVisitor(Visitor v) {
Method added
v.visit(this);
}
}
Declared error
aspect DeclareErrorWarning {
declare error : get(java.io.PrintStream System.out) &&
within(figures..*)
: "illegal access to System.out";
}

Aspect: container holding point cuts & advice

Weaver: the tool that instruments the primary concerns
with the advice based on matched pointcuts.
19
Example showing Advice, Pointcut
and Aspect
aspect Logging {
Pointcut
pointcut log () : execution (void *.*()) ||
execution (static void Baz.doSomething()) ;
before(): log() {
Logger.log("Start --" +
thisJoinPoint.getSignature ());
}
after(): log() {
Logger.log(“End --" +
thisJoinPoint.getSignature ());
}
Aspect
Piece of Advice
20
Weaving Types

Advice is inserted at




Compile-time: source code is instrumented
before compilation. (AspectC++)
Link-time: object code (byte code) is
instrumented after compilation (AspectJ)
Load-time: specialized class loaders
instrument code (AspectWerkz)
Run-time: virtual machine instruments or
application framework intercepts loaded code
(JBossAOP, XWork).
21
AspectJ & AOP




AspectJ is an aspect-oriented extension to the
Java programming language
It was originally developed and co-founded by
Gregor Kiczales and his team at Xerox PARC
Later Xerox group’s work was integrated with
Eclipse Java IDE
Freely available implementation

Compiler, tools and plugins are Open source
22
Example
(Simple bank account class)
package org.thewhittakers.banking;
public class Account implements Loggable {
private double balance;
private String owner;
public Account(String owner, double initialBalance) {
this.setOwner(owner);
this.credit(initialBalance);
}
public void credit(double amount) {
this.balance += amount;
}
public void debit(double amount) {
this.balance -= amount;
}
public void transferTo(Account other, double amount) {
this.debit(amount);
other.credit(amount);
} // less interesting items removed.
}
23
Example
(Adding pre-condition checking)
package org.thewhittakers.banking;
public aspect AccountConstraintsAspect {
pointcut preventNegativeAmounts(Account account, double amount)
: (execution(* Account.credit(double))
|| execution(* Account.debit(double)))
&& this(account) && args(amount);
pointcut preventOverdraft(Account account, double amount)
: execution(* Account.debit(double))
&& this(account) && args(amount);
before(Account account, double amount):
preventNegativeAmounts(account, amount) {
if (amount < 0)
throw new RuntimeException("Negative amounts not permitted");
}
before(Account account, double amount): preventOverdraft(account,
amount) {
if (account.getBalance() < amount)
throw new RuntimeException("Insufficient funds");
}
}
24
Example Exception Handling
Catch any exception in all the
public methods:
aspect CatchException{
// Catch any exception in all the public
methods
void around(): execution(public * *(..)){
try{
proceed();
} catch(Exception e){
System.out.println("Printing
exception");
//Handle exception
}
}
}
Throwing of exception:
aspect ThrowException {
private boolean Point.inGroup = false;
before(Point p):
execution(void Group.add(FigureElement)) &&
args(p) {
if (p.inGroup) {
throw new IllegalStateException();
} else {
p.inGroup = true;
}
}
}
Catch SQL exceptions that need to
be logged:
after() throwing (SQLException ex) :
inDataLayer() {
logException(ex);
}
25
Example of Persistence
aspect DatabaseAspect {
pointcut transactionalMethods ():
execution (/* pattern for transactional
methods */) ;
before(): transactionalMethods () {
initialiseDatabase() ;
}
after() returning: transactionalMethods() {
commitTransaction() ;
}
after() throwing: transactionalMethods() {
rollbackTransaction() ;
}
}
26
Uses Cases for AOP
Not only for Logging & Exception Handling



Thread Safety
Multi-Object Protocol
Performance optimization








Middleware Specialization using AOP
(http://www.dre.vanderbilt.edu/~dkaul/pdf/acm_aspect.pdf)
Timing / Monitoring /Tracing
Various kinds of invasive/non-invasive instrumentation
Authentication & Authorization
Transactional management /Locking
Session Handling
Synchronization
Caching
27
Why bother with AOP?

Capture the crosscutting concern explicitly



Change is easier



Both the behavior of the concern
The specification of its applicability
Change the aspect – no grepping
Aspects can be plugged in or out easily
Many people suggest use of patterns, template
and careful programming as alternative to AOP

Research has proved that all these proposed ideas
always fail to localize the crosscutting concerns. Then
tend to have some code that remains in base structure
28
Installing AspectJ

Download AJDT plugin for eclipse from:
http://download.eclipse.org/technology/ajdt/30/update






AspectJ Development Tools (AJDT) project
provides Eclipse platform based tool support for
AOSD with AspectJ
Runtime library required for AspectJ is a very
small library of about 35K
It creates normal java class files & can be
execute on any JVM
Aspect files can have .Java or .aj extension
Weaves into class files
Produces standard Java bytecode
29
First Steps
Converting existing Java project

It enables us to use AspectJ language to
implement the applications , & AspectJ
compiler to build it

Converting regular existing Java project
to AspectJ will be like this…
30
Step 1:
Select Project from the package
explorer & Right click & choose
“Convert to AspectJ Project”
from the context menu
31
Step 2:
 Some changes in the
Package Explorer are
seen
 First, the project icon
has changed from the
Java project icon J to
AspectJ project icon AJ
 Second, a new jar file
has been added to the
project's build path,
using the Eclipse path
variable ASPECTJRT_LIB
 Creates build
configuration file which
stores information about
the build of project
32
First Steps (contd..)
Creating new AspectJ projects

To do this, you use the New AspectJ Project Wizard. You
can reach the wizard via the Eclipse workbench menus by
selecting File -> New -> Project, & then AspectJ
Project.
33
34
35
First Steps (contd..)
Configuring Workbench



First time you convert old java project to Aspectj
or create new Aspectj project we need to
configure workbench
Preference window will come up to set some
settings
You can also change these preferences at any
stage by going through eclipse workbench menus
by selecting Window -> Preference ->
AspectJ
36
Create New Aspect


To creating new
aspect for a
package
Select package in
the package
explorer & right
click to go to
context menu & do
New ->Aspect
37
Skeletal Aspect
38
Moving back to regular Java
project

Easy to revert back to your regular java
project
39


Select Project from
the package explorer
& Right click &
choose “Remove
AspectJ Nature” from
the context menu
It is a good idea not
use Aspectj related
artifacts in actual
project otherwise we
may get build errors

Good practice to keep
aspect file extension
as .aj
40
History of AOP



AOP has been following other technologies
like OOP
Worked in academia
Popping up more & more in real world
41
Conclusion

AOP is an evolutionary step





Not a replacement of OOP, but is used to enhance it
Decomposes the system into primary & crosscutting
concerns which map more directly into requirements.
Easy to understand system by reducing tangling &
scattering.
Joinpoints, pointcuts, & advice are used to instrument
primary concerns with crosscutting concerns
If you’ve got an orthogonal concern that is about exactly
one place in the original code, & you’re sure that that
orthogonal concern will not propagate to other loci as
the system evolves, it is probably a bad idea to use AOP
42
Related Work
Traditional techniques:
 Code re-factoring
 Ahead-of-time design
Modern techniques:
But have shortcomings:
• Manual & Error prone
• High Memory consumption
• Performance overhead
 Feature-Oriented Programming
 Incremental stepwise refinement
 Feature modularity features are basic design
components
 Hybrid Programming (FeatureC++)
 Mix of AOP & FOP
 Still research is going on
43
Questions??
44
References
AspectJ.org. (2004). AspectJ Sample Code. Retrieved May 11, 2004, from
the AspectJ documentation:
http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectjhome/sample-code.html
C2.com. (2004). You Arent Gonna Need It. Retrieved May 11, 2004, from the
Extreme Programming Wiki: http://xp.c2.com/YouArentGonnaNeedIt.html.
Gradecki, J., & Lesiecki, N. (2003). Mastering AspectJ: Aspect-Oriented
Programming in Java. Indianapolis, IN: Wiley Publishing.
Laddad, R. (2002). I want my AOP! Part 1. Retrieved May 11, 2004, from
JavaWorld: http://www.javaworld.com/javaworld/jw-01-2002/jw-0118aspect_p.html
Laddad, R. (2002). I want my AOP! Part 2. Retrieved May 11, 2004, from
JavaWorld: http://www.javaworld.com/javaworld/jw-03-2002/jw-0301aspect2_p.html
Laddad, R. (2003). AspectJ in Action: Practical Aspect-Oriented Programming.
Greenwich, CT: Manning Publications.
45
Defining Pointcuts


“*” is wild card
“..” is multi-part wild card
Calling methods & constructors
Advice is inserted after argument evaluation, but before
calling. Access to caller’s context
Pointcut
Description
call (public void
MyClass.myMethod(String))
Call to myMethod() in MyClass taking a String
argument, returning void, & public access
call (* MyClass.myMethod*(..))
Call to any method with name starting in
"myMethod" in MyClass
call (MyClass.new(..))
Call to any MyClass' constructor with any
arguments
call (MyClass+.new(..))
Call to any MyClass or its subclass's
constructor. (Subclass indicated by use of '+'
wildcard)
call (public *
com.mycompany..*.*(..))
All public methods in all classes in any
package with com.mycompany the root package
46
Defining Pointcuts

Control flow based pointcuts
Pointcut
Description
cflow (call (*
MyClass.myMethod(..))
All the join points in control flow of call to any
myMethod() in MyClass including call to the
specified method itself
cflowbelow (call (*
MyClass.myMethod(..))
All the join points in control flow of call to any
myMethod() in MyClass excluding call to the
specified method itself
47
Defining Pointcuts

Context capturing pointcuts

Can attach names to the types to capture the variables for use
inside the associated advice.
Pointcut
Description
this (JComponent+)
All the join points where this is instanceof
JComponent
target (MyClass)
All the join points where the object on which
the method is called is of type MyClass
args (String,..,int)
All the join points where the first argument is
of String type & the last argument is of int
type
args (RemoteException)
All the join points where the type of argument
or exception handler type is RemoteException
48
Defining Pointcuts

Execution of methods & constructors


Advice is inserted in the method or constructor body itself.
Access to callee’s context. Replace call with execution.
Field access – read or write
Pointcut
Description
get (int MyClass.x)
Execution of read-access to field x of
type int in MyClass
set (int MyClass.x)
Execution of write-access to field x of
type int in MyClass
49
Defining Pointcuts

Pointcuts & logical operators

Can be combined with &&, ||, & !
before()
: execution(public * *(..)) && within(figures.*) {
Log.write(thisJoinPoint);
}
50
What is AOP?

The AOP-style solution
 Three



phase implementation (Laddad, 2003)
Aspectual decomposition: based on requirements,
extract concerns, identifying them as primary &
crosscutting.
Concern implementation: code each concern
separately – primary (OO), crosscutting (AO).
Aspectual re-composition: tools weave the separately
implemented code together into a final instrumented
software system.
51