You are on page 1of 23

Programming Languages

and Paradigms
Aspect Oriented Programming
Introduction
 Currently, the dominant programming paradigm is object-
oriented programming that:
 has been presented as a technology that can
fundamentally aid software engineering
 is reflected in the entire spectrum of current software
development methodologies and tools

 Object orientation is a good technique but has certain


limitations
Problems with OOP
 OOP has difficulty handling the idea of
cross-cutting concerns
 some functionalities(concerns) are meant to
be applied to several locations in your code
 This usually results in code being duplicated
 E.g. System.out.println() logging
 Makes some tasks like disabling the feature
difficult since the code is in several locations
Terminology
 Concern – is a particular goal, concept, or area of interest. An
engineering process deals with many concerns. High level vs. low
level, localized vs. system wide.

 Crosscutting concerns – concerns that in conventional


implementations cannot be implemented without scattering code.

 Code tangling – in conventional environments, implementing


crosscutting concerns is scattered in many modules.

 Code scattering
 Duplicated code blocks

 Complementary code blocks


Cross-Cutting Concerns Visualized:
Logging in Tomcat

(From AspectJ tutorial)


Tracing class TraceSupport {
static int TRACELEVEL = 0;
static protected PrintStream stream = null;
static protected int callDepth = -1;
TraceSupport
static void init(PrintStream _s) {stream=_s;}

static void traceEntry(String str) {


if (TRACELEVEL == 0) return;
callDepth++;
printEntering(str);
}
static void traceExit(String str) {
if (TRACELEVEL == 0) return;
callDepth--;
printExiting(str);
}
class Point { }
void set(int x, int y) {
TraceSupport.traceEntry(“Point.set”);
_x = x; _y = y; Consistent trace
TraceSupport.traceExit(“Point.set”);
}
form but using it
} is cross-cutting...
Other Cross-Cutting Concerns
 Systemic
 security (authorization and auditing)

 logging and debugging

 synchronization and transactions

 persistence and many more

 Functional
 business rules and constraints

 traversal of complex object graphs

 accounting mechanisms (timing and billing)


The Cost of Tangled Code
 Redundant code
 same or similar fragment of code in many places

 Difficult to reason about


 non-explicit structure

 the big picture of the tangling isn’t clear

 Difficult to change
 have to find all the code involved

 and be sure to change it consistently

 and be sure not to break it by accident


AOP: Opening a New Dimension in
Software Development

Object
decomposition
is flatland

A cross-cutting concern is scattered


because it is realized in the wrong
dimension!

concerns
AOP: Opening a New Dimension in
Software Development

Object
decomposition
is flatland

Aspects are
aspect
orthogonal to the
primary decomposition
concerns
Expected Benefits of AOP
 Good modularity, even for crosscutting
concerns
 less tangled code
 more natural code
 shorter code
 easier maintenance and evolution
 easier to reason about, debug, change
 more reusable
 library aspects
 plug and play aspects when appropriate
AOP Basic Concepts
 The following are some of the fundamental
ideas in AOP
 Aspect
 Advice
 Joinpoint
 Pointcut
 Target
 Weaving
Aspect
 Aspect - Think of this as the general
feature you want to apply globally to your
application
 Logging
 performance monitoring
 exception handling
 DB session/transaction management
 E.g. Hibernate transaction handling via Spring
Advice
 Advice - A chunk of code that is invoked
during program execution, and is a piece
of the logic for implementing your aspect

 Different advice types exist, e.g. advise


 before joinpoint
 after joinpoint
 around joinpoint
 several others
Joinpoint
 Joinpoint - A *single* location in the code
where an advice should be executed
 A Joinpoint can represent things such as
 access to a specific field
 invocations to a specific method
 constructor invocation
 several others
Pointcut
 Pointcut - A pointcut is a set of one or
more joinpoints where a given advice
should be applied to/executed.
 Some implementations implement this via
some kind of Regular Expression mechanism to
define class and method signature patterns
Target
 Targets/Target Objects - The objects you
want to apply an aspect or set of aspects
to
 Most implementations allow a means to
apply an aspect to many target object at
once
Weaving
 Weaving is the ability of the AOP
implementation to insert the aspect code
into the specified locations in the system
 Weaving can be done at
 the compilation level using a special compiler
(either by altering the source code or
generated byte code)
 can be done at runtime via special class
loaders
Weaving
 There are several types of AOP weaving
 Source code based – source code is altered
with the new functionality during the
compilation phase
 Byte code enhancement – byte code is altered
by the AOP compiler to add new functionality
 JIT-weaving - when the .class is loaded the
data is directly altered by the Java classloader
to add the new functionality
General flow of AOP
1. Separate the actual code that will be applied to different
locations (e.g. logging, session management)
 Advice

2. Define “where” the Advice will be applied


 Pointcut – which is a set of Joinpoints

3. Let the AOP implementation add your aspect code to the


supplied locations
 Weaving
AOP in Java
 Over the years, several AOP implementations have been
made

 Java Language Extension(new compiler, new keywords,


etc.)
 AspectJ – most powerful/complete AOP

implementation for Java


 Requires a new compiler to handle new AOP-related

keywords
AOP in Java
 Add-on libraries
 JBossAOP, SpringAOP, AspectWerkz

 Does not alter the Java language (implements AOP

features via new classes or classloaders)


 Differing levels of AOP functionality support

 For our examples, we will be looking at Spring AOP


References
 http://www.onjava.com/pub/a/onjava/20
04/07/14/springaop.html
 http://www.javalobby.org/java/forums/t4
4746.html
 http://www.onjava.com/pub/a/onjava/20
04/01/14/aop.html
 spring-reference.pdf

You might also like