6 Replies Latest reply on Feb 4, 2011 5:08 PM by loupi loupi

    The reuse&&JBoss AOP

    loupi loupi Newbie

      Hello,

      First, i really need someone to answer me, because I have many questions, but in this forum only a few people answer

       

      Question N=°1 how JBoss AOP defines an abstract aspect? and how it makes the reuse?(example in AspectJ we find Abstarct aspect)

       

      Question N=°2 how can we access to variable with the mixin mechanism?because I followed your example

       

      MixIN example

      package Fooo;

      public class Foo {

            public int fooField;

            public Foo () {

              fooField = 0;

            }

       

            public String fooMethod (int i) {

              return Integer.toString (fooField + i);

            }

          }

      package Fooo;

      public interface FooMixinInt {

            public String fooMethod2 (int i);

          }

      package Fooo;

      public class FooMixin implements FooMixinInt {

       

            public String fooMethod2 (int i) {

             return Integer.toString (fooField - i);

       

            }

          }

      package Fooo;

      public class Main {

       

          public static void main(String[] args) {

              // TODO Auto-generated method stub

              Foo foo = new Foo ();

              FooMixinInt fooint = (FooMixinInt) foo;

              String s = fooint.fooMethod2 (-2);

       

          }

       

      }

       

       

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <aop>
          <introduction class="Fooo.Foo">
              <mixin>
                  <interfaces>Fooo.FooMixinInt</interfaces>
                  <class>Fooo.FooMixin</class>
                  <construction>new Fooo.FooMixin()</construction>
              </mixin>
          </introduction>  
      </aop>
      

       

      and I often find this error:

       

      Exception in thread "main" java.lang.Error: Unresolved compilation problem:

          fooField cannot be resolved

       

          at Fooo.FooMixin.fooMethod2(FooMixin.java:6)

          at Fooo.Foo.Fooo$Foo$fooMethod2$aop(Foo.java)

          at Fooo.Foo$FooAdvisor.fooMethod24106554489332185313(Foo$FooAdvisor.java)

          at Fooo.Foo.fooMethod2(Foo.java)

          at Fooo.Main.main(Main.java:12)

       

       

      Thank you

        • 1. Re: The reuse&&JBoss AOP
          Flavia Rainone Master

          loupi loupi wrote:

           

          Question N=°1 how JBoss AOP defines an abstract aspect? and how it makes the reuse?(example in AspectJ we find Abstarct aspect)

           

          JBoss AOP does not define an abstract aspect, you can however write an abstract class that will be the superclass of your aspect,  or define a common interface to more than one aspects in your system. The reason for this is that JBoss AOP, differently from AspectJ, is not an AOP language, it is an AOP tool. It will weave code that has been previously compiled with javac.

           

           

          loupi loupi wrote:

           

          Question N=°2 how can we access to variable with the mixin mechanism?because I followed your example

          See the previous paragraph. As JBoss AOP is a Java tool, your code needs to compile first, using a plain Java compiler.

          Still, what you want to do can be achieved. There is a very good example in our dist, in the examples dir. The introductions example contains the POJO2ExternalizableMixin, that acquires access to the class it is applied to, POJO2, in its constructor. You would have to do the same, i.e., FooMixin will receive Foo in its constructor, initialize a private field with foo, and use that to get access to fooMethod2 value:

           

           

          public clas FooMixin implements FooInt {
          
               private Foo target;
          
               public FooMixin (Foo foo) {
                    target = foo;
               }
          
               public String fooMethod2 (int i) {
                    return Integer.toString (target.fooField - i); 
               }
          }
          

           

          Check out introductions example code to see the details (it is in the docs/aspect-framework/examples/introductions path of your JBoss AOP installation dir).

          • 2. The reuse&&JBoss AOP
            loupi loupi Newbie

            hello,
            thank you for your reply, my problem : I want to capture and test a variable value of a class1, and from this value, I must add a method to another class class2.
            the realization of the observer design pattern (Class1=Subject, Class2=Observer).

            Unfortunately, the 'if' statement does not exist in JBossAOP, and the mixin mechanism can not cover two classes at once!

            So, how can i do that??

             

            Thank you in advance

            • 3. The reuse&&JBoss AOP
              Flavia Rainone Master

              Can you explain better? Are you saying that you want to have something like a pointcut that checks the value of the variable of class1 before deciding if the mixin will apply to class 2?

               

              If that's not what you are trying to say, maybe a piece of code (even if it doesn't compile with JBoss AOP) showing what you want to do would help a lot!

               

              Anyway, what is the relationship of class1 and class2? We need someway of going from Class2 to Class1. It is ok if this requires using package protected methods and fields.

              • 4. The reuse&&JBoss AOP
                loupi loupi Newbie

                Hello,
                Thank you for your reply,  The example that I want to realise is the following(Realization of the observer design pattern with JBossAOP):

                Flower (Subject ):

                package Bee;

                public class Flower {

                 

                      private boolean isOpen;

                 

                      public boolean isOpen(){returnthis.isOpen;}

                     

                      public Flower(){

                            this.isOpen=false;

                                  }

                     

                      public void open(){

                            this.isOpen=true;         

                      }

                     

                      public void close(){

                            this.isOpen=false;

                      }

                }

                 

                The Bee class observe  the opening and closing of Flower (attribute(isOpen))

                 

                PS: for easier reuse of the Bee class, we put the Update method (observer role)in another class and we make an introduction

                 

                Bee (Observer ):

                package Bee;

                public class Bee {

                 

                private String name;

                     

                public Bee(String name){

                      this.name = name;

                }

                     

                public void dinner(){

                      System.out.println("Bee "+ name

                                   + "'s breakfast time!");

                      }

                     

                      public void rest(){

                            System.out.println("Bee" + name

                                    + "'s bed time!");           

                      }             

                }

                 

                The interface for all observers is the following:

                 

                package Bee ;

                public interfaceFlowerObserver {   

                      public void update();

                 

                }

                 

                 

                And Mixin class is as follows:

                 

                package Bee;

                 

                public class BeeObserver implements FlowerObserver{

                        

                        Bee bee;

                        

                         public BeeObserver(Bee bee){this.bee=bee;}

                         public void update(){

                              if (isOpen) //the attribute value of Flower

                               bee.dinner();

                                    

                              else  bee.rest();

                                    

                }}

                Finally the Main class:

                package Bee;

                 

                public class Main {

                public static voidmain(String[] args) {

                       

                      Flower f = new Flower();    

                      Beeb1 = new Bee("B1");

                      Beeb2 = new Bee("B2");

                     

                        f.open();

                        f.close();

                        f.open();

                        System.out.println("c'estbon");

                    }

                }

                 

                theXML file:


                <?xmlversion="1.0" encoding="UTF-8"?>

                <aop>

                  

                   <introductionclass="Bee.Bee">

                        <mixin>

                           <interfaces>Bee.FlowerObserver</interfaces>

                           <class>Bee.BeeObserver</class>

                            <construction>new Bee.BeeObserver(this)</construction>

                        </mixin>

                    </introduction> 

                 

                   <aspect class="Bee.BeeObserver"/>

                   <bind pointcut="set(private boolean Bee.Flower->isOpen)">

                   <after aspect="Bee.BeeObserver" name="update"/>

                   </bind>

                 

                </aop>

                This code contains several errors!! but I would have the following result:

                 

                B1 Bee's breakfast time!
                B2 Bee's breakfast time!
                B1 Bee's bed time!
                B2 Bee's bed time!
                B1 Bee's breakfast time!
                B2 Bee's breakfast time!

                 

                Tahnk's in advance

                • 5. Re: The reuse&&JBoss AOP
                  Flavia Rainone Master

                  In order to do that, first of all you need to change the signature of update method to something like this:

                   

                   

                  package Bee ;
                  public interface FlowerObserver {    
                        public void update(boolean isOpen);
                  }
                  

                   

                   

                  Now, your implementation must also receive the isOpen value as a parameter and that way you can check on whether the flower is open before deciding to either call Bee.dinner() or Bee.rest().

                   

                  Another thing that you have to workaround is how to call this. JBoss AOP needs to control the creation of aspects. AFAIK, this is not a limiting factor exclusive to JBoss AOP, all AOP languages/tools need to have some control of how to create the aspect. So, you can't have as an aspect an  object you created yourself (i.e, it can't be the bees you are creating in main).

                   

                  To workaround that:


                  - keep the interface as above

                  - add an Observable interface and a Mixin for that:

                  public interface Observable {
                      public void addObserver(FlowerObserver observer);
                      public void notifyAll();
                  }
                  
                  public ObservableMixin {
                      private Collection<FlowerObserver> observers = ...;
                      private final Flower flower;
                      public ObservableMixin(Flower flower) { this.flower = flower}
                      public void addObserver(FlowerObserver observer) { observers.ad(observer);}
                      public void notifyAll() { for(FlowerObserver: observers) { observer.update(flower.isOpen());}}
                  }
                  
                  

                   

                  - and finally an after aspect that triggers the notification (use just the same xml you used, but replace BeeObserver update() by the ObserverAspect updateObservers advice below):

                   

                  public class ObserverAspect() {
                        public void updateObservers(@Target Observable observable) { observable.notifyAll(); }
                  }
                  

                   

                  - don't forget to add the ObservableMixin above to Flower class!

                   

                  Now, in main, you can make:

                   

                  ((Observable) flower).addObserver((FlowerObserver) bee1);
                  ((Observable) flower).addObserver((FlowerObserver) bee2);
                  

                   

                  And that's it!

                  • 6. Re: The reuse&&JBoss AOP
                    loupi loupi Newbie

                    hello Flavia,
                    Thank you very much for your help
                    In my work I have to implement the 23 GOF design patterns with AspectJ&JBoss AOP&CaeserJ!!!!. So I'am sure,i will need your help later because you are the only expert that I know in JBoss AOP

                     

                    Good luck in your search