Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Is it possible to define a scope of methods accepting at least one non primitive argument?

There is a long standing issue with parameter matching, it originally
came up because of this popular use case:

void foo(@NotNull String one)
void foo(int x, @NotNull String two)
void foo(String y, @NotNull String two, @NotNull String three)

how do you write some advice to assert that the @NotNull parameters
are not null.  I feel this is similar to what you want to achieve but
you don't have the annotation you kind of assume it is always there on
all parameters (sorry if I'm assuming too much here).

This means you are left with the same problem that we have for the
annotation variant, although we have a way to indicate a particular
parameter (using '..' notation), we can't specify multiple parameters
'wherever they might be', so for the above case you end up with:

pointcut one(Object o): execution(* *((@NotNull *),..)) && args(o,..);
pointcut two(Object o): execution(* *(*,(@NotNull *),..)) && args(*,o,..);
pointcut three(Object o): execution(* *(*,*,(@NotNull *),..)) && args(*,*,o,..);

one for each position.  And separate advice for all pointcuts (that
advice can be small, just delegating to a common function):

before(Object o): one(o) {
  checkNotNull(o);
}

Yes it is tedious, but if you have more than, say 8 variants of it,
you have other problems with your code :)

In your case I guess the pointcuts read:

pointcut one(Object o): execution(* *(..)) && args(o,..);
pointcut two(Object o): execution(* *(..)) && args(*,o,..);

(multiple pointcuts will match against one method signature taking
multiple non primitive arguments).

cheers
Andy


On 14 December 2011 07:07, Mark <mark.kharitonov@xxxxxxxxx> wrote:
> I wish to write an aspect to assert that every argument is given. It should
> only be applied to methods, having at least one non primitive parameter. For
> instance:
> public void f(int x)  <---- should not be in scope, because x cannot be
> compared with the null
> public void g(int x, String s) <-- should be in scope, because s can be
> compared against the null
>
> Is there a way to express such a scope in AspectJ?
>
> Thanks.
>
> --
> View this message in context: http://aspectj.2085585.n4.nabble.com/Is-it-possible-to-define-a-scope-of-methods-accepting-at-least-one-non-primitive-argument-tp4195341p4195341.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top