10 Replies Latest reply: Sep 21, 2009 8:56 AM by 791266 RSS

    Tutorial required for annotation

    843793
      can any one suggest a good tutorial for annotation as i am new to annotation.

      thanks in advance
        • 1. Re: Tutorial required for annotation
          843793
          can any one suggest a good tutorial for annotation as
          i am new to annotation.
          If you are using JDK 5, the apt getting started guide can be found here:

          http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html
          • 2. Re: Tutorial required for annotation
            843793
            I do not know of one, that explains the ins and outs, but if you find an example Annotation that modifies source code, let me know!
            Todd_Musheno@yahoo.com
            • 3. Re: Tutorial required for annotation
              608410
              but if you find an example Annotation that modifies
              source code, let me know!
              You won't.


              Annotation Processing with APT or a JSR269 tool does not permit you to modify source code. You can generate new source files but not modify existing ones.

              Bruce
              • 4. Re: Tutorial required for annotation
                843793
                This is not the case see: http://java.sun.com/javaee/5/docs/api/javax/ejb/Stateful.html
                If this were the case there would be NO implmentation of the persistance, yet it would still work!
                The questions was targeted at the implementation of such a (specific) mechinisum. Please not the annotation does not define HOW data is to be persisted, just that it should be (We are looking for an example on HOW we generate code that does the actual work).
                Todd_Musheno@yahoo.com
                • 5. Re: Tutorial required for annotation
                  608410
                  This is not the case see:
                  http://java.sun.com/javaee/5/docs/api/javax/ejb/Statef
                  ul.html
                  That page does not change my view that APT and JSR269 do not provide a mechanism to modify existing code.

                  Annotations may be processed at build time or run time. APT and JSR269 address build time processing. java.lang.reflect addresses run time processing. I do not know how the @Stateful annotation causes statefulness, I do know that APT or javac will NOT be directly modifying the annotated code.

                  There are many mechanisms available to you. Your job is to understand the strengths and limitations of each and work out which combination of mechanisms best meets your need.

                  If you describe what you want to achieve rather than ask how to do something impossible, we might be able to point you in the right direction.
                  • 6. Re: Tutorial required for annotation
                    843793
                    I am looking to use annotations to (Fill in) implementation detail, given a proper annotation processor. I guess I was assuming APT was an (Annotation Processor), if this is not the case, can someone point me twards one?
                    Todd_Musheno@yahoo.com
                    • 7. Re: Tutorial required for annotation
                      843793
                      I am looking to use annotations to (Fill in)
                      implementation detail, given a proper annotation
                      processor. I guess I was assuming APT was an
                      (Annotation Processor), if this is not the case, can
                      someone point me twards one?
                      Todd_Musheno@yahoo.com
                      The apt tool or javac with JSR 269 can be used for this purpose, subject to the capabilities provided in those APIs. Both APIs present a read-only view of the source code; the source code cannot be directly modified via those APIs. However, as Bruce has noted, if the annotated class is designed to inherit from a generated class or return objects from a generated subclass, much of the effect of modifying the annotated source can be achieved.
                      • 8. Re: Tutorial required for annotation
                        843793
                        see: http://forum.java.sun.com/thread.jspa?messageID=4367185
                        So, what I am hearing is this is not possable, how does one put a request in for this???
                        Alternatly, how does one acomplish this?
                        Todd :-)
                        • 9. Re: Tutorial required for annotation
                          843793
                          Hmmm maybe there is a way to modify the parsed stuff.

                          javax.lang.model is read-only but it's just an interface... maybe one could use the underlying implementation classes of this interface.

                          If I output the class name of an instance of javax.lang.model.element.Element, I get "com.sun.tools.javac.code.Symbol$ClassSymbol".

                          There is an API Documentation of these classes... of course it says not to use this classes ;-). I think i am going to figure out if these classes can be used to modify the parsed source structure tomorrow. I really need this too ;-).
                          • 10. Re: Tutorial required for annotation
                            791266
                            Hi,

                            Please don't resurrect old threads. I'm locking this one.

                            Kaj