4 Replies Latest reply on Apr 14, 2008 10:52 AM by flavia.rainone

    Are names supposed to be unique in JBoss AOP?

    danamin

      When closely studying the creation of interceptors, there is a strange thing going on: each time an interceptor, aspect or advice is deployed, a new one is made, even if there already is an identical one in the system.

      In the MethodExecution example

      <aop>
       <bind pointcut="execution(public void POJO->noop())">
       <interceptor class="SimpleInterceptor"/>
       </bind>
      
       <bind pointcut="execution(* POJO->*(int))">
       <interceptor class="MethodInterceptor"/>
       </bind>
      
       <bind pointcut="execution(static * POJO->*(..))">
       <interceptor class="MethodInterceptor"/>
       </bind>
      
       <bind pointcut="execution(* POJO$Bar->*(..))">
       <interceptor class="MethodInterceptor"/>
       </bind>
      
      </aop>
      


      One would expect there are two aspects with one Interceptor/advice each:
      SimpleInterceptor with an invoke advice and
      MethodInterceptor with an invoke advice

      This example creates 4 aspects with one advice each:
      1 SimpleInterceptor with an invoke advice and
      3 MethodInterceptors with an invoke advice

      This could of course be considered a feature. If it is considered a feature, we loose uniqueness of name for aspects and interceptors.

      The only unpleasant side effect is that the interceptorFactories and aspectDefinitions fields in the AspectManager are no longer consistent, because they use name as their key.

      I don't have a full view of the consequences of this, but I can list a few

      • debugging information is inconsistent
      • when the second aspect with the same name is deployed, the first one is undeployed, this removes it from all advisors, but it's bindings (and interceptors) remain active, the bindings will continue to create instances of the aspect, which can no longer be centrally undeployed
      • bindings silently replace each other, correctly as far as I can see


        • 1. Re: Are names supposed to be unique in JBoss AOP?
          flavia.rainone

          Hi!

          It is expected that JBoss AOP would create four instances of aspects instead of two.
          This is true because an interceptor tag declares a new interceptor to JBoss AOP. Whether you have more than one interceptor declaration with the same class is not relevant. Look at the example below:

          <bind pointcut="execution(static * POJO->*(..))">
           <interceptor class="MethodInterceptor name="interceptor1"/>
          </bind>
          
          <bind pointcut="execution(* POJO$Bar->*(..))">
           <interceptor class="MethodInterceptor" name="interceptor2"/>
          </bind>


          In the xml tags above two different interceptors, with the same class, are declared. Their names are "interceptor1" and "interceptor2". If their names are not defined, JBoss AOP creates a name for those interceptors.

          So, if you want to use a single interceptor, i.e., to have only one aspect definition generated to represent your interceptor, you should do something like this:
          <interceptor class="MethodInterceptor name="interceptor1"/>
          
          <bind pointcut="execution(static * POJO->*(..))">
           <interceptor-ref name="interceptor1"/>
          </bind>
          
          <bind pointcut="execution(* POJO$Bar->*(..))">
           <interceptor-ref name="interceptor1"/>
          </bind>


          However, it looks like JBoss AOP does create two interceptors with the same name when the name attributes are not defined. I will do some investigation before answering this part of your question.


          • 2. Re: Are names supposed to be unique in JBoss AOP?
            flavia.rainone

            Hi again,

            I have taken a deeper look at the code, and now I see that JBoss AOP does not generate another aspect class to represent the same interceptor. When the attribute name is ommitted, JBoss AOP names the AspectDefinition object after the class of the interceptor. I.e., the declaration below:

            <bind pointcut="execution(* POJO$Bar->*(..))">
             <interceptor class="MethodInterceptor"/>
            </bind>


            Will generate an AspectDefinition named "MethodInterceptor". When another binding with the same interceptor tag is found (and with the name attribute ommitted again), JBoss AOP looks for an AspectDefinition named "MethodInterceptor" instead of creating another AspectDefinition.

            So, in the example you gave, JBoss AOP creates an AspectDefinition to represent SimpleInterceptor when it finds the first binding. When it finds the second one, it creates an AspectDefinition named MethodInterceptor. In the next bindings, it realizes it already has an AspectDefinition named MethodInterceptor and reuses this definition instead of creating extra AspectDefinitions.

            • 3. Re: Are names supposed to be unique in JBoss AOP?
              danamin

              Ah yes, correct,

              It is indeed

              1 SimpleInterceptor with an invoke advice and
              1 MethodInterceptor with 3 invoke advices

              So the issue with interceptors remains

              Wouter

              • 4. Re: Are names supposed to be unique in JBoss AOP?
                flavia.rainone

                Wouter

                You are right. I have created a Jira ssue to fix this bug:

                http://jira.jboss.com/jira/browse/JBAOP-554

                Thanks for pointing this out.