Match all method calls with a specific parameter type at any position

View: New views
6 Messages — Rating Filter:   Alert me  

Match all method calls with a specific parameter type at any position

by Tahir Akhtar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
Is there a way to declare a pointcut that matches all method calls whose
any parameter is of specific type (say java.sql.Connection).

I see that following will match methods whose last (or only) parameter
is of type Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(..,conn );

And following will match methods whose first (or only) parameter is of
type Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(conn, .. );

But how can I match when I don't know the exact position or even number
of parameters in advance?


Regards
Tahir Akhtar


_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Match all method calls with a specific parameter type at any position

by Tahir Akhtar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

One somewhat limited solution I see is to use an annotation on the
methods with thisJoinPoint.getArgs().

Tahir Akhtar wrote:

> Hi,
> Is there a way to declare a pointcut that matches all method calls
> whose any parameter is of specific type (say java.sql.Connection).
>
> I see that following will match methods whose last (or only) parameter
> is of type Connection.
>
> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
> args(..,conn );
>
> And following will match methods whose first (or only) parameter is of
> type Connection.
>
> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
> args(conn, .. );
>
> But how can I match when I don't know the exact position or even
> number of parameters in advance?
>
>
> Regards
> Tahir Akhtar
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@...
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>

_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Match all method calls with a specific parameter type at any position

by Andrew Eisenberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This should work for you.  All 3 method calls below are advised, but
the sysout call is *not* advised.

public aspect ParameterMatcher {
    before() : call(* *(..,String,..)) {
        System.out.println('S');
    }
}

public class HasParameters {
    static void doNothing(int x, int y, String z) { }
    static void doNothing(int x, String y, int z) { }
    static void doNothing(String x, int y, int z) { }
    public static void main(String[] args) {
        doNothing(0, 0, "");
        doNothing(0, "", 0);
        doNothing("", 0, 0);
    }
}

And then you can write an additional clause in the before advice to
capture the args.

On Wed, Jun 10, 2009 at 4:32 AM, Tahir Akhtar<tahir@...> wrote:

> One somewhat limited solution I see is to use an annotation on the methods
> with thisJoinPoint.getArgs().
>
> Tahir Akhtar wrote:
>>
>> Hi,
>> Is there a way to declare a pointcut that matches all method calls whose
>> any parameter is of specific type (say java.sql.Connection).
>>
>> I see that following will match methods whose last (or only) parameter is
>> of type Connection.
>>
>> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
>> args(..,conn );
>>
>> And following will match methods whose first (or only) parameter is of
>> type Connection.
>>
>> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
>> args(conn, .. );
>>
>> But how can I match when I don't know the exact position or even number of
>> parameters in advance?
>>
>>
>> Regards
>> Tahir Akhtar
>>
>>
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@...
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@...
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Match all method calls with a specific parameter type at any position

by Andy Clement :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you want to use args directly for binding, the normal (not all that
pretty) solution right now is:

pointcut havingAConnectionParameter1(Connection conn): call(* *(..))
&& args(conn,..);
pointcut havingAConnectionParameter2(Connection conn): call(* *(..))
&& args(*,conn,.. );
pointcut havingAConnectionParameter3(Connection conn): call(* *(..))
&& args(*,*,conn,.. );
pointcut havingAConnectionParameter4(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter5(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter6(Connection conn): call(* *(..))
&& args(*,*,*,*,conn,.. );

obviously you need as many variants as the max number of arguments,
but more than 6/7 arguments then I think the code you are advising has
other problems ;)

there are open enhancement requests relating to this.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=160509
https://bugs.eclipse.org/bugs/show_bug.cgi?id=233718

The second one is interesting as it discusses a lot of possible syntaxes.

cheers,
Andy.

2009/6/10 Tahir Akhtar <tahir@...>:

> Hi,
> Is there a way to declare a pointcut that matches all method calls whose any
> parameter is of specific type (say java.sql.Connection).
>
> I see that following will match methods whose last (or only) parameter is of
> type Connection.
>
> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
> args(..,conn );
>
> And following will match methods whose first (or only) parameter is of type
> Connection.
>
> pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
> args(conn, .. );
>
> But how can I match when I don't know the exact position or even number of
> parameters in advance?
>
>
> Regards
> Tahir Akhtar
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@...
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Match all method calls with a specific parameter type at any position

by Tahir Akhtar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I tried combining such pointcuts with || .
But now I am getting an "ambiguous binding of parameter(s) conn across '||' in pointcut" error when I try to advice the composed pointcut with an around advice.
Is there any work around to this error?

Regards
Tahir Akhtar

Andy Clement wrote:
If you want to use args directly for binding, the normal (not all that
pretty) solution right now is:

pointcut havingAConnectionParameter1(Connection conn): call(* *(..))
&& args(conn,..);
pointcut havingAConnectionParameter2(Connection conn): call(* *(..))
&& args(*,conn,.. );
pointcut havingAConnectionParameter3(Connection conn): call(* *(..))
&& args(*,*,conn,.. );
pointcut havingAConnectionParameter4(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter5(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter6(Connection conn): call(* *(..))
&& args(*,*,*,*,conn,.. );

obviously you need as many variants as the max number of arguments,
but more than 6/7 arguments then I think the code you are advising has
other problems ;)

there are open enhancement requests relating to this.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=160509
https://bugs.eclipse.org/bugs/show_bug.cgi?id=233718

The second one is interesting as it discusses a lot of possible syntaxes.

cheers,
Andy.

2009/6/10 Tahir Akhtar tahir@...:
  
Hi,
Is there a way to declare a pointcut that matches all method calls whose any
parameter is of specific type (say java.sql.Connection).

I see that following will match methods whose last (or only) parameter is of
type Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(..,conn );

And following will match methods whose first (or only) parameter is of type
Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(conn, .. );

But how can I match when I don't know the exact position or even number of
parameters in advance?


Regards
Tahir Akhtar


_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

    
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users


  


_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Re: Match all method calls with a specific parameter type at any position

by Andy Clement :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The workaround is to have multiple small pieces of advice that all call whatever code you then want to run

pointcut havingAConnectionParameter1(Connection conn): call(* *(..)) && args(conn,..);
pointcut havingAConnectionParameter2(Connection conn): call(* *(..)) && args(*,conn,.. );
etc...

before(Connection c): havingAConnectionParameter1(c) {run(c);}
before(Connection c): havingAConnectionParameter2(c) {run(c);}
etc...

Andy.

2009/6/21 Tahir Akhtar <tahir@...>
I tried combining such pointcuts with || .
But now I am getting an "ambiguous binding of parameter(s) conn across '||' in pointcut" error when I try to advice the composed pointcut with an around advice.
Is there any work around to this error?

Regards
Tahir Akhtar


Andy Clement wrote:
If you want to use args directly for binding, the normal (not all that
pretty) solution right now is:

pointcut havingAConnectionParameter1(Connection conn): call(* *(..))
&& args(conn,..);
pointcut havingAConnectionParameter2(Connection conn): call(* *(..))
&& args(*,conn,.. );
pointcut havingAConnectionParameter3(Connection conn): call(* *(..))
&& args(*,*,conn,.. );
pointcut havingAConnectionParameter4(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter5(Connection conn): call(* *(..))
&& args(*,*,*,conn,.. );
pointcut havingAConnectionParameter6(Connection conn): call(* *(..))
&& args(*,*,*,*,conn,.. );

obviously you need as many variants as the max number of arguments,
but more than 6/7 arguments then I think the code you are advising has
other problems ;)

there are open enhancement requests relating to this.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=160509
https://bugs.eclipse.org/bugs/show_bug.cgi?id=233718

The second one is interesting as it discusses a lot of possible syntaxes.

cheers,
Andy.

2009/6/10 Tahir Akhtar tahir@...:
  
Hi,
Is there a way to declare a pointcut that matches all method calls whose any
parameter is of specific type (say java.sql.Connection).

I see that following will match methods whose last (or only) parameter is of
type Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(..,conn );

And following will match methods whose first (or only) parameter is of type
Connection.

pointcut havingAConnectionParameter(Connection conn): call(* *(..)) &&
args(conn, .. );

But how can I match when I don't know the exact position or even number of
parameters in advance?


Regards
Tahir Akhtar


_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users

    
_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users


  


_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users



_______________________________________________
aspectj-users mailing list
aspectj-users@...
https://dev.eclipse.org/mailman/listinfo/aspectj-users