Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] how to intercept joinpoints ...

Hello,

Davi, I have tried not to change the original code and that was the reason why I have suggested that approach. If, however, you can change the code in Aspect1, you can either use the way you told us, with Annotations, or make the System.out.println("I'm before m1"); invocation be inside in a method, such as:

public aspect Aspect1 {

   pointcut pc(): execution(* Foo.m1(..));

   public void beforeM1() {
	System.out.println("I'm before m1");
   }

   public void afterM1() {
	System.out.println("I'm after m1");
   }

   before(): pc() {
	beforeM1();
   }

   after(): pc() {
	afterM1();
   }
}

public aspect Aspect2 {

pointcut beforeM1(): execution(public void Aspect1.beforeM1()) && within(Aspect1);

pointcut afterM1(): execution(public void Aspect1.afterM1()) && within(Aspect1);

   before(): beforeM1() {
	System.out.println("I'm before before m1.");
   }

   after(): beforeM1() {
	System.out.println("I'm after before m1.");
   }

   before(): afterM1() {
	System.out.println("I'm before after m1.");
   }

   after(): afterM1() {
	System.out.println("I'm after after m1.");
   }

}

Fortunately, we have several ways to achieve the same goal, right? :)

Now, Fabio, when you cannot change the Aspect1 source code, and you have a source code like you have showed us, with the System.out.println() invocation inside the aspect advice, then I don't see any other way to do what you intend except the one I have showed you. And if you don't know what is going on inside the aspect itself, you can always use an aspect to tell you that (the following aspect is supposed to print before any advice from Aspect1, and it will print the aspect advice signature):

public aspect Aspect3 {

   before() :        adviceexecution() &&
       within(Aspect1) {
// This would print something like: // adviceexecution(void pt.iscte.ci.example1.Aspect1.before())
       System.out.println(thisJoinPoint.toString());
   }

}

Hope this helps. Regards,

Paulo Zenida


Citando Davi Pires <inhodpr@xxxxxxxxx>:

Not very neat...

If you need a way to "name" your advices, I suggest using annotations. For
example:

public aspect Aspect1 {
      pointcut pc(): execution(* Foo.m1(..));

      @BeforeAdvice
      before(): pc() {
              System.out.println("I'm before m1");
      }

      @AfterAdvice
      after(): pc() {
              System.out.println("I'm after m1");
      }

}

public aspect Aspect2 {

   pointcut beforeM1(): within(Aspect1) && @annotation(BeforeAdvice);
   pointcut afterM1(): within(Aspect1) && @annotation(AfterAdvice);

   before(): beforeM1() {
       System.out.println("I'm before before m1.");
   }

   after(): beforeM1() {
       System.out.println("I'm after before m1.");
   }

   before(): afterM1() {
       System.out.println("I'm before after m1.");
   }

   after(): afterM1() {
       System.out.println("I'm after after m1.");
   }

}

This works as expected, and generates a correct output.

Davi


On 10/20/06, Paulo Alexandre Corigo Zenida <paulo.zenida@xxxxxxxx> wrote:

Hello,

I believe the following Aspect would to what you wish:

public aspect Aspect2 {

    pointcut pc(): execution(* Foo.m1(..));

    before() :  adviceexecution() &&
        within(Aspect1) &&
        if(thisJoinPoint.toString().equals("adviceexecution(void
pt.iscte.ci.example1.Aspect1.before())")) {
        System.out.println("I'm before before m1");
    }

    before() :  adviceexecution() &&
        within(Aspect1) &&
        if(thisJoinPoint.toString().equals("adviceexecution(void
pt.iscte.ci.example1.Aspect1.after())")) {
        System.out.println("I'm before after m1");
    }

    after() :   adviceexecution() &&
        within(Aspect1) &&
        if(thisJoinPoint.toString().equals("adviceexecution(void
pt.iscte.ci.example1.Aspect1.before())")) {
        System.out.println("I'm after before m1");
    }

    after() :   adviceexecution() &&
        within(Aspect1) &&
        if(thisJoinPoint.toString().equals("adviceexecution(void
pt.iscte.ci.example1.Aspect1.after())")) {
        System.out.println("I'm after after m1");
    }
}

The output with this aspect and your code is:

I'm before before m1
I'm before m1
I'm after before m1
I'm on m1
I'm before after m1
I'm after m1
I'm after after m1


Hope this helps.

Best Regards,

Paulo Zenida



Citando Davi Pires <inhodpr@xxxxxxxxx>:

> I was wondering if the following pointcut could do this:
>
> pointcut x(): adviceexecution() && within(Aspect1);
>
> []'s,
> Davi Pires
>
> On 10/20/06, Fabio Fagundes Silveira <ffs@xxxxxx> wrote:
>>
>> Hello,
>>
>>   Is it possible to intercept an before, around or an after defined in
>> an aspect?
>>
>>   Let's suppose that method "m1" of class Foo is an advised method.
>>
>> public class Foo {
>>         public void m1() {
>>                 System.out.println("I'm on m1");
>>         }
>>
>>         public static void main(String[] args) {
>>                 Foo foo = new Foo();
>>                 foo.m1();
>>         }
>> }
>>
>> public aspect Aspect1 {
>>         pointcut pc(): execution(* Foo.m1(..));
>>
>>         before(): pc() {
>>                 System.out.println("I'm before m1");
>>         }
>>
>>         after(): pc() {
>>                 System.out.println("I'm after m1");
>>         }
>>
>> }
>>
>>
>>    This way, m1 is correctly being intercepted, ok? The output is of
>> course:
>>
>> I'm before m1
>> I'm on m1
>> I'm after m1
>>
>> So far so good ... But I would like to implement an aspect that  could
>> print something like:
>>
>> I'm before before m1
>> I'm before m1
>> I'm after before m1
>>
>> I'm on m1
>>
>> I'm before after m1
>> I'm after m1
>> I'm after after m1
>>
>>
>> (ie. an before and an after of an before, and an before and a after of
>> an after)
>>
>> Is it possible?
>>
>> Thanks a lot
>> _______________________________________________
>> 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