Bug 67592 - value in the args[] array of thisjoinpoint can be changed by one advice decl and updated values seen in another advice decl
Summary: value in the args[] array of thisjoinpoint can be changed by one advice decl ...
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 1.2.1   Edit
Assignee: Adrian Colyer CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-06-17 04:06 EDT by Laurie Hendren CLA
Modified: 2004-10-21 04:32 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Laurie Hendren CLA 2004-06-17 04:06:53 EDT
In ajc a use of thisJoinPoint corresponds to an object of type JoinPoint, where
a JoinPoint object has four components, which can be retrieved using
accessor methods.

  this                   getThis()
  target                 getTarget()
  args                   getArgs()
  staticJoinPoint        getStaticPart()

In the case of args, the object returned is an Object array.  This means
that an advice decl can overwrite the args,  and then another advice which
shares the same JoinPoint will see the changed args.   Here is a
small example.  In this aspect the
before advice changes arg[0] to always be "Laurie" and then the after
advice will always say that "Laurie won a million pounds".

Is this the behaviour that is intended?   I would have thought that the
interface to args[] should be such that the values can only be read.  Otherwise
very strange interactions between advice decls could occur.

---------------------------------------------------------------------

public class Main {

  public static Main v = new Main();

  void lottery ( String arg )
    { System.out.println("The arg in lottery is " + arg);
    }

  public static void main (String args[])
    { v.lottery("Oege");
      v.lottery("Ganesh");
    }

}

---------------------------------------------------------------------

public aspect Aspect {

  // This pair of advice decls demonstrates that an advice can change to
  //   the args of a join point.

  before () : call(* lottery(java.lang.String)) && !within(Aspect)
    { System.out.println("BEFORE " + thisJoinPoint +
                         " at " + thisJoinPointStaticPart.getSourceLocation());
      Object args[] = thisJoinPoint.getArgs();
      System.out.println("arg[0] is " + args[0]);
      args[0] = "Laurie";
      System.out.println("... now it is " + thisJoinPoint.getArgs()[0]);
    }


  after ()  : call(* lottery(java.lang.String)) && !within(Aspect)
    { System.out.println("AFTER " + thisJoinPoint +
                         " at " + thisJoinPointStaticPart.getSourceLocation());
      if (thisJoinPoint.getArgs()[0].equals("Laurie"))
        System.out.println("Laurie wins 1 million pounds!");
    }

}
Comment 1 Adrian Colyer CLA 2004-08-06 08:45:58 EDT
will fix this on Monday... simple matter of taking a defensive copy of the args before handing them out.
Comment 2 Adrian Colyer CLA 2004-08-09 09:03:12 EDT
getArgs() now returns an Object[] with the same behaviour as arguments in a 
method or in advice parameters: any change to an object reference in the array 
is not visible outside of the advice body, but any change to the state of an 
argument (calling a method on a referenced arg object) is visible.

Will close the defect once this fix is available in a published build.
Comment 3 Adrian Colyer CLA 2004-08-09 15:13:15 EDT
fix now available in latest jar published from the Aspectj download page.
Comment 4 Adrian Colyer CLA 2004-10-21 04:32:56 EDT
Fix released as part of AspectJ 1.2.1