0% found this document useful (0 votes)
110 views20 pages

Manu Goel Anshul Jain Ankit Gupta Honey Kumar: 1 Tuesday, August 1, 2017

The document discusses aspect-oriented programming (AOP), which addresses the problem of crosscutting concerns in code. AOP allows programmers to modularize crosscutting concerns into distinct modules called aspects. Aspects define pointcuts that specify where advice or additional code should be inserted in a modular way. Common examples of crosscutting concerns that can be addressed with AOP include logging, profiling, and security checks. The document provides an example of using an aspect in Java code to add additional behavior at method call join points.

Uploaded by

Manu Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views20 pages

Manu Goel Anshul Jain Ankit Gupta Honey Kumar: 1 Tuesday, August 1, 2017

The document discusses aspect-oriented programming (AOP), which addresses the problem of crosscutting concerns in code. AOP allows programmers to modularize crosscutting concerns into distinct modules called aspects. Aspects define pointcuts that specify where advice or additional code should be inserted in a modular way. Common examples of crosscutting concerns that can be addressed with AOP include logging, profiling, and security checks. The document provides an example of using an aspect in Java code to add additional behavior at method call join points.

Uploaded by

Manu Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

MANU GOEL

ANSHUL JAIN
ANKIT GUPTA
HONEY KUMAR

Tuesday, August 1, 2017 1


The Problem
Some programming tasks cannot be neatly encapsulated in
objects, but must be scattered throughout the code
Examples:
Logging (tracking program behavior to a file)
Profiling (determining where a program spends its time)
Tracing (determining what methods are called when)
Session tracking, session expiration
Special security management
The result is crosscuting code--the necessary code cuts
across many different classes and methods

2
Aspect Oriented Programming
AOP is about modularizing crosscutting concerns

well modularized concern aspect

badly modularized
without AOP with AOP

examples: tracing, synchronization, security, buffering,


error handling, constraint checks, ...
3
Programming Paradigms
Procedural programming
Executing a set of commands in a given sequence
Fortran, C, Cobol
Functional programming
Evaluating a function defined in terms of other functions
Lisp, ML, OCaml
Logic programming
Proving a theorem by finding values for the free variables
Prolog
Object-oriented programming (OOP)
Organizing a set of objects, each with its own set of responsibilities
Smalltalk, Java, C++ (to some extent)
Aspect-oriented programming (AOP)
Executing code whenever a program shows certain behaviors
AspectJ (a Java extension), AspectC++ (a C++ extension) etc
Does not replace O-O programming, but rather complements it

4
Terminology
Cross-cutting concerns are parts of a program which rely
on or must affect many other parts of the system
A join point is a well-defined point in the program flow
A pointcut is a group of join points
Advice is code that is executed at a pointcut
Introduction modifies the members of a class and the
relationships between classes
An aspect is a module for handling crosscutting concerns
Aspects are defined in terms of pointcuts, advice, and
introduction
Aspects are reusable and inheritable

5
Example
void main()
{int a,b,c;
float d; Now imagine similar code in
cin>>a; every method
cin>>b;
cin>>c;
try { if((a-b)!=0) { d=c/(a-b);
cout<<"Result is:"<<d; }
else { throw(a-b); }
}
catch(int i) { cout<<"Answer is infinite because a-b is:"<<i; }

6
Consequences of Crosscutting Code
Redundant code
Same fragment of code in many places
Difficult to reason about
Non-explicit structure
The big picture of the tangling isnt 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

7
Aspects
In AOP crosscutting concerns are implemented in
aspects instead of fusing them into core modules.
Aspects are an additional unit of modularity.
Aspects can be reused.
By reducing code tangling it makes it easier to
understand what the core functionality of a
module is.
An aspect weaver takes the aspects and the core
modules and composes the final system.
8
Aspects in Some Common Problems
APPLICATION GP LANGUAGE COMPONENTS ASPECTS

Image Processing Procedural Filters loop fusion,


result sharing,
compile-time
memory Allocation
Digital Library Object-oriented repositories, Minimizing
printers, network traffic,
services Synchronization
constraints,
failure handling
Matrix Algorithms Procedural linear algebra Matrix
operations representation,
Permutation,
floating point error
9
Join Points
A join point is a well-defined point in the program flow
We want to execute some code (advice) each time a join
point is reached
We do not want to clutter up the code with explicit indicators
saying This is a join point
AspectC++ provides a syntax for indicating these join points
from outside the actual code
A join point is a point in the program flow where
something happens
Examples:
When a method is called
When an exception is thrown
When a variable is accessed

10
Pointcuts
pointcut is a set of join points
allows a programmer to describe where and when
additional code should be executed in addition to an
already defined behaviour
This permits the addition of aspects to existing
software, or the design of software with a
clear separation of concerns, wherein the
programmer weaves (merges) different aspects into a
complete application

11
Advice
class of functions which modify other functions when
the latter are run
certain function, method or procedure that is to be
applied at a given join point of a program
term advice goes back to the term advising as
introduced by Warren Teitelman in his PhD thesis in
1966
"Advising" found its way into BBN Lisp and later
into Xerox PARC's Interlisp

12
Introduction*
An introduction is a member of an aspect, but it
defines or modifies a member of another type (class).
With introduction we can
add methods to an existing class
add fields to an existing class
extend an existing class with another
implement an interface in an existing class
convert checked exceptions into unchecked exceptions

*This concepts comes mainly from AspectJ 13


Implementations
The following programming languages have implemented AOP, within the
language, or as an external library:
.NET Framework languages Java
(C# / VB.NET) AspectJ
ActionScript JavaScript
Ada Logtalk
AutoHotkey Lua
C / C++ make
COBOL ML
The Cocoa Objective-C frameworks PHP
ColdFusion Racket
Common Lisp Perl
Delphi Prolog
Delphi Prism Python
e (IEEE 1647) Ruby
Emacs Lisp Squeak Smalltalk
Groovy UML 2.0
Haskell XML
* List taken from en.wikipedia.org 14
AspectJTM
AspectJ is the first full AOP language
AspectJ is a small, well-integrated extension to Java
Every valid Java program is a valid AspectJ program.
Based on the 1997 PhD thesis by Christina Lopes, A Language
Framework for Distributed Programming
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 JBuilder, Fort, Eclipse, probably others
Best online write-up: http://www.eclipse.org/aspectj/
Parts of this lecture were taken from the above paper

15
AspectJ Example
public class Example {

public static void deliverMessage(String message) {


System.out.println("The message is: " + message);
}

public static void main(String [] args) { Output:


deliverMessage("I'm here");
deliverMessage("AspectJ rocks"); Hello! The message is: I'm here
} The message has been delivered.
} Hello! The message is: AspectJ rocks
The message has been delivered.
public aspect ExampleAspect {

pointcut helloPC() : call(void Example.deliverMessage(..));

before() : helloPC() {
System.out.print("Hello! ");
}

after() : helloPC() {
System.out.println("The message has been delivered.");
}
}

16
Open Issues
AOP is a young idea
The current status of AOP is much like that of
OOP twenty years ago
As AOP is still in research, quantitative assessment
of AOP not done till date
Very little support available
..and many more may come out after all rules
are totally established.

17
Conclusion
Aspect-oriented programming (AOP) is a new
paradigm--a new way to think about programming
not the perfect solution to the issues involved in
programming, but is a good starting point
With experience, you should be able to fine tune the
concepts of AOP to better fit your own work environment
AOP is somewhat similar to event handling, where the
events are defined outside the code itself
Like all new technologies, AOP may--or may not--
catch on in a big way
18
References
http://www.codeproject.com/Articles/4039/Aspect-
Oriented-Programming-Aspect-Oriented-Softwa
http://en.wikipedia.org/wiki/Aspect-
oriented_programming
http://www.aspectc.org/
cseweb.ucsd.edu/~wgg/CSE218/aop-ecoop97.pdf
and many more

19
Any Questions

20

You might also like