Capturing multiple return values from recursive calls

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

Capturing multiple return values from recursive calls

by Dinkar Rao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Folks,

Need some help with AspectJ. How can I capture return values from recursive calls ? I want not only the returned value from the top level method call in the stack, but also link the values from the intermediate method calls. For example, given this simple method:

    public int fact(int val) {
        if (val > 0) {
            return val * fact(val - 1);
        }
        else {
            return 1;
        }
    }

I can write an aspect that gets me the returned values from each call.

    public pointcut FactCall() :
        call(public int fact(int));
   
    after() returning(int f) :
        FactCall() {
            System.out.println("returned = " + f);
        }

 So for fact(4), I get the returned values from the top-level call as 1, 1, 2, 6, 24.

But what I really want to do is to capture the return values from the top 2 (or top n) recursive method calls, i.e. something that links

1 => 1
1 => 2
2 => 6
6 => 24

Any suggestions on how to do this ?

Thanks,
Dinkar

RE: Capturing multiple return values from recursive calls

by jeanlouis.pasturel-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


What happens with an execution pointcut instead of a call pointcut ?
 
 
Jean-Louis Pasturel

-----Message d'origine-----
De : aspectj-users-bounces@...
[mailto:aspectj-users-bounces@...] De la part de Dinkar Rao
Envoyé : mercredi 2 septembre 2009 10:21
À : aspectj-users@...
Objet : [aspectj-users] Capturing multiple return values from recursive
calls


Hi Folks,

Need some help with AspectJ. How can I capture return values from recursive
calls ? I want not only the returned value from the top level method call in
the stack, but also link the values from the intermediate method calls. For
example, given this simple method:

    public int fact(int val) {
        if (val > 0) {
            return val * fact(val - 1);
        }
        else {
            return 1;
        }
    }

I can write an aspect that gets me the returned values from each call.

    public pointcut FactCall() :
        call(public int fact(int));
   
    after() returning(int f) :
        FactCall() {
            System.out.println("returned = " + f);
        }

 So for fact(4), I get the returned values from the top-level call as 1, 1,
2, 6, 24.

But what I really want to do is to capture the return values from the top 2
(or top n) recursive method calls, i.e. something that links

1 => 1
1 => 2
2 => 6
6 => 24

Any suggestions on how to do this ?

Thanks,
Dinkar

--
View this message in context:
http://www.nabble.com/Capturing-multiple-return-values-from-recursive-calls-
tp25253444p25253444.html
Sent from the AspectJ - users mailing list archive at Nabble.com.

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



*********************************
This message and any attachments (the "message") are confidential and intended solely for the addressees.
Any unauthorised use or dissemination is prohibited.
Messages are susceptible to alteration.
France Telecom Group shall not be liable for the message if altered, changed or falsified.
If you are not the intended addressee of this message, please cancel it immediately and inform the sender.
********************************

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

Re: Capturing multiple return values from recursive calls

by Andrew Eisenberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The simplest way would be to attach state to your aspect so that it can keep track of the last n values.  You also may want to attach a percflow instantiation clause to the aspect to make sure that each initial call to fact() has its own aspect instance.

So, something like this:

aspect A percflow(topLevelFactCall()) {

  pointcut topLevelFactCall() : factCall() && !cflowbelow(factCall());

  ...

  Stack<Integer> returnValues;

  ...



And just push a new value onto the stack until you get as many return values as you require.


On Wed, Sep 2, 2009 at 1:20 AM, Dinkar Rao <dinkar.d91411118@...> wrote:

Hi Folks,

Need some help with AspectJ. How can I capture return values from recursive
calls ? I want not only the returned value from the top level method call in
the stack, but also link the values from the intermediate method calls. For
example, given this simple method:

   public int fact(int val) {
       if (val > 0) {
           return val * fact(val - 1);
       }
       else {
           return 1;
       }
   }

I can write an aspect that gets me the returned values from each call.

   public pointcut FactCall() :
       call(public int fact(int));

   after() returning(int f) :
       FactCall() {
           System.out.println("returned = " + f);
       }

 So for fact(4), I get the returned values from the top-level call as 1, 1,
2, 6, 24.

But what I really want to do is to capture the return values from the top 2
(or top n) recursive method calls, i.e. something that links

1 => 1
1 => 2
2 => 6
6 => 24

Any suggestions on how to do this ?

Thanks,
Dinkar

--
View this message in context: http://www.nabble.com/Capturing-multiple-return-values-from-recursive-calls-tp25253444p25253444.html
Sent from the AspectJ - users mailing list archive at Nabble.com.

_______________________________________________
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: Capturing multiple return values from recursive calls

by Dinkar Rao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jean-Louis,

With an execution pointcut, the output is the same as for a call pointcut.

Still, I don't follow the significance. Execution or call, I need to
be able to (somehow) expose return values from the top 2 recursive
calls on the stack, in the invocation of some advice.

Thanks,
Dinkar


On Wed, Sep 2, 2009 at 1:55 AM, <jeanlouis.pasturel@...> wrote:

>
> What happens with an execution pointcut instead of a call pointcut ?
>
>
> Jean-Louis Pasturel
>
> -----Message d'origine-----
> De : aspectj-users-bounces@...
> [mailto:aspectj-users-bounces@...] De la part de Dinkar Rao
> Envoyé : mercredi 2 septembre 2009 10:21
> À : aspectj-users@...
> Objet : [aspectj-users] Capturing multiple return values from recursive
> calls
>
>
> Hi Folks,
>
> Need some help with AspectJ. How can I capture return values from recursive
> calls ? I want not only the returned value from the top level method call in
> the stack, but also link the values from the intermediate method calls. For
> example, given this simple method:
>
>    public int fact(int val) {
>        if (val > 0) {
>            return val * fact(val - 1);
>        }
>        else {
>            return 1;
>        }
>    }
>
> I can write an aspect that gets me the returned values from each call.
>
>    public pointcut FactCall() :
>        call(public int fact(int));
>
>    after() returning(int f) :
>        FactCall() {
>            System.out.println("returned = " + f);
>        }
>
>  So for fact(4), I get the returned values from the top-level call as 1, 1,
> 2, 6, 24.
>
> But what I really want to do is to capture the return values from the top 2
> (or top n) recursive method calls, i.e. something that links
>
> 1 => 1
> 1 => 2
> 2 => 6
> 6 => 24
>
> Any suggestions on how to do this ?
>
> Thanks,
> Dinkar
>
> --
> View this message in context:
> http://www.nabble.com/Capturing-multiple-return-values-from-recursive-calls-
> tp25253444p25253444.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@...
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>
> *********************************
> This message and any attachments (the "message") are confidential and intended solely for the addressees.
> Any unauthorised use or dissemination is prohibited.
> Messages are susceptible to alteration.
> France Telecom Group shall not be liable for the message if altered, changed or falsified.
> If you are not the intended addressee of this message, please cancel it immediately and inform the sender.
> ********************************
>
> _______________________________________________
> 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: Capturing multiple return values from recursive calls

by Dinkar Rao :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Andrew. I was hoping to avoid using external data structures,
because in my real-world application, the recursive call is a create()
method, which creates a "department", which in turn creates each
"employee" of the department, which in turn creates each "machine" of
employee, etc, and I want to keep track of who created whom. So the
object graph is quite large, and I don't need all of that information
in memory at a time.

I'll tweak your pointcut code and its advice to pare down the
reference stack/tree.

Thanks,
Dinkar

On Wed, Sep 2, 2009 at 8:55 AM, Andrew Eisenberg<andrew@...> wrote:

> The simplest way would be to attach state to your aspect so that it can keep
> track of the last n values.  You also may want to attach a percflow
> instantiation clause to the aspect to make sure that each initial call to
> fact() has its own aspect instance.
>
> So, something like this:
>
> aspect A percflow(topLevelFactCall()) {
>
>   pointcut topLevelFactCall() : factCall() && !cflowbelow(factCall());
>
>   ...
>
>   Stack<Integer> returnValues;
>
>   ...
>
> }
>
> And just push a new value onto the stack until you get as many return values
> as you require.
>
>
> On Wed, Sep 2, 2009 at 1:20 AM, Dinkar Rao <dinkar.d91411118@...>
> wrote:
>>
>> Hi Folks,
>>
>> Need some help with AspectJ. How can I capture return values from
>> recursive
>> calls ? I want not only the returned value from the top level method call
>> in
>> the stack, but also link the values from the intermediate method calls.
>> For
>> example, given this simple method:
>>
>>    public int fact(int val) {
>>        if (val > 0) {
>>            return val * fact(val - 1);
>>        }
>>        else {
>>            return 1;
>>        }
>>    }
>>
>> I can write an aspect that gets me the returned values from each call.
>>
>>    public pointcut FactCall() :
>>        call(public int fact(int));
>>
>>    after() returning(int f) :
>>        FactCall() {
>>            System.out.println("returned = " + f);
>>        }
>>
>>  So for fact(4), I get the returned values from the top-level call as 1,
>> 1,
>> 2, 6, 24.
>>
>> But what I really want to do is to capture the return values from the top
>> 2
>> (or top n) recursive method calls, i.e. something that links
>>
>> 1 => 1
>> 1 => 2
>> 2 => 6
>> 6 => 24
>>
>> Any suggestions on how to do this ?
>>
>> Thanks,
>> Dinkar
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Capturing-multiple-return-values-from-recursive-calls-tp25253444p25253444.html
>> Sent from the AspectJ - users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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: Capturing multiple return values from recursive calls

by Andrew Eisenberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Then, you can perhaps use around advice to cut short the recursion after you've gone down the required number of times.

On Wed, Sep 2, 2009 at 9:11 AM, Dinkar Rao <dinkar.d91411118@...> wrote:
Thanks Andrew. I was hoping to avoid using external data structures,
because in my real-world application, the recursive call is a create()
method, which creates a "department", which in turn creates each
"employee" of the department, which in turn creates each "machine" of
employee, etc, and I want to keep track of who created whom. So the
object graph is quite large, and I don't need all of that information
in memory at a time.

I'll tweak your pointcut code and its advice to pare down the
reference stack/tree.

Thanks,
Dinkar

On Wed, Sep 2, 2009 at 8:55 AM, Andrew Eisenberg<andrew@...> wrote:
> The simplest way would be to attach state to your aspect so that it can keep
> track of the last n values.  You also may want to attach a percflow
> instantiation clause to the aspect to make sure that each initial call to
> fact() has its own aspect instance.
>
> So, something like this:
>
> aspect A percflow(topLevelFactCall()) {
>
>   pointcut topLevelFactCall() : factCall() && !cflowbelow(factCall());
>
>   ...
>
>   Stack<Integer> returnValues;
>
>   ...
>
> }
>
> And just push a new value onto the stack until you get as many return values
> as you require.
>
>
> On Wed, Sep 2, 2009 at 1:20 AM, Dinkar Rao <dinkar.d91411118@...>
> wrote:
>>
>> Hi Folks,
>>
>> Need some help with AspectJ. How can I capture return values from
>> recursive
>> calls ? I want not only the returned value from the top level method call
>> in
>> the stack, but also link the values from the intermediate method calls.
>> For
>> example, given this simple method:
>>
>>    public int fact(int val) {
>>        if (val > 0) {
>>            return val * fact(val - 1);
>>        }
>>        else {
>>            return 1;
>>        }
>>    }
>>
>> I can write an aspect that gets me the returned values from each call.
>>
>>    public pointcut FactCall() :
>>        call(public int fact(int));
>>
>>    after() returning(int f) :
>>        FactCall() {
>>            System.out.println("returned = " + f);
>>        }
>>
>>  So for fact(4), I get the returned values from the top-level call as 1,
>> 1,
>> 2, 6, 24.
>>
>> But what I really want to do is to capture the return values from the top
>> 2
>> (or top n) recursive method calls, i.e. something that links
>>
>> 1 => 1
>> 1 => 2
>> 2 => 6
>> 6 => 24
>>
>> Any suggestions on how to do this ?
>>
>> Thanks,
>> Dinkar
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Capturing-multiple-return-values-from-recursive-calls-tp25253444p25253444.html
>> Sent from the AspectJ - users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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