Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Restricting a pointcut

Hello!

In the example shown below, I'd like to restrict a pointcut to affect
only the superclass, not the subclasses that extend it. Is there a
generic way to do this? Do I need to especify all subclasses that my
pointcut can't affect?


 public class Figure {
    public String printPosition() { return ""; };
 }

 public class Point extends Figure {
      private int x, y;
      public Point(int x, int y) { this.x = x; this.y = y; }
      public void setX(int x) { this.x = x; }
      public void setY(int y) { this.y = y; }
      public int getX() { return x; }
      public int getY() { return y; }

      public String printPosition() {
         return "Point at (" + this.x + "," + this.y + ")";
      }
 }

 public aspect TestingPointcut {
   pointcut p1(): call(public * Figure.printPosition());
   before(): p1() {
     System.out.println("TestingPointcut: " + thisJoinPoint);
   }
 }


 When I run the following main class,

 public class Teste {
 public static void main(String[] args) {
         Point p1 = new Point(3,3);
         Point p2 = new Point(-1,-1);
         System.out.println(p1.printPosition());
         System.out.println(p2.printPosition());
   }
 }

 I get this result:
 > TestingPointcut: call(String javaFonts.figure.Point.printPosition())
 > Point at (3,3)
 > TestingPointcut: call(String javaFonts.figure.Point.printPosition())
 > Point at (-1,-1)

 As you can observe, I'm not using the wildcard + in the pointcut
 definition, but the subtypes of Figure are also being affected.
 According to the AspectJ Programming Guide, hosted in eclipse.org, if I
 intend to affect exactly a type, I should define a pointcut like this:

 call(Foo.new());

 If I want to affect also the subtypes of Foo, I should define a pointcut
 like this:

 call(Foo+.new());

 Does it work only for constructors?


 Thanks,
 Fabiano Cutigi Ferrari.


Back to the top