Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: [NEWSDELIVER] Cannot get the inserted element for Collection field


SunnyDay,

The get() pointcut allows you to intercept field references. In you example this will happen before the object is added. If you want to advise the program immediately after an object is added to a Collection then you should use something like this:

public aspect Aspect {
        pointcut fieldGet() : get(* *);

        pointcut addObject (Collection field) :
                call(public * add(Object)) && target(field);
       
//        after() returning(Object field): fieldGet() {
        after(Object field) returning : addObject(field) {
                  if (field instanceof Collection)
                  {
                                          Collection collField = (Collection)field;
                                          Iterator it = collField.iterator();
                                          while (it.hasNext())
                                          {
                                                           System.out.println("after " + it.next());
                                          }
                  }
                                                           
        }
}

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/

To:        undisclosed-recipients:;
cc:        
Subject:        [NEWSDELIVER] Cannot get the inserted element for Collection field



Hi,

I am trying to get the new contents for a field of Collection type, such
as java.util.Vector, after an element is added or deleted each time. Here
is what I did:

pointcut fieldGet() : get(* *);

after() returning(Object field): fieldGet() {
         if (field instanceof Collection)
         {
                                 Collection collField = (Collection)field;
                                 Iterator it = collField.iterator();
                                 while (it.hasNext())
                                 {
                                                  System.out.println("after " + it.next());
                                 }
         }
                                                 
}

Matched code:

public class A
{
                Vector v = new Vector();
                public void addB()
                {
                                 v.add(new B());
                                 // v.add(new B());
                }
}

I had expected to get object B's reference after the first v.add(new B());
but there is nothing returned from the collection. But if I add another
v.add(new B());, I can only see one B's instance from the collection. How
may I get the most up-to-date contents of the Vector v after it is
accessed? Thanks very much for any help!

SunnyDay





Back to the top