(JAVA) (Pro JPA 2 - Mastering The Java Persistence API)
(JAVA) (Pro JPA 2 - Mastering The Java Persistence API)
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 Adding new logging functionality
class Foo {
class Foo {
public void foo () {
public void foo () {
logger.log ("Start -- Foo.foo()");
bar.doSomething ();
bar.doSomething ();
}
logger.log ("End -- Foo.foo()");
}
}
}
class Bar {
static void doSomething () {
class Bar {
baz.doSomething ();
static void doSomething () {
}
logger.log ("Start -- Bar.doSomething()");
}
baz.doSomething ();
logger.log ("End -- Bar.doSomething()");
class Baz {
}
static void doSomething () {
}
for (int i = 0; i < 100; i++) {
doSomething(i);
} class Baz {
} static void doSomething () {
} logger.log ("Start -- Baz.doSomething()");
for (int i = 0; i < 100; i++) {
doSomething(i);
}
logger.log ("End -- Baz.doSomething()");
}
Logging code is inserted at }
many places in the code . 3
Problem Scenario (2/2)
Rewriting former logging example in Aspect Oriented
Programming way. Original code will not change….
aspect Logging {
after(): log() {
Logger.log( “End --" + 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
5
Aspect Oriented Programming (1/2)
6
Aspect Oriented Programming(2/2)
7
What is AOP?
Existing
OOP Project
OOP Classes
Interfaces
Concerns Executable
software
New
Concerns Concern Aspects
functionality identifier AOP
WEAVER
11
Aspect Oriented Programming
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
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);
}
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.
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";
}
before(): log() {
Logger.log("Start --" +
thisJoinPoint.getSignature ());
}
after(): log() {
Logger.log(“End --" +
thisJoinPoint.getSignature ());
}
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
22
Example
(Simple bank account class)
package org.thewhittakers.banking;
public class Account implements Loggable {
private double balance;
private String owner;
aspect DatabaseAspect {
26
Uses Cases for AOP
27
Why bother with AOP?
Capture the crosscutting concern explicitly
Both the behavior of the concern
The specification of its applicability
Change is easier
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
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
33
34
35
First Steps (contd..)
Configuring Workbench
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
41
Conclusion
42
Related Work
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~/aspectj-
home/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-0118-
aspect_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-0301-
aspect2_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
Pointcut Description
call (public void Call to myMethod() in MyClass taking a String
MyClass.myMethod(String)) argument, returning void, & public access
Call to any method with name starting in
call (* MyClass.myMethod*(..))
"myMethod" in MyClass
Call to any MyClass' constructor with any
call (MyClass.new(..))
arguments
Call to any MyClass or its subclass's
call (MyClass+.new(..)) constructor. (Subclass indicated by use of '+'
wildcard)
call (public * All public methods in all classes in any
com.mycompany..*.*(..)) package with com.mycompany the root package
46
Defining Pointcuts
Control flow based pointcuts
Pointcut Description
All the join points in control flow of call to any
cflow (call (*
myMethod() in MyClass including call to the
MyClass.myMethod(..))
specified method itself
All the join points in control flow of call to any
cflowbelow (call (*
myMethod() in MyClass excluding call to the
MyClass.myMethod(..))
specified method itself
47
Defining Pointcuts
Pointcut Description
All the join points where this is instanceof
this (JComponent+)
JComponent
All the join points where the object on which
target (MyClass)
the method is called is of type MyClass
All the join points where the first argument is
args (String,..,int) of String type & the last argument is of int
type
All the join points where the type of argument
args (RemoteException)
or exception handler type is RemoteException
48
Defining Pointcuts
Pointcut Description
Execution of read-access to field x of
get (int MyClass.x)
type int in MyClass
49
Defining Pointcuts
before()
: execution(public * *(..)) && within(figures.*) {
Log.write(thisJoinPoint);
}
50
What is AOP?
51