Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] adding local variables using aspect J

Hi,

You cannot directly inject local variables.  But there are a few
options for maintaining state, and which is appropriate will depend on
where you need that state to be visible from.

There is around advice:

void around(): execution(* Foo.someMethod(..)) {
  long starttimer=System.currentTimeMillis();
  proceed(); // run what was going to run anyway
  long endtimer = System.currentTimeMillis();
  long duration = (endtimer-starttimer);
  System.out.println("someMethod took "+duration+"ms");
}

There are different instantiation models for aspects (perthis/percflow):

(stolen from an old post by Simone):

public aspect NotifyPersonOfChanges perthis(setter()) {

    private String mailtext = "There has been some changes:\n";

    pointcut setter() : execution(* Person.set*(..));

    after() returning : setter() {
       mailtext += "The field .... has changed with ... ";
    }
}

(I don't really want to go into a description of those, but they are
covered in the docs - basically they are about having aspect instances
per-something in your program - so they can maintain state).

> Also, how can I add members to classes if
> I don't know their names in advance (while intercepting them during load
> time). Please help. Thanks in advance

Not sure what you want to do here...  If you use ITDs to add a field
or method, then if you are accessing them from another AspectJ class
they will have the names you used when you specified them.  If you are
doing reflection they do have reliable names - but I wouldn't
recommend that unless using public ITDs.  Or you can use the AjType
system which is the AspectJ version of the Java reflection system,
which is ITD aware.

Andy


On 14 March 2010 03:18, Ashank <k_arvind_shankar@xxxxxxxxx> wrote:
>
> Hello All,
>
> I am very new to AOP and aspectj. I have a little background in bytecode
> instrumentation where I am freely able to intercept classes during load time
> and add some local variables to selected methods to maintain my
> instrumentation state during method execution. I know there is something
> called inter type declaration in aspectj using which I can declare
> fields/methods of classes that I know the names of. I really need to be able
> to maintain some working state inside selected methods using local variables
> (so that I don't have to worry about threads overwriting each others'
> state). Can I accomplish this using aspectJ and if so, how? This would be
> very helpful. I searched the forums and the net and there is surprisingly
> less discussion around this topic. Also, how can I add members to classes if
> I don't know their names in advance (while intercepting them during load
> time). Please help. Thanks in advance
>
> -Arvind
> --
> View this message in context: http://old.nabble.com/adding-local-variables-using-aspect-J-tp27893938p27893938.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top