Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] static pointcut for "super"

Hi Steve.

On Fri, Dec 2, 2011 at 3:50 AM, Steve Ash <stevemash@xxxxxxxxx> wrote:
> I would like to create an aspect to declare an error if a particular method
> is called on the super from a subtype.  Something like:
>
> public class SuperClass {
>    public final void dontCallThisFromBase() { }
> }
>
> public class BaseClass extends SuperClass {
>    public void doSomething() {
>       super.dontCallThisFromBase(); // would love to be a compiler error
>    }
> }

Note that you don't need to use super here, since you're not
overriding the method.

> public class UnrelatedClass {
>    public void unrelatedMethod() {
>       new SuperClass().dontCallThisFromBase(); // this is ok
>    }
> }
>
> I'm having a hard time matching the join point with the super... any
> thoughts?  Or should this work and I'm just doing something stupid?
>
> I first wanted to create a general @CantCallFromSubclass attribute that I
> could throw on the declaration of dontCallThisFromBase() -- but that doesn't
> seem to be possible... so plan B was just creating a specific aspect for
> this specific case.. but I'm running into trouble there too.

For the specific case, I'd say something like
within(SuperClass+) && !within(SuperClass) && call(*
SuperClass.dontCallThisFromBase())

For a less specific case, you could use
within(SuperClass+) && !within(SuperClass) &&
call(@CantCallFromSubclass * SuperClass.*())
but you're still limited to the SuperClass hierarchy.

If you want to be even more generic, you might be able to use an
abstract generic aspect (I've never used it though, and I'm not sure
it's compatible with within()), but you'd still need to explicitely
declare a concrete aspect for each hierarchy.

Frank


Back to the top