Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Matching a Class That Contains an Annotation

Hi Andy,

I'm not familiar with any of the compiler args, so I will look into -XhasMember. What you have here though is very clever - I would not have thought to use a second annotation in this way. This really saved me a lot of time trying to solve the problem.

As you can probably guess from the second case, we want to factor out code into testable methods, but not allow direct invocation outside of the class itself or outside of unit tests. I completely forgot about the AspectJ reference variables (I'm getting back into AOP after a number of years), so I will be reading http://www.eclipse.org/aspectj/doc/next/progguide/index.html again from top to bottom.

I'll take your word for the moment that what I want to express is currently not possible. This is what I was doing a number of years ago to deal with the limitation:
    declare error:
        (call(@Testable * Foo.*(..))               
        || get(@Testable * Foo.*)
        || set(@Testable * Foo.*)
        || call(@Testable Foo.new(..)))
        && !within(Foo+):
        "called from elsewhere!";

Where I needed to declare this separately for every class I wanted to protect. It seems the AspectJ syntax has changed on me since then, so the above probably doesn't work anymore without modification.

However, thanks again for the fantastic @Inject/@HasInjections solution!

Regards,
Nick


-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Andy Clement
Sent: Thursday, July 08, 2010 5:44 PM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] Matching a Class That Contains an Annotation

Hi,

First problem:

aspect X {
  // If any members are @Inject, mark the type with @HasInjections
  declare @type: hasmethod(@Inject * *(..)): @HasInjections;

  // look for calls to constructors on types with @HasInjections
  declare warning: call((@HasInjections *).new(..)): "don't do it!";
}

(will need to compile with -XhasMember)

---
For the second case, what you'd like to write (but you can't) is
something like this:

declare warning: call(@Testable * *(..)) &&
if(!thisJoinPoint.getSignature().getDeclaringType().isAssignableFrom(thisEnclosingJoinPointStaticPart.getSignature().getDeclaringType())):
"called from elsewhere!";

it is a shame you cant.. this is the kind of thing
https://bugs.eclipse.org/bugs/show_bug.cgi?id=292262 is supposed to
allow.

cheers
Andy

On 8 July 2010 13:59, Nick Pace (npace) <npace@xxxxxxxxx> wrote:
> Hello. I would appreciate any suggestions on the following:
>
>
>
> I'm having an hard time expressing two scenarios I have via pointcuts. I
> want to declare an error anytime someone tries to instantiate Foo() when
> there is an @Inject annotation anywhere else in the same class. For
> instance:
>
>
>
> public class Foo {
>
>     public Foo() {
>
>     }
>
>
>
>     public Foo(boolean bar) {
>
>     }
>
>
>
>     @Inject
>
>    public setBean() {
>
>    }
>
> }
>
>
>
> So as an example, "new Foo()" "new Foo(false)" should both be matched by the
> pointcut.
>
>
>
> I also want to *not* match calls from within the same class (or it's
> subclasses) to a method annotated with @Testable declared within the same
> class (or superclass)
>
>
>
> public class Foo {
>
>     public Foo() {
>
>         execute(); <-- Should not match
>
>     }
>
>
>
>     @Testable
>
>    public static void execute() {
>
>    }
>
> }
>
>
>
> public class Bar {
>
>     public Bar () {
>
>         Foo.execute(); <-- Should match
>
>     }
>
> }
>
>
>
>
>
> I've been able to make specific pointcuts for each class to satisfy the
> behavior I want - but that's a lot of pointcuts to maintain. I would rather
> have a general pointcut for the general behavior. I think I am just running
> up against a limitation of what can be expressed in AspectJ. But I
> appreciate any insights anyone may have on this.
>
>
>
> Thank you,
>
> Nick
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top