Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] [aspectj-dev] Need suggestion in adding a line of code inside a method through aspectJ

First off, Santosh, this is probably better asked on the user list (aspectj-users@xxxxxxxxxxx).  No big, though.

You can't really arbitrarily add a single line of code to a method, to my knowledge, via aspectj.  You can, however, cause code to be executed before and/or after a method.  Your intent will dictate which kind of advice you need.  I recommend using the least powerful form of advice that satisfies your need.

You have three choices of advice:  before, after & around.  After advice comes in three flavors, corresponding to try/catch/finally:  after returning, after throwing, and after (meaning "after finally").

Before advice executes, naturally before the joinpoint picked out by your pointcut executes.  You can only prevent execution of the joinpoint by having your before advice throw an exception.

Around advice, the most powerful, executes before the joinpoint, and allows you to determine whether the joinpoint is allowed to execute.  If it should, simply call the aspectj builtin method proceed() (which has arguments that match that of your advice).  Remember to capture the return value from the joinpoint so that you can either return it, change it, or ignore it, returning something else.

After returning advice only executes after the normal return of the joinpoint.  If the joinpoint throws, after returning advice will not execute.

After throwing advice only executes after the joinpoint throws an exception.  If the joinpoint returns normally, after throwing advice will not execute.

Plain after advice means "after finally", and executes whether or not the joinpoint threw an exception.

In your case, you could effectively "add a line of code" via before advice, any type of after advice.  Using around advice would allow you to "add a line of code" both before and after the method, if that's what you intended.

Also, more is available if you RTFM at http://eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html#advice

HTH,
Matthew


On Thu, Oct 24, 2013 at 1:54 AM, santosh kumar <santosh.2408@xxxxxxxxxxx> wrote:
Hi Everybody

I am new to this mailing list.

Actually i have started using aspectJ very recently in my project and i am finding it very cool way of programming.
I have one query of which i am trying to find a solution so need your suggestions.

My business case is very simple :

1. I have a method say showUserDetails()  in various java files of my project , i want to add a single line of code in this method through aspectJ. I dont want that i should go in each java file and add manually everywhere in this method.

2. I thought about aspectj (I hope my smart thought for this case :) ).

3. I have made pointcut to point to implementation of all these java methods.

4. i am not sure that how i can achieve this....Should i use around advice or what........

Can you guys please let me know the way to do it.

Thanks a TON in advance for your support.

Regards
Santosh

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




--
mailto:matthew@xxxxxxxxxxxxxxx 
skype:matthewadams12
googletalk:matthew@xxxxxxxxxxxxxxx
http://matthewadams.me
http://www.linkedin.com/in/matthewadams

Back to the top