3 Replies Latest reply on Sep 22, 2004 4:59 AM by nthx

    Annotate class only and crosscut methods within the class

      Hi all

      During weekend heavilly developed (what? later :) ) with JBossAOP.
      Question by the code:

      package my.demo;
      /** @@my.root */
      public class RootClass
      {
       public RootClass()
       {
       //constructor here
       //...
       }
      }
      
      jboss-aop.xml
      <bind pointcut="execution(my.demo.*->@my.root)">
       <interceptor class="RootCreationInterceptor" />
      </bind>
      


      I want to crosscut every constructor of class that has only been annotated with tag @@my.root for the class (not for the constructor).
      I don't want to annotate _every_ constructor inside class (which works with JBossAOP).

      I've also looked at tutorial at page about annotation introductions. It could also work for me, if I can _introduce_ annotation to constructors, by specifying only tag for the whole class. But annotation-introducionts seem not to work that way.
      Sth like that, doesn't work:
      jboss-aop.xml:
      <annotation-introduction expr="constructor(my.demo.*->new()) AND class(@my.root)">
       @my.root
      </annotation-introduction>
      

      Although, above doesn't make sense, I think.

      Are there any possibilities of solving my problem?
      Thanks, for help :)

      Tomasz

        • 1. Re: Annotate class only and crosscut methods within the clas

          Yupiii!

          And once more, I reply to my own post :)

          So here is the solution with new TYPE_DEF feature:

          jboss-aop.xml:

          <aop>
          <typedef name="rootable" expr="class(@root)" />
          
          <bind pointcut="all($typedef{rootable})">
           <interceptor class="RootInterceptor"/>
          </bind>
          
          <!-- If you annotated class with "@@root" you don't need this
          <annotation tag="root" class="MyRoot">
           <class/>
          </annotation>
          -->
          </aop>
          


          Interceptor:
          public class RootInterceptor
           implements Interceptor
          {
           public Object invoke(Invocation invocation) throws Throwable
           {
           if (invocation instanceof ConstructorInvocation)
           {
           ...put logic here for Constructor joinpoints..
           }
           ....
           }
          }
          


          and core code:
          /** @@root */
          public class MyRoot
          {
           public void method1(){}
           public static void main(String[] args)
           {
           System.out.println("------START");
           MyRoot root = new MyRoot();
           root.method1();
           System.out.println("------END");
           }
          }
          


          There is only default constructor here. But the logic from
          RootInterceptor will be applied. That's great.

          Output:
          [java] ------START
           [java] ==O==[main] RootInterceptor: 23: MyRoot0OptimizedConstructorInvocation@15e9756
           [java] ==O==[main] RootInterceptor: 25: I'm in ConstructorInterceptor
           [java] ==O==[main] RootInterceptor: 23: MyRoot_method1_N4778546224999628736_
          OptimizedMethodInvocation@121ab80
           [java] ------END
          


          --Tomasz



          • 2. Re: Annotate class only and crosscut methods within the clas
            bill.burke

            Yikes, sorry I missed your post before....

            This may be a better approach:

            <bind pointcut="execution(@my.root->new(..))">
             <interceptor class="RootCreationInterceptor" />
            </bind>
            


            This will bind your interceptor to every constructor of every class tagged as @my.root.

            Bill


            • 3. Re: Annotate class only and crosscut methods within the clas

              Hi

              And I was going to work today with a thought about posting about this simpler way that I found out yesterday evening... Ehhh :)

              Tomasz