Experiment No.: 4 Aim:: Concept of Aop
Experiment No.: 4 Aim:: Concept of Aop
: 4
Aim: Write any example program (Like “Hello World! Hello John”) by applying the AspectJ
concepts
a) Join Point
b) Point Cut
c) Advice
And State which part of the code is implementation of above concepts of AspectJ.
Theory:
CONCEPT OF AOP:
a) JOIN POINT:
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”
• AspectJ provides a syntax for indicating these join points “from outside” the actual code.
Thus, JoinPoint is very diverse, where you initialize an object is also considered a JoinPoint.
Join point
initialization
of object
Join point
call method
b) POINT CUT:
Pointcut definitions consist of a left-hand side and a right-hand side, separated by a colon.
• The left-hand side consists of the pointcut name and the pointcut parameters (i.e. the
data available when the events happen).
• The right-hand side consists of the pointcut itself.
Example:
pointcut callSayHello(): call(* HelloAspectJDemo.sayHello());
• The name of this pointcut is callSayHello
• The pointcut has no parameters
• The pointcut itself is call(* HelloAspectJDemo.sayHello())
• The pointcut refers to any time the HelloAspectJDemo.sayHello() method is
Called
Other
Join
point
Join point(s)
of Pointcut
call
SayHello
c) ADVICE:
Advice defines crosscutting behavior. It is defined in terms of pointcuts. The code of a piece of
advice runs at every join point picked out by its pointcut. Exactly how the code runs depend on
the kind of advice.
AspectJ supports three kinds of advice. The kind of advice determines how it interacts with
the join points it is defined over. Thus, AspectJ divides advice into that which runs before its join points,
that which runs after its join points, and that which runs in place of (or “around”) it join point.
While before advice is relatively unproblematic, there can be three interpretations of after
advice: After the execution of a join point completes normally, after it throws an exception, or
after it does either one. AspectJ allows after advice for any of these situations.
Code:
HelloAspectJDemo.java File
package com.aop.gbu;
public class HelloAspectJDemo {
public static void sayHello() {
System.out.println("Hello");
}
public static void greeting() {
String name = new String("John");
sayHello();
System.out.print(name);
}
public static void main(String[] args) {
sayHello();
System.out.println("--------");
sayHello();
System.out.println("--------");
greeting();
}
}
HelloAspectJ.aj File
package com.aop.gbu;
public aspect HelloAspectJ {
// Define a Pointcut is
// collection of JoinPoint call sayHello of class HelloAspectJDemo.
pointcut callSayHello(): call(* HelloAspectJDemo.sayHello());
before() : callSayHello() {
System.out.println("Before call sayHello");
}
after() : callSayHello() {
System.out.println("After call sayHello");
}
}
Output:
HelloAspectJ2.aj File
package com.aop.gbu;
public aspect HelloAspectJ {
pointcut callSayHello(): call(* HelloAspectJDemo.sayHello());
after() returning(Object retObj): callSayHello() {
System.out.println("returned normally with" + retObj);
}
after() throwing (Exception e): callSayHello() {
System.out.println("Threw an exception" + e);
}
after() : callSayHello() {
System.out.println("returned or threw an exception");
}
}
Output:
HelloAspectJ3.aj File
package com.aop.gbu;
public aspect HelloAspectJ {
pointcut callSayHello(): call(* HelloAspectJDemo.sayHello());
after() returning(Object retObj): callSayHello() {
System.out.println("returned normally with" + retObj);
}
after() returning(): callSayHello() {
System.out.println("returned normally ");
}
after() returning : callSayHello() {
System.out.println("Returned normally");
}
}
Output: