Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: How to log both the caller object and the executing object of a setter pointcut

Those are two different events:

One joinpoint is the field-set of data that occurs in the
Test.updateData() method.  Joinpoint is field-set, EnclosingJoinPoint
is method-execution of Test.updateData()

Second joinpoint is the method-call of updateData(), made from
Runner.go().  Joinpoint is method-call, EnclosingJoinPoint is
method-execution of Runner.go().

AspectJ doesn't (currently) provide a way to match a pair of events
like that.  In your advice you *could* use
Thread.currentThread().getStackTrace() to see the originator of the
call to updateData (in your advice against the field-set).  Or you
could annotate the mutator method in your Test class and detect
callers of it (call(@Mutator * *(..)) - where the enclosing joinpoint
of the call will be your Runner.go()).

If anyone wants to dig up an old (or propose a new) language construct
for this kind of thing, I'd be interested.  I see requests for this
kind of thing now and again.

> By the way, why would the constructor call point cut always give a null for thisJoinPoint.getTarget(), while constructor execution ponit cut would not?

See http://www.eclipse.org/aspectj/doc/released/progguide/semantics-joinPoints.html
- there is no target for constructor-call.  The reason relates to
avoiding letting the advice see something that hasn't been initialized
yet - it is covered now and again in the mailing list archives.

As I said, you can use after() returning to obtain the instance, or
use the initialization joinpoint:

after(): initialization(Foo.new(..)) {
		System.out.println(thisJoinPoint.getTarget());
}

Andy

On 15 March 2010 02:55,  <ekinrf@xxxxxxx> wrote:
> Hi Andy,
>
> Thanks for you answer, it did help me a lot. However I still got some confusions:
>
> Here's an example of my set field point cut.
>
> public class Test1
> {
>    private int data = 0;
>
>    public void updateData(int data)
>    {
>        this.data = data;
>    }
> }
>
> public class Runner
> {
>    void go()
>    {
>         new Test1().updateData(0);
>    }
>
>   public static void main(String [] args)
>   {
>      new Runner().go();
>   }
> }
>
> public pointcut setter() : set(* Test1.*);
>
> Now i got the pointcut -- setter, and what I want to log is the caller of the updateData() method, which is the instance of the Runner. But using  neither thisJoinPoint nor thisEnclosingJoinPoint cannot trace back to the Runner. Any suggestion?
>
> By the way, why would the constructor call point cut always give a null for thisJoinPoint.getTarget(), while constructor execution ponit cut would not?
>
> Regards with many thanks.
>
>
> Andy Clement wrote:
>>
>> On 14 March 2010 13:56, rstyle <ekinrf@xxxxxxx> wrote:
>>> I am trying to write a logger, and need to log both the caller object and
>>> the executing object of a setter call. I used a field set point cut,
>>> however, found no caller's context can be obtained from the
>>> 'thisJoinPoint.'
>>> Can anyone please give me some hints to solve the problem?
>>
>> I only see thisJoinPoint.getThis() return null when the calling
>> context is a static method - where is your field set join point?
>>
>> Sometimes thisEnclosingJoinPoint may be useful as that will give you
>> the surrounding join point containing the field set.
>>
>>> Is there a way to access caller's context with a execution join point?
>>
>> no (other than introspecting the thread stack).  Perhaps you could
>> construct something ugly with cflow but it wouldn't perform well.
>>
>> Obtaining the caller and instance built at a constructor call site:
>>
>>   after() returning (Foo newInstance): call(Foo.new(..)) {
>>         System.out.println("New instance "+newInstance);
>>         System.out.println("From "+thisJoinPoint.getThis());
>>   }
>>
>> again, getThis will be null if the caller is a static context.
>>
>> Andy
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>>
> Quoted from:
> http://old.nabble.com/How-to-log-both-the-caller-object-and-the-executing-object-of-a-setter-pointcut-tp27896906p27898756.html
>
>


Back to the top