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

    Annotate class only and crosscut methods within the class

    Tomasz Nazar Newbie

      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
      <bind pointcut="execution(my.demo.*->@my.root)">
       <interceptor class="RootCreationInterceptor" />

      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:
      <annotation-introduction expr="constructor(my.demo.*->new()) AND class(@my.root)">

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

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


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


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

          So here is the solution with new TYPE_DEF feature:


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

          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)
           MyRoot root = new MyRoot();

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

          [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_
           [java] ------END


          • 2. Re: Annotate class only and crosscut methods within the clas
            Bill Burke Master

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

            This may be a better approach:

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

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


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


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