3 Replies Latest reply: Nov 7, 2008 12:56 PM by 843793 RSS

    Using an abstract method to assign an annotion property

    843793
      Hi Guys

      I have an annotation for specifiying that a method requires a transation. It's very simple, and looks like this:
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.METHOD)
      public @interface TransationRequired
      {
          PersistenceUnitDescriptor persistenceUnitDescriptor();
      
      }
      PersistenceUnitDescriptor is a simple enum, and I use Aspect J to weave code around methods annotated with this annotation.


      When I annotate a method like this
      @TransationRequired(persistenceUnitDescriptor = PersistenceUnitDescriptor.XXX)
           public Boolean func(String xx)
           {
               //TODO: Something
           }
      It works fine, but when I annotate like this :
      @TransationRequired(persistenceUnitDescriptor = getPersistenceUnit())
           public Boolean func(String xx)
           {
               //TODO: Something
           }
      the apect code is never run.

      getPersistenceUnit() is an abstract method and it is the only thing which is different between the two cases.

      I'm not sure if this is beacuse of the annotation or because the weaving, but I thought I would ask in the annotation categroy just in case.

      Anyone got an idea as to what may be causing this?

      Thanks in advance,
      Vackar
        • 1. Re: Using an abstract method to assign an annotion property
          843793
          I don't know about the interaction with aspects, but the "getPersistenceUnit()" part of
          @TransationRequired(persistenceUnitDescriptor = getPersistenceUnit())
          is not valid; the values of annotation methods must be compile-time constants so the result of a method call is not acceptable.
          • 2. Re: Using an abstract method to assign an annotion property
            843793
            OK, Thanks for the tip. Pity that it has to be constant, in my opinion annotations would be a lot more powerful if you could dynamically change attributes. That really would make them more than just an alternative to annoying config files. But perhaps there are some technical details that I'm not aware of which makes this idea seem silly.

            Thanks for the help,
            Vackar
            • 3. Re: Using an abstract method to assign an annotion property
              843793
              VackarAfzal wrote:
              OK, Thanks for the tip. Pity that it has to be constant, in my opinion annotations would be a lot more powerful if you could dynamically change attributes. That really would make them more than just an alternative to annoying config files. But perhaps there are some technical details that I'm not aware of which makes this idea seem silly.
              Annotations were designed as a compile-time construct so the values within need to be compile time constants. At runtime, dynamic proxies are used to construct objects whose methods will return the values in question. You are free to construct your own objects implementing the interface of an annotation type at runtime that return values determined more dynamically.