[
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,
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
>
>