AspectJ

Contributed by Chris Rathman

Shape Interface (Shape.java)

interface Shape {
   public void moveTo(int newx, int newy);
   public void rMoveTo(int deltax, int deltay);
   public void draw();
}

Rectangle Class (Rectangle.java)

class Rectangle implements Shape {
   private int width;
   private int height;

   // constructor
   public Rectangle(int newwidth, int newheight) {
      setWidth(newwidth);
      setHeight(newheight);
   }

   // accessors for the width & height
   public int getWidth() { return width; }
   public int getHeight() { return height; }
   public void setWidth(int newwidth) { width = newwidth; }
   public void setHeight(int newheight) { height = newheight; }

   // draw the rectangle
   public void draw() {
      System.out.println("Drawing a Rectangle: Width " +
         getWidth() + ", Height " + getHeight());
   }
}

Circle Class (Circle.java)

class Circle implements Shape {
   private int radius;

   // constructor
   public Circle(int newradius) {
      setRadius(newradius);
   }

   // accessors for the radius
   public int getRadius() { return radius; }
   public void setRadius(int newradius) { radius = newradius; }

   // draw the circle
   public void draw() {
      System.out.println("Drawing a Circle: Radius " + getRadius());
   }
}

PositionableShape Aspect (PositionableShape.java)

aspect PositionableShape {
   // properties for shape interface
   private int Shape.x = 0;
   private int Shape.y = 0;

   // circle constructor with position
   public Circle.new(int newx, int newy, int newradius) {
      this(newradius);
      moveTo(newx, newy);
   }

   // rectangle constructor with position
   public Rectangle.new(int newx, int newy, int newwidth, int newheight) {
      this(newwidth, newheight);
      moveTo(newx, newy);
   }

   // accessors for x & y
   public int Shape.getX() { return x; }
   public int Shape.getY() { return y; }
   public void Shape.setX(int newx) { x = newx; }
   public void Shape.setY(int newy) { y = newy; }

   // move the x & y position
   public void Shape.moveTo(int newx, int newy) {
      setX(newx);
      setY(newy);
   }
   public void Shape.rMoveTo(int deltax, int deltay) {
      moveTo(getX() + deltax, getY() + deltay);
   }

   // position the shape for drawing
   pointcut draw(Shape self): target(self) && call(void Shape.draw());

   before(Shape self): draw(self) {
      System.out.print("At (" + self.getX() + ", " + self.getY() + ") ");
   }

}

Try Shapes (Polymorph.java)

class Polymorph {
   public static void main(String argv[]) {

      // create some shape instances
      Shape scribble[] = new Shape[2];
      scribble[0] = new Rectangle(10, 20, 5, 6);
      scribble[1] = new Circle(15, 25, 8);

      // iterate through the list and handle shapes polymorphically
      for (int i = 0; i < scribble.length; i++) {
         scribble[i].draw();
         scribble[i].rMoveTo(100, 100);
         scribble[i].draw();
      }

      // call a rectangle specific function
      Rectangle arect = new Rectangle(0, 0, 15, 15);
      arect.setWidth(30);
      arect.draw();
   }
}

Build File (sources.lst)

Shape.java
Rectangle.java
Circle.java
PositionableShape.java
Polymorph.java

Output

At (10, 20) Drawing a Rectangle: Width 5, Height 6
At (110, 120) Drawing a Rectangle: Width 5, Height 6
At (15, 25) Drawing a Circle: Radius 8
At (115, 125) Drawing a Circle: Radius 8
At (0, 0) Drawing a Rectangle: Width 30, Height 15

Chris Rathman / Chris.Rathman@tx.rr.com