Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ advising with generic pointcuts

On 4 January 2011 15:51, Srinivas <tssrinivas@xxxxxxxxx> wrote:
> This however does not seem to work if the argument type is a "grandchild" of
> Vehicle. That is , if Toyota-->Car-->Vehicle, then this does not seem to
> match Toyota. Will it only match Car, and not Toyota ?

Not quite sure what you mean, it should work for all subtypes of
Vehicle (Car and Toyota).  Are you sure your Toyota extends Car?  Here
is a test program:

import org.aspectj.lang.*;
import org.aspectj.lang.annotation.*;

public class Code {
  public static void main(String argv[]) {
    new Code().run();
  }

  public void run() {
    Vehicle v = new Vehicle();
    Car c = new Car();
    Toyota t = new Toyota();
    a(v);
    a(c);
    a(t);
    b(c);
    b(t);
    c(t);
  }

  public void a(Vehicle v) {}
  public void b(Car c) {}
  public void c(Toyota t) {}
}

@Aspect
class Azpect {

@Before("execution(public * *(Vehicle+, ..))  && args(myarg, ..) ")
public void m(Vehicle myarg,JoinPoint.StaticPart jpsp) {
  System.out.println("Vehicle is "+myarg.getClass().getName()+" at "+jpsp);
}


}

class Vehicle {
}

class Car extends Vehicle {
}

class Toyota extends Car {
}

and produces the expected 6 lines of output:
Vehicle is Vehicle at execution(void Code.a(Vehicle))
Vehicle is Car at execution(void Code.a(Vehicle))
Vehicle is Toyota at execution(void Code.a(Vehicle))
Vehicle is Car at execution(void Code.b(Car))
Vehicle is Toyota at execution(void Code.b(Car))
Vehicle is Toyota at execution(void Code.c(Toyota))

cheers
Andy


Back to the top