Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] Inner aspects

Hi Matthew and Wes,

Thanks for your interest in this feature. Here's are some better examples
to show what I mean about inner aspects being different than a nested
aspect:

void breadthFirstTraverse(Vertex v) {
  private boolean Vertex.visited = false; // inner-introduction, must be
                                          // private
  Queue<Vertex> q = new Queue<Vertex>();
  q.enqueue(v);
  while (q.hasElements()) {
    Vertex x = q.dequeue();
    for (Vertex y: x.adjacent()) {
      if (y.visited == false) {
        y.visited = true;
        q.enqueue(y);
        found(x, y);
      }
    }
  }
}

Here, the code uses y.visited just like it was a true field, but the field
only exists when breadthFirstTraverse is executing. That is, the class
itself is not holding a space for the field to be stored. While an
external data-structure could be used to hold "visited" values instead
based on a hashtable (and, in fact that's how I see this would be
implemented) that code can be error prone and lacks the idea that
"visited" really is a member of the class, but only when the
breadthFirstTraverse method is executing. By allowing inner aspects you
get these kinds of features for free.

Here's an example using advice:

void countSets() {
  int numSets = 0;
  double acc = 0.0;

  pointcut setOccured(double rhs): set(double *.*) && args(rhs);
  after(double newVal): setOccured(newVal) {
    acc += newVal;
    ++numSets;
  }
  someObject.someMethod();
  anotherObject.anotherMethod();

  System.out.println("Number of sets=" + numSets + ", acc=" + acc);
}

Here, not only can the advice access the local variables "acc" and
"numSets", the advice is also only executing when countSets is called. I
think the data-structures needed to implement this one would be even
trickier for the coder (but not the compiler!).

Note that when introduced inner-methods are used, they would act like true
sub-routines and be able to access local variables in the scope. It is
accessing local variables that adds the real value.

Thanks,
Macneil


On Mon, 12 Jan 2004, Matthew Webster wrote:
> Macneil,
>
> I assume the reason for this discussion is the desire to create fields
> whose visibility is restricted to the methods that need them. This can be
> done today as inner aspects are supported however you must use an
> inter-type declaration to add the method as well as the field it is
> accessing. Moving to a code branch level is not possible as no such join
> point is supported by AspectJ.
>
> import java.util.Random;
>
> public class MyClass {
>
>       private static aspect InnerAspect1 {
>
>             private static Random MyClass.rgen = new Random();
>
>             static int MyClass.rand(int n, int m) {
>                   return n + Math.abs(rgen.nextInt()) % (m - n + 1);
>             }
>       }
>
>       private static aspect InnerAspect2 {
>
>             private boolean MyClass.flag = false;   // only the f
> member-function uses the
>
>             void MyClass.f() {
>                   if (flag) {
>                   }
>             }
>       }
>
>       public static void main (String[] args) {
>             rand(1,2);
>             new MyClass().f();
>       }
> }
>
> Matthew Webster
> AOSD Project
> Java Technology Centre, MP146
> IBM Hursley Park, Winchester,  SO21 2JN, England
> Telephone: +44 196 2816139 (external) 246139 (internal)
> Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
> http://w3.hursley.ibm.com/~websterm/
>
> _______________________________________________
> aspectj-dev mailing list
> aspectj-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-dev
>


Back to the top