[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Exposing Arguments to Advice


I will admit it has been a while since I have done this, but you can expose just part of the arguments. The problem you run into which I do not think there is a solution to is that you can not arbitrarily pick out a n arguments that fall in the middle of the list. I once tried to do this to no avail [and with some discussion]. What I mean by this is the following:

public class ParametersInMethodCalls
   public void example1(ClassX x, double I, String s)

   public void example2(int I, ClassX x, double d)

public void example2(float I, String s, ClassX x)
public static void main(String[] args)
ParametersInMethodCalls calls = new ParametersInMethodCalls();
calls.example1(new ClassX(), 0.0d, new String("s)"));
calls.example2(0, new ClassX(), 0.0d);
calls.example2(0.0f, new String("s"), new ClassX());

And the aspect is:

public aspect RandomMethodAspect
pointcut examples(ClassX x) :
execution(public * ParametersInMethodCalls.*(..))
&& ( args(.., x) || args(x, ..) );
before(ClassX x) : examples(x)
System.out.println("Entering: " + thisJoinPoint);

The result you will see is:

Entering: execution(void com.regular.ParametersInMethodCalls.example1(ClassX, double, String))
Entering: execution(void com.regular.ParametersInMethodCalls.example2(float, String, ClassX))

So the method where "ClassX" is the middle parameter is lost. The aspectj guys will have to comment on why/how this occurs. So if you need to pickup the other one, then yes you will need to expose all arguments.


Wilkerson, Cory wrote:

Hello All,

I'm fairly new to the world of AspectJ - quite fond of it thus far -
but, I've hit the wall WRT a particular issue; here's to hoping that a
wise AspectJ-sage can get me through.

In short, I'd like to be able to write a pointcut that looks something
like the following:

pointcut sendTransaction(TransactionManager tm, String foo):
execution(* TransactionManager.sendTransaction(String, ..)) && target(tm) && args(foo);

Instead, based on my limited knowledge, I'm having to write (Note
argument "bar" in this example):

pointcut sendTransaction(TransactionManager tm, String foo, String bar):
execution(* TransactionManager.sendTransaction(String, String)) && target(tm) && args(foo, bar);

I know that's probably crazy-talk but just wanted to ensure that if I
plan on exposing the arguments of a method to the advice, it's an
all-or-nothing scenario.

Thanks for any help,
Cory Wilkerson
aspectj-users mailing list