2 Replies Latest reply: Sep 4, 2008 6:20 PM by 843793 RSS

    Query about apt

    843793
      Hi,

      I am just trying to understand and explore Annotations and apt.

      I was wondering, is it possible to replace a method with a particular annotation with another piece of code ( another method) ?

      What I have in mind is, lets say there is a method which does DB operations. I could write it initially assuming that it will be used on Oracle. But then I want to be able to replace this with code that can be used with Java DB if I want to change my architecture.

      If i have a special annotation on such methods, can i use apt to replace them all and generate new source files?

      Thanks
      Hari
        • 1. Re: Query about apt
          843793
          Wilddev007 wrote:
          Hi,

          I am just trying to understand and explore Annotations and apt.
          If you can use JDK 6 instead, I strongly recommend instead using the standardized JSR 269 annotation processing built into javac.
          I was wondering, is it possible to replace a method with a particular annotation with another piece of code ( another method) ?
          In general, no. However, it is possible to achieve some of that effect using the Decorator pattern and generating the superclass or subclass using apt; see the archives of this forum for past discussion.
          What I have in mind is, lets say there is a method which does DB operations. I could write it initially assuming that it will be used on Oracle. But then I want to be able to replace this with code that can be used with Java DB if I want to change my architecture.

          If i have a special annotation on such methods, can i use apt to replace them all and generate new source files?
          As a general comment, it sounds to me like that functionality should be factored into an interface and then a factory could give the rest of your code some implementation of that interface, perhaps even using java.util.ServiceLoader in JDK 6. In other words, while it may be convenient to use annotation processing to manage generating variants of the same functionality, I recommend not baking in a hard source or class file dependency on this into your system.
          Thanks
          Hari
          • 2. Re: Query about apt
            843793
            I was wondering, is it possible to replace a method with a particular annotation with another piece of code ( another method) ?
            I don´t think it would be useful in any way.