Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How do i apply a pointcut to only an object which implements an interface or is of type X.class or subclass

I am a bit confused.  Do you want to catch constructor calls made from the BarInterface or construction of things implementing BarInterface - I'll assume the latter.  This:

public pointcut PT_new() :
      within(com.foo.BarInterface) &&
      call(*.new(..));

means calls to any constructor made from within BarInterface - there isn't usually much code in an interface so you won't match much with that.  Adding a + to BarInterface will mean calls to any constructor (of anything)  made from within BarInterface or a subtype of it. (In neither case has a restriction been placed on the type of the object being constructed)

If you want to catch construction of classes implementing BarInterface, you need to specify the type pattern in the call pointcut:

interface SomeInterface { }

class C implements SomeInterface { }
class D implements SomeInterface { }

aspect X {
        public static void main(String []argv) {
                new C(); // advised
                new D(); // advised
        }

        before(): call(SomeInterface+.new(..)) {}
}

Note: because execution() join points occur in the same type as you are naming using within() - you will have been getting away with using within() and execution() together:

execution(*.new(..)) && within(SomeInterface+);

that just wont work for call() and within()

Andy.
2008/12/3 Andrew Eisenberg <andrew@xxxxxxxxxxxx>
>   public pointcut PT_new() :
>
>       within(com.foo.BarInterface) &&
>       call(*.new(..));
>

Try this instead:

  public pointcut PT_new() :
      within(com.foo.BarInterface+) &&
      call(*.new(..));

'+' means to include subclasses.
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top