0% found this document useful (0 votes)
73 views27 pages

Experiment No.: 1 Aim: Procedure:: A) Steps To Install Eclipse: - 1. Download The Eclipse Installer

The document describes experiments conducted to learn about Aspect Oriented Programming (AOP) using AspectJ. It provides steps to install Eclipse and AspectJ, create a sample Java application that uses AOP concepts like join points and pointcuts, and demonstrates how to define pointcuts that refer to classes and methods. The coding examples showcase how to apply AOP concepts like join points, pointcuts, and advice to create applications that output "Hello World" and "Hello John".

Uploaded by

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

Experiment No.: 1 Aim: Procedure:: A) Steps To Install Eclipse: - 1. Download The Eclipse Installer

The document describes experiments conducted to learn about Aspect Oriented Programming (AOP) using AspectJ. It provides steps to install Eclipse and AspectJ, create a sample Java application that uses AOP concepts like join points and pointcuts, and demonstrates how to define pointcuts that refer to classes and methods. The coding examples showcase how to apply AOP concepts like join points, pointcuts, and advice to create applications that output "Hello World" and "Hello John".

Uploaded by

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

Experiment No.

: 1

Aim: Install Eclipse and AspectJ for Aspect Oriented Programming.

Procedure: The Steps to install Eclipse and AspectJ are given below.
A) Steps to install Eclipse: -

1. Download the Eclipse Installer


Download Eclipse Installer from http://www.eclipse.org/downloads .

2. Start the Eclipse Installer executable


For Windows users, after the Eclipse Installer executable has finished downloading

3. Select the package to install


4. Select your installation folder

5. Launch Eclipse

B) Steps to install AspectJ

1. Download the AspectJ Installer


Download AspectJ Installer from https://www.eclipse.org/ajdt/downloads/index.php .
 2. Install AJDT tool Plugin

A New Install Dialog box will appear. Add work with field and add Repository Name – AJDT for Eclipse 4.1
After that Click on checkbox of I accept the terms of the license agreements. And finish installation of AJDT
(AspectJ Development Tool) by clicking Finish Button.

Now, software will start installing.


Click on Restart Now Button. The Application will restart from fresh. Now, we can Start working on Eclipse.
Experiment No.: 2

Aim: Create a Java application that uses AOP. Run the application while modifying it and every time record
the output.

Theory:
1) Aspect Oriented Programming: Aspect Oriented Programming (AOP) compliments OOPs in the sense
that it also provides modularity. But the key unit of modularity is aspect than class.
AOP breaks the program logic into distinct parts (called concerns). It is used to increase modularity
by cross-cutting concerns.
A cross-cutting concern is a concern that can affect the whole application and should be centralized in
one location in code as possible, such as transaction management, authentication, logging, security etc.

Procedure: Steps to create Aspect project.

1. Create the AspectJ Program.

2. Define the name for your project i.e. AOP_Tutorial and click on Finish Button.
3. Create Module name i.e. AOP_Tutorial.

4. To create package, define the name of the package i.e. com.aop.gbu.


5. To create java class, define the name of the class i.e. AOPDemo. Mark a tick on checkbox named
public static void main (String[] args).
6. To create aspect, define its name i.e. Demoaspect.

7. Now both the java class as well as aspect has been created.
8. Now Start coding on your respective Software i.e. Eclipse.

9. Now, to have output click on Run >> AspectJ /Java Development. And then click Ok Button. The
output will appear on output console.

Coding:
AOPDemo.java file

package com.aop.gbu;
public class AOPDemo {
public static void main(String[] args) {
AOPDemo aOPDemo=new AOPDemo();
aOPDemo.method1(25);
aOPDemo.method1(23, "stringvalue");
aOPDemo.method2("stringvalue");
}
public void method1(int number) {
System.out.println("executing method1(int)code here...");
}
public void method1(int number,String value) {
System.out.println("executing method1(int,string)code here...");
}
public void method2(String value) {
System.out.println("executing method2(string)code here...");
}
}

Demoaspect.aj file:

a) Method 1(*)

package com.aop.gbu;

public aspect Demoaspect {


pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1*(*));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:

b) Method 2 (*, *)
package com.aop.gbu;

public aspect Demoaspect {


pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1(*,*));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:

c) Method 3*(..)
package com.aop.gbu;
2
public aspect Demoaspect {
pointcut callDemoaspectPointcut():
call (void com.aop.gbu.AOPDemo.method1*(..));
after() :callDemoaspectPointcut(){
System.out.println("Demo Aspect Point Cut after....");
}
}

Output:

Experiment No.: 3

Aim: Create a java application that makes uses of AOP to give the output as “Hello World!”.
Coding:
1) Hello.java File

package com.aop.gbu;
public class Hello {
public static void main(String[] args) {
sayHello();
}
public static void sayHello(){
System.out.println("HEllO");
}
}

World.aj File

package com.aop.gbu;
public aspect World {
pointcut greeting(): execution (* Hello.sayHello(..));
after() returning(): greeting(){
System.out.println("WORLD !");
}
}

Output:

2) Modified Hello.java File

package com.aop.gbu;
public class Hello {
public static void main(String[] args) {
sayHello();
sayHello("John");
}
public static void sayHello(){
System.out.println("HEllO");
}
public static void sayHello(String name){
System.out.println("HEllO "+name+ "! HEllO ");
}
}

World.aj File

package com.aop.gbu;

public aspect World {


pointcut greeting(): execution (* Hello.sayHello(..));

after() returning(): greeting(){


System.out.println("WORLD !");
}
}
Output:

Experiment No.: 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:
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.

A join point is a point in the program flow “where something happens”.

 When a method is called


 When an exception is thrown
 When a variable is accessed
 When instantiating an object
 When referring an object

Thus, JoinPoint is very diverse, where you initialize an object is also considered a JoinPoint.

Example: Back to the HelloWorld example we will specify JoinPoints:

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.

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.

Back to the HelloWorld example, we have two Advice:


 before ()
 after ()
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:
Experiment No.: 5

Aim: Create a Java application with AOP in which AspectJ refer to a class or a method in declaring Pointcut
with illustration of rules for class, method, *.

Theory:
In this experiment, some classes that participate in many illustrated examples of AspectJ are:
 Box
 FigureElement
 Group
 Line
 Point
 ShapeFigureElement
 SlotFulPoint
The sources of these classes are from:
https://github.com/eclipse/org.aspectj/tree/master/docs/teaching/exercises/figures

CODE:
Point.java file
package figures;
import java.awt.*;
import java.awt.geom.*;
public class Point extends ShapeFigureElement {
private int _x;
private int _y;
public Point(int x, int y) {
_x = x;
_y = y;
}
public int getX() { return _x; }
public int getY() { return _y; }
public void setX(int x) { _x = x; }
public void setY(int y) { _y = y; }
public void move(int dx, int dy) {
_x += dx;
_y += dy;
}
public String toString() {
return "Point(" + _x + ", " + _y + ")";
}
/** The height of displayed {@link Point}s. */
private final static int HEIGHT = 10;
/** The width of displayed {@link Point}s. -- same as {@link HEIGHT}. */
private final static int WIDTH = Point.HEIGHT;
public Shape getShape() {
return new Ellipse2D.Float((float)getX()-Point.WIDTH/2,
(float)getY()-Point.HEIGHT/2,
(float)Point.HEIGHT,
(float)Point.WIDTH);
}
}
ClassTest01.java file
package com.aop.gbu;
import figures.Point;
public class ClassTest01 {
public static void main(String[] args) {
Point point = new Point (10,200);
System.out.println("---- (1) ----");
point.setX(20);
System.out.println("---- (2) ----");
point.setY(100);
System.out.println("---- (3) ----");
}
}

AspectJ01.aj file
package com.aop.gbu;
public aspect AspectJ01 {
pointcut callSetX(): call(void figures.Point.setX(int)) && within (ClassTest01);
before() : callSetX() {
System.out.println("Before callPoint.setX(int)");
}
}

OUTPUT:

The asterisk (*) in AspectJ


AspectJ01.aj(1) file
package com.aop.gbu;
public aspect AspectJ01 {
pointcut callSetX(): call(void *.Point.setX(int));
// Advice
before() : callSetX() {
System.out.println("Before callPoint.setX(int)");
}
}

OUTPUT:

AspectJ01.aj(2) file
package com.aop.gbu;
public aspect AspectJ01 {
pointcut callSetX(): call(* *.Point.setX(int));
before() : callSetX() {
System.out.println("Before callPoint.setX(int)");
}
}

OUTPUT:

AspectJ01.aj(3) file
package com.aop.gbu;
public aspect AspectJ01 {
pointcut callSetX(): call(public static int sample.*Point.setX(int));
before() : callSetX() {
System.out.println("Before callPoint.setX(int)");
}
}

OUTPUT:

AspectJ01.aj(4) file
package com.aop.gbu;
public aspect AspectJ01 {
pointcut callSetX(): call(public static int sample.*Point.setX(..));
before() : callSetX() {
System.out.println("Before callPoint.setX(int)");
}
}

OUTPUT:

Experiment No.: 6

Aim: Create a java application with fields in AspectJ.


Theory:
In this experiment, some classes that participate in many illustrated examples of AspectJ are:
 Box, FigureElement, Group, Line, Point, ShapeFigureElement, SlotFulPoint.

The sources of these classes are from:


https://github.com/eclipse/org.aspectj/tree/master/docs/teaching/exercises/figures

Code:

FieldInAspectJ.aj File
package com.aop.gbu;
import java.io.PrintStream;
public aspect FieldInAspectJ {
// Field in AspectJ.
PrintStream logStream = System.err;
pointcut move() : call(* figures.Point.move(int,int)) && within(FieldInAspectJTest);
before () : move() {
logStream.println("Before Point move");
}
}

FieldInAspectJTest.java file
package com.aop.gbu;
import figures.Point;
public class FieldInAspectJTest {
public static void main(String[] args) {
Point point = new Point(10, 200);
System.err.println("---- (1) ----");
System.err.println("---- (2) ----");
point.move(10, 10);
System.err.println("---- (3) ----");
}
}

Output:

Experiment No.: 7

Aim: Write an aspect using AspectWerkz.


Compilation:

Steps:
To compile the aspect class, add the aspectwerkz-0.10.jar in the Classpath,
i.e.
javac -d target -classpath $ASPECTWERKZ_HOME/lib/aspectwerkz-2.0.RC1.jar
MyAspect.java

Code:
MyAspect.aj File
package testAOP;
import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
public class MyAspect {
    public void beforeGreeting(JoinPoint joinPoint) {
        System.out.println(“before greeting...”); }
    public void afterGreeting(JoinPoint joinPoint) {
        System.out.println(“after greeting...”); }
}

HelloWorld.java file
package testAOP; 
public class HelloWorld {
    public static void main(String args[]) {
        HelloWorld world = new HelloWorld();
        world.greet(); }
    public String greet() {
        System.out.println(“Hello World!”);}
}

Output:

Experiment No.: 8

Aim: Write an aspect using XML.


Compilation:
Steps:
To compile xml class, add external jar-
i.e.
java -cp $ASPECTWERKZ_HOME/lib/aspectwerkz-2.0.RC1.jar
org.codehaus.aspectwerkz.annotation.AnnotationC . target

using annotations means we don't have to write all aspect details in XML, we do still have to create a tiny XML
'stub' which tells the AspectWerkz runtime system which Java classes it should load and treat as aspects.
An example of this is show below:
<aspectwerkz>
    <system id="AspectWerkzExample">
        <aspect class="testAOP.MyAspectWithAnnotations "/>
    </system>
</aspectwerkz>

Code:
Aop.xml File
<aspectwerkz>
    <system id="AspectWerkzExample">
        <package name="testAOP">
            <aspect class="MyAspect">
                <pointcut name="greetMethod" expression="execution (*testAOP.HelloWorld.greet(..))"/> 
                <advice name="beforeGreeting" type="before" bind-to="greetMethod"/>
                <advice name="afterGreeting" type="after" bind-to="greetMethod"/>
            </aspect>
        </package>
    </system>
</aspectwerkz>

HelloWorld.java file
package testAOP; 
public class HelloWorld {
    public static void main(String args[]) {
        HelloWorld world = new HelloWorld();
        world.greet(); }
    public String greet() {
        System.out.println(“Hello World!”);}
}

Output:

Experiment No.: 9
Aim: Tracking method which defines an Aspect that defines a pointcut on every method execution, as well as
some advice to run when they turn up when the code is executed.

Theory:
In this experiment, some classes that participate in many illustrated examples of AspectJ are:
 Box, FigureElement, Group, Line, Point, ShapeFigureElement, SlotFulPoint.
The sources of these classes are from:
https://github.com/eclipse/org.aspectj/tree/master/docs/teaching/exercises/figures

Code:

SimpleTracing.aj File
package com.aop.gbu;
import org.aspectj.lang.Signature;
public aspect SimpleTracing {
pointcut tracedCall() : call (* *(..)) && within(SimpleTracingTest);
before() : tracedCall() {
Signature sig = thisJoinPointStaticPart.getSignature();
String line = ""
+ thisJoinPointStaticPart.getSourceLocation().getLine();
String sourceName = thisJoinPointStaticPart.getSourceLocation()
.getWithinType().getCanonicalName();
//
System.out.println("Call from " + sourceName + " line " + line + "\n to "
+ sig.getDeclaringTypeName() + "." + sig.getName() +"\n");
}
}

SimpleTracingTest.java file
package com.aop.gbu;
import figures.Point;
public class SimpleTracingTest {
private static void testMethod1() {
Point point = new Point(100, 100);
point.setX(100);
}
private static void testMethod2() {
String text = "This is text";
String s = text.substring(2);
System.out.println(s);
}
public static void main(String[] args) {
testMethod1();
testMethod2();
}
}

Output:
Experiment No.: 10
Aim: Write a java application with Aspect illustrating the operator && and ||.

Theory:
In this experiment, some classes that participate in many illustrated examples of AspectJ are:
 Box, FigureElement, Group, Line, Point, ShapeFigureElement, SlotFulPoint.

The sources of these classes are from:


https://github.com/eclipse/org.aspectj/tree/master/docs/teaching/exercises/figures

Code:
AspectJ03.aj File
package com.aop.gbu;
import figures.FigureElement;
import figures.Point;
public aspect AspectJ03 {
pointcut moveAction() : ( call(void FigureElement.move(int,int)) || call(void Point.setX(int)) ||
call(void Point.setY(int)) )
&& within (ClassTest03);
before() : moveAction() {
System.out.println("before move");
}
}

ClassTest03.java file
package com.aop.gbu;
import figures.FigureElement;
import figures.Line;
import figures.Point;
public class ClassTest03 {
public static void main(String[] args) {
Point point = new Point(10, 200);
System.out.println("---- (1) ----");
point.setY(20);
System.out.println("---- (2) ----");
FigureElement line= new Line (new Point (1,1), new Point (10,10));
line.move(10, 10);
System.out.println("---- (3) ----");
}
}
Output:

You might also like