Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How can I get a field's value for a call made from within a static context?

Dear Adrian,
 
Thank you so much for your efforts!

Yes, the highlighted line:  c.call2(b); is matched by my advice. But I don't quite agree that there is no "this" object just because I don't know how to get it. Please note that A.call2() is an instance method, so if I have the following code:
 
A a = new A();
a.call2();
 
Now, although the c.call2(b); is caught in a static method (A.call1), the static method is actually called by an instance of A (which is 'a' in the above code). In this case, I think we should be able to get the caller's instance -- 'a', right? Then, my purpose is to test if the caller 'a' passes its field   ('b' in this case) to the callee 'c' as argument.
 
So can I get the caller 'a' in that advice?
 
 
I hope this makes my question clearer, and thanks again for your help.

-- Sunny
 
 
On 3/24/06, Adrian Colyer <adrian.colyer@xxxxxxxxx> wrote:
Hi Sunny,
I've read your question several times, but I'm afraid I'm still not quite getting it...

You have advice matching a call join point arising from executing the highlighted line of code:


public class A
{
         B b = new B();
         public static void call1(B b)
        {
               C c = new C();
>>>>       c.call2 (b);
        }
 
At this call join point there is a target object (the instance of C), and an argument (an instance of B). There is no 'this' object, because as you say, this is a static method.

At this stage it doesn't make sense to ask whether the argument value is the same as the object referenced by the field 'b' in an instance of A, because we don't know which instance of A you are asking the question of.

I *think* you might be asking whether you can test if the argument 'b' passed to C when A.call1 is called by A.call2 is the same as the argument 'b' passed on the call to A.call1 from within A.call2.  If that's the case, you can express something like that as follows:

pointcut call1CalledFromCall2(A anA, B aB)  :
   call(* A.call1(B)) && this(anA) && args(aB) && withincode(* A.call2(..));

pointcut call2FromWithinCall1(B aB) :
   call(* C.call2 (B)) && args(aB);

(the duplicate methods names really don't help here...!)

pointcut call2FromWithinCall1InCflowOfCall2(A anA, B originalB, B passedB) :
  call2FromWithinCall1(passedB) &&
  cflow(call1CalledFromCall2(anA, originalB);

before(A anA, B originalB, B passedB) : call2FromWithinCall1InCflowOfCall2(anA,originalB,passedB) {
  if (passedB != orginalB) { ... }

  // or if you prefer,
  if (passedB != anA.b) { ...}
}

Hope that makes /some/ sense!

Regards, Adrian.


On 21/03/06, Sunny < sunfire001@xxxxxxxxx> wrote:
I am sorry if this is a duplicate message to you. I doubt my previous message was delivered successfully without an appropriate subject.
 
Hi,
 
Suppose I have three classes A, B, and C (C is omitted here), where the static method call1 can access an instance field passed by a non-static method call2. Also suppose there is a pointcut that catches all method calls. Now when the call: c.call2(b) is intercepted, I can obtain the callee object which is referenced by c, also I can only get the caller's TYPE which is class A (since the method call is made within a static context). My question is the following:
 
given the argument b obtained in the advice associated with the above pointcut, how can I know whether the object referenced by b is actually the object that is referenced by a field in class A? In other words, I would like to know whether A's object is passing its field b into C's object or not.
 
public class A
{
         B b = new B();
         public static void call1(B b)
        {
               C c = new C();
               c.call2(b);
        }
 
        public void call2()
       {
               call1(b);
       }
}
 
I tried Java reflection to check if one of A's fields references b (by calling Field.get(Object obj) == b). This won't work if the field is an instance field because in this case the method requires an enclosing object as argument. But how can I get the caller's object?
 
Thanks a lot for any idea!
 
SunnyDay

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





--
-- Adrian
adrian.colyer@xxxxxxxxx

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




Back to the top