Hi Mouna,
I think this is an area where the AspectJ
compiler is limited but it should work. You’d like a program like this to work:
public aspect BindingOr {
pointcut a(String o, int x) :
execution(* foo(..)) && args(o, x);
pointcut b(String o) :
execution(!static * *(..)) && args(o);
pointcut aOrB(String o): a(o, *) ||
b(o);
public static void main(String args[])
{
aspectOf().foo("woof",
1);
aspectOf().bar("zip");
}
void foo(String o, int x) {}
void bar(String o) {}
after(String o) returning: aOrB(o) {
System.out.println("executed
"+o);
}
}
Unforunately this fails even though there
could never be any ambiguity:
C:\devel\scratch\BindingOr.aj:3 [error]
ambiguous binding of parameter(s) o across '||' in pointcut
The best you can do is something like
this:
…
after(String o) returning: a(o) {
common(o);
}
after(String o) returning: b(*, o) {
common(o);
}
private void common(String o) {
System.out.println("executed
"+o);
}
In your case, you might also be able to
use this approach of binding separately:
after(String o) returning: (a(*) ||
b(*, *)) && args(o, ..) {
System.out.println("executed
"+o);
}
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On
Behalf Of mouna SAHIB
Sent: Thursday, May 04, 2006 6:42
AM
To: wes@xxxxxxxxxxxxxx;
aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users]
COmbining pointcuts in the same advice
Thank you all for your
answers!
As I've tried to explain it before, my problem is to do something like this:
pointcut p1(String s, int i):execution(@tag * *(..)) && args(s, i);
after(): p1(){
//...
}
pointcut p2(String s):execution(@tagBis * *(..)) && args (s);
after() : p2(){
//...
}
and combine the two pointcuts in a way like:
pointcut p3(String s, *): p1(s, ??) || p2(s);
after(??) :p3(s,*){
//...
}
Is it possible to do something like that?
My two pointcuts p1() and p2() have both a parameter of type String and the
p3() pointcut will treat only about this String no matter is the other
parameters.
If something like this is possible to do, then how? I've tried several
possiblities but I still have problems with parameters.
Hope I'm clearer! (it is unfortunatly not a homework :), but in my project it
will be easier and more clean to do something like this, because I have one
functionnality that is called in almost all classes of all modules! What is
sure is that I'm a beginner on aspectj ans do not know well all its features).
Still need your help please!
Thanks
2006/5/4, Wes <wes@xxxxxxxxxxxxxx>:
> to make an advice crosscutting multiple pointcuts
Each advice has exactly one pointcut and one set of variables bound by that
pointcut.
Variables declared by a pointcut must be bound for all affected join points
using one or
more of this(), target(), and args(). You can have advice in an
abstract aspect on an
abstract pointcut, and it will work from any number of concrete subaspects
declaring
the abstract pointcut differently. You can access the arguments
directly from the join
point, and you can delegate to methods, as Ron kindly suggested. (That seemed
to me to
be the best solution, but your problem is not stated clearly - it sounds more
like a
homework problem.) That's basically it.
As I mentioned to Marco, you might find it much easier to go through the
AspectJ
Programming Guide and the examples carefully before trying any significant
aspects,
since the guide answers most questions. (You'll probably also want
to read the FAQ
sections "Programming notes and tips" "Common problems" and
"Getting Help".)
The documentation comes with Eclipse AJDT and can be viewed online
or downloaded with the AspectJ release.
Wes
http://www.eclipse.org/aspectj/doc/released/progguide/index.html
http://www.eclipse.org/aspectj/doc/released/index.html
http://www.eclipse.org/aspectj/downloads.php
------------Original Message------------
From: "mouna SAHIB" <mouna.sahib@xxxxxxxxx>
To: aspectj-users@xxxxxxxxxxx
Date: Thu, May-4-2006 1:24 AM
Subject: Re: [aspectj-users] COmbining pointcuts in the same advice
Could you please give me an example how to use adviceexcution in that case?
The problem is to make an advice crosscutting multiple pointcuts with different
types of parameters with at least one parameter that has the same type for all
pointcuts.
I've tried to use adviceexcution()pointcut, bbut the problem still the same:
how to access to the parameters of other poitncuts?
On the other hand, the example you proposed bellow do
not match!! (inconsistent binding is the error)
Any other ideas please?
Thanks anyway.
Mouna
2006/5/3, Ron Bodkin <rbodkin@xxxxxxxxxxxxxx>:
Sometimes you can achieve an effect like this by having multiple advices that
just dispatch to a helper method, e.g.,
aspect Recorder {
after(String s) returning: saySmthng(s) {
recordSpeech(s);
//handle specific requirements here
}
after(String s) returning: sayWorld(s) {
recordSpeech(s);
//handle specific requirements here
}
private void recordSpeech(String s) {
System.out.println("smthg" + s);
}
}
You might also just use an extra piece of advice:
aspect Recorder {
after(String s) returning: (saySmthng(s) || sayWorld(s)) {
recordSpeech(s);
}
after(String s) returning: saySmthng(s) {
//handle specific requirements here
}
after(String s) returning: sayWorld(s) {
//handle specific requirements here
}
…
}
However, I don't know what you hope to achieve by "sequencing"
though. You can track state of a sequence of join points with state in the
aspect (possibly holding state in a thread local or using a percflow
instantiation model). You could also advise adviceexecution, which would let
you do consistent logic before each advice runs although it
By the way, in past I've seen issues where the compiler complains about
"ambiguous binding of parameter(s)" but when you are binding to the
same thing (e.g., args(s)) in both forks of the or, it seems to work. E.g.,
this works:
pointcut a(Object o) : execution(* foo(..)) &&
args(o);
pointcut b(Object o) : execution(!static * *(..))
&& args(o);
pointcut aOrB(Object o): a(o) || b(o);
But this fails:
pointcut a(Object o) : execution(* foo(..)) &&
this(o);
pointcut b(Object o) : execution(!static * *(..))
&& args(o);
pointcut aOrB(Object o): a(o) || b(o);
I would prefer to have short-circuit evaluation rules so that if a matches o is
bound to this, but I gather this was changed to improve performance?
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx]
On Behalf Of mouna SAHIB
Sent: Wednesday, May 03, 2006 6:30 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] COmbining pointcuts in the same advice
What I want to express but I have probmlems communicating it is the idea that
there is one pointcut with an advice related to it that represent the
minimalist code that should be advising other join points (no matter the type
of arguments of each of the other pointcuts to advice, but they have all, at
least one similar type of arguments that will be manipulated by this minimalist
code.
In my example, the "minimalist" advice is :
private pointcut saySmthg(String s):execution(@tagBis * *(..))&& args
(s);
after(String s): saySmthg(s)&& sayWorld(s) {
System.out.println
("smthg" + s);
}
which is applied when running the method (@tagBis) and when the
@tag method is running but after the first advice (sayWorld(s)) was applied to.
In some other words, I'm trying to make such a sequence of advice that are
added to others that are applied before in other methods.
2006/5/3, Ron Bodkin <rbodkin@xxxxxxxxxxxxxx
>:
What behavior are you trying to achieve?
When @tag methods run, Print hello and then "smthng "+s?
When @tagBis methods run, just print "smthngs "+s?
How does the second argument factor in here?
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto: aspectj-users-bounces@xxxxxxxxxxx]
On Behalf Of mouna SAHIB
Sent: Wednesday, May 03, 2006 6:03 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] COmbining pointcuts in the same advice
Euh, my problem then is to access to the two arguments of the two pointcuts!
What I presented inthe example doesn't match.
2006/5/3, mouna SAHIB < mouna.sahib@xxxxxxxxx
>:
Okay, here is my exmaple:
The target class with 2 methods annotated by @tag and @tagBis:
package HelloWorld;
public class HelloWorld {
@tag(value = "mamethode")
public void sayHello( String s){
s = "sayHello";
System.out.println(s);
}
@tagBis
public void say(String s){
System.out.println("say"
+s);
}
public static void main(String[] args) {
new
HelloWorld().sayHello(null);
new HelloWorld().say("a
call to say()");
}
}
and my aspect:
public aspect World {
private pointcut sayWorld(String s):execution(@tag *
*(..))&& args(s);
after(String s): sayWorld(s) {
System.out.println("World!");
}
private pointcut saySmthg(String s):execution(@tagBis *
*(..))&& args (s);
after(String s): saySmthg(s)&& sayWorld(s) {
System.out.println("smthg"
+ s);
}
}
The two poitncuts have here the same type of arguments and I would like to
treat the two the same way (here a simple System.out.println("smthg"
+ s);)
All in all, I would like to advise the first pointcut by the second to add an
other treatement, envetually the one made with the second
poitncut;
I hope I'm clear and excuse me expressing with a bad english ...
2006/5/3, Ron Bodkin < rbodkin@xxxxxxxxxxxxxx
>:
Mouna,
If you use || then you aren't guaranteed to have access to arguments from p2.
Maybe you could post a concrete example of what you're trying to achieve.
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx]
On Behalf Of mouna SAHIB
Sent: Wednesday, May 03, 2006 5:27 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] COmbining pointcuts in the same advice
but thisJoinPoint.getArgs () gives only arguments of the pointcut p1()!
Is there a posiblity to have access to argumets of both p1 and p2? Because I
want to croscut the two bby the same code advice,and should for this manipulate
the arguments of the pne and the other!
2006/5/3, Ron Bodkin <rbodkin@xxxxxxxxxxxxxx
>:
Mouna,
You can use p2(*) or p2(Type) to use the pointcut without binding. However, if
you want to use t2 in the body of your advice you need to bind it. It can be
possible to use thisJoinPoint.getArgs() to get arguments, or perhaps (p2(*) ||
p1(*)) && args(t) if you want the single argument in either case.
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx]
On Behalf Of mouna SAHIB
Sent: Wednesday, May 03, 2006 3:16 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] COmbining pointcuts in the same advice
Hi all,
I have some pointcuts in my aspect:
pointcut p1(<argumentType t>): execution (...) && args(t);
after(<argumentType t>): p1(t){
//some code
}
pointcut p2(<argumentType t2>): execution (...) && args(t2);
after(<argumentType t2>): p2(t2){
//some code
}
and would rather like to do something like this:
pointcut p2 (<argumentType t2>): execution(...)&& args(t2);
after() : p2(t2) || p1(<argumentType t>){
//some code
}
but I don't kknow how to declare arguments in the pointcut and advice in this
case!
If I do like above, I can't use t2 in the advice body ! What can I do it
please?
Thanks for suggestion
Mouna
_______________________________________________
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
_______________________________________________
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
_______________________________________________
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