[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RES: [aspectj-users] Capturing setting inside List

Thank you very mutch Matthew! 
Your tip works very well :-)


-----Mensagem original-----
De: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] Em nome de Matthew Webster
Enviada em: sexta-feira, 15 de abril de 2005 07:24
Para: aspectj-users@xxxxxxxxxxx
Assunto: Re: [aspectj-users] Capturing setting inside List





André,

This is a classic example of the GoF Observer pattern which can easily be
applied using an aspect (I have modified you example to make it compile):

public aspect ObserverPattern {

      private interface Subject {
            public void addObserver (Observer o);
            public void notifyObservers();
      }

      private interface Observer {
            public void update ();
      }

      /*
       * Make B the subject of the Observer pattern
       */
      declare parents : B implements Subject;

      private Set Subject.observers = new HashSet();

      public void Subject.addObserver (Observer o) {
            observers.add(o);
      }

      public void Subject.notifyObservers() {
            for (Iterator i = observers.iterator();i.hasNext();) {
                  Observer o = (Observer)i.next();
                  o.update();
            }
      }

      pointcut addObserver (Observer o, Subject s) :
            execution(void A.add(..)) && this(o) && args(s);

      after (Observer o, Subject s) returning : addObserver(o, s) {
            s.addObserver(o);
      }

      pointcut modified (Subject s) :
            execution(* set*(..)) && this(s);

      after (Subject s) returning : modified (s) {
            s.notifyObservers();
      }

      /*
       * Make A the observer
       */
      declare parents : A implements Observer;

      public void A.update () {
            A.serialize(this);
      }
}


public class A implements Serializable {
        List list = new ArrayList();
        public void add(Object o) {
          list.add(o);
        }

        public static void serialize (A a) {
              System.out.println("? serialize() a=" + a);
        }
}


public class B {
        int x;
        public void setX(int x) {
          this.x = x;
        }
      }


public class Test {
      public static void main(String[] args) {
              A a1 = new A();
              B b1 = new B();
              a1.add(b1);

              A a2 = new A();
              B b2 = new B();
              a2.add(b2);

              b1.setX(3);  // a1 affected
            }
}

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/

André Dantas Rocha <ad-rocha@xxxxxxxxxx>@eclipse.org on 14/04/2005 21:14:09

Please respond to aspectj-users@xxxxxxxxxxx

Sent by:    aspectj-users-bounces@xxxxxxxxxxx


To:    <aspectj-users@xxxxxxxxxxx>
cc:
Subject:    [aspectj-users] Capturing setting inside List


Suppose I have this classes...

public class A implement Serializable {
  List list = new ArrayList();
  public add(Object o) {
    list.add(o);
  }
}

public class B {
  int x;
  setX(int x) {
    this.x = x;
  }
}

... and a main method:

public void main(String[] args) {
  a1 = new A();
  b1 = new B();
  a1.add(b1);

  a2 = new A();
  b2 = new B();
  a2.add(b2);

  b1.setX(3);  // a1 affected
}

I would like to serialize the A object every time an element in his List is
modified (in this case, only a1 will be serialized).
Is it possible to intercep this pointcut using AspectJ?

Thanks,

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


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