Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

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

❖ In AspectJ, pointcut expressions can be combined with the operators && (and), || (or), !
(not).
e.g.: Match all methods with names ending with Manager and DAO

Use '||' sign to combine both expressions.


bean(*Manager) || bean(*DAO)

Steps:
1. The package figures we have already installed in our Eclipse IDE for previous experiment,
and here also we are going to use Point.java, ShapeFigureElement.java and Line.java class
for our experiment.
2. Create ClassTest03.java class file and AspectJ03.aj aspect file for demonstrating the
experiment work.

3. Now, Start working on code to run java application with fields in AspectJ.
4. Run the Aspect/Java Application file.

5. Now, output of the java application is visible in output console.

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