4 Replies Latest reply on Sep 1, 2005 6:43 PM by Atle Atle

    Inheritance and AOP

    Atle Atle Newbie

      Is it possible to create a class A, with annotations binding methods to pointcuts, and have a class B extends A with the aspect annotation?

        • 1. Re: Inheritance and AOP
          Bill Burke Master

          can you expand on what you want with pseudo code? I want to make sure I answer your question correctly.

          • 2. Re: Inheritance and AOP
            Atle Atle Newbie

            I have a system running on JBoss, handling incoming messages (Message Driven Beans). The messages alter the state of the system. I have a set of rules that check the state, and do actions if neccessary. I want to expand the set of rules at runtime, and it seems to me that AOP is not only fit for the job, but makes it very easy to do so. (I hope)

            My idea is to define the method that executes the transaction as a pointcut, and as time goes by, add more and more rules by binding advices to that pointcut. To ease the job, i would like to have s base implementation, and just subclass every time i want to add a new rule.

            In my message manager i have a method called when the state is altered

            public class MsgManager{
            ...
            public void afterTransaction(Context ctx){} <- the pointcut, called after each message.
            ...
            
            }
            


            My base Aspect would be somthing like this:
            
            public class BaseRule{
            
             @Bind (pointcut="execution(public void MsgManager->afterTransaction(Context))")
             public Object advice(MethodInvocation invocation) throws Throwable{
             Context ctx = getContext( invocation );
             examine( ctx );
             }
            
             private Context getContext(MethodInvocation inv){...}
            
             protected abstract void examine(Context ctx);
            
            }
            


            To make new rules i subclass the base rule, which already contains the AOP code.
            @Aspect
            public class Rule1 extends BaseRule{
            
             protected void examine(Context ctx){
             ...
             //examine the context and do some action
             ...
             }
            }
            


            If i need to extends my (running) system, i just bundle the new rules in an .aop jar and deploy them.
            Would this work?

            • 3. Re: Inheritance and AOP
              Kabir Khan Master

              It will only look for @Bind annotations within classes annotated with @Aspect or @InterceptorDef. Also, aspect classes cannot be abstract.

              You may be better off using xml, and you can deploy as many -aop.xml files you like.

              Something like the following should work

              <aop>
               <aspect name="aspect1" class="BaseAspect">
               <attribute name="rule">MyRule</attribute>
               </aspect>
               <aspect name="aspect2" class="BaseAspect">
               <attribute name="rule">OtherRule</attribute>
               </aspect>
              
               <bind pointcut="execution(public void MsgManager->afterTransaction(Context))"/>
               <advice aspect="aspect1" name="advice"/>
               </bind>
               <bind pointcut="execution(public void MsgManager->afterTransaction(Context))"/>
               <advice aspect="aspect2" name="advice"/>
               </bind>
              
              </aop>
              


              As long as you name your aspects you can have as many of a given aspect class as you like

              
              public class BaseAspect{
               Rule rule;
               public setRule(java.lang.Class clazz)
               {
               rule = (Rule)clazz.newInstance();
               }
              
               public Object advice(MethodInvocation invocation) throws Throwable{
               Context ctx = getContext( invocation );
               rule.examine( ctx );
               }
              
               private Context getContext(MethodInvocation inv){...}
              }
              
              
              public class MyRule implements Rule
              {
               public void examine(Context ctx)
               {
               ...
               }
              }
              
              



              • 4. Excellent!
                Atle Atle Newbie

                Thank you, your approach make things even easier!

                -Atle