Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] execution pointcut and subclassing

Hi -

This is a good question because it shows exactly what's being matched.

Further down in the programming guide is a discussion of matching 
method execution join points:

   When matching method-execution join points, if the execution 
   pointcut method signature specifies a declaring type, the 
   pointcut will only match methods declared in that type, or 
   methods that override methods declared in or inherited by 
   that type

So,...

> Notice that, when square.moveBy is called, even moveBy method is not 
> defined in Rectangle class, the pointcut execution(* 
> Rectangle.moveBy(..)) still matches it, 

Shape.moveBy(int,int) is a method inherited by Rectangle.  The join 
point signature Square.moveBy(int,int) overrides that, so the 
pointcut matches.

> however, when rect.moveBy is called, 
> "exec:Rectangle.moveBy" doesn't show up

The method-execution join point signature is not Rectangle.moveBy(int,int) 
but Shape.moveBy(int, int).  That does not "override methods
declared in or inherited by" Rectangle - Rectangle inherits that.

This is designed to capture ordinary overriding semantics:
Square is a Rectangle and a Shape, but Shape is not a Rectangle.

To pick out calls to Shape.moveBy(int, int) accessed via Rectangle,
use call(void Rectangle.moveBy(int, int)) or 
call(void Shape.moveBy(int,int)).

Does that make sense?
Wes

> ------------Original Message------------
> From: "Linton Ye" <lintonye@xxxxxxxxxxxxxxx>
> To: "aspectj-users" <aspectj-users@xxxxxxxxxxx>
> Date: Fri, Mar-10-2006 10:32 AM
> Subject: [aspectj-users] execution pointcut and subclassing
>
> Hi,
> 
> Recently I came across some issues regarding execution pointcut and 
> subclassing, a sample code is as following:
> 
> class Shape {
>   public void moveBy(int x, int y) {...}
> }
> 
> class Rectangle extends Shape {
> }
> 
> class Square extends Rectangle {
>   public void moveBy(int x, int y) {...}
> }
> 
> class Driver {
>   public void main(String[] args) {
>     System.out.println("[ Shape square = new Square(); 
> square.moveBy(1,1); ]");
>     Shape square = new Square();
>     square.moveBy(1,1);
> 
>     System.out.println("[ Shape rect = new Rectangle(); 
> rect.moveBy(1,1); ]");
>     Shape rect = new Rectangle();
>     rect.moveBy(1,1);
>   }
> }
> 
> aspect SomeAspect {
>   after():execution(* Rectangle.moveBy(..)) {
>     System.out.println("exec:Rectangle.moveBy");
>   }
>   after():execution(* Square.moveBy(..)) {
>     System.out.println("exec:Square.moveBy");
>   }
>   after():execution(* Shape.moveBy(..)) {
>     System.out.println("exec:Shape.moveBy");
>   }
> }
> 
> If we run the above example, we will get the following output:
> 
> [ Shape square = new Square(); square.moveBy(1,1); ]
> exec:Rectangle.moveBy
> exec:Square.moveBy
> exec:Shape.moveBy
> 
> [ Shape rect = new Rectangle(); rect.moveBy(1,1); ]
> exec:Shape.moveBy
> 
> Notice that, when square.moveBy is called, even moveBy method is not 
> defined in Rectangle class, the pointcut execution(* 
> Rectangle.moveBy(..)) still matches it, however, when rect.moveBy is called, 
> "exec:Rectangle.moveBy" doesn't show up because moveBy method is not overriden in 
> Rectangle. 
> 
> I searched on the bugzilla and tried the same program on both AspectJ 
> and ABC compiler and got the same result, so I assume it's not a bug in 
> AspectJ implementation.
> 
> The question is, what is the semantics of the method signature in 
> execution pointcut?  I checked the semantics appendix on eclipse.org and got 
> the answer that "the signature is a method signature whose qualifying 
> type is the declaring type of the method".  However, this doesn't seem 
> to explain much - the result is still contradictory.
> 
> Thanks,
> Linton
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 



Back to the top