4 Replies Latest reply on Sep 12, 2009 7:52 AM by 843793

    Applying an annotation processor on its own source code

    843793
      Hi,

      I am developing a larger application and for a certain low level implementation problem, I came up with the idea of tagging some classes with own annotations and use an annotation processor to collect these classes and store the list of them somehow. I am a complete newbie in respect to this, and as my first attempts failed, I wonder whether it is really possible to include the implementation of Processor/AbstractProcessor (with the respective annotations on its own) in the same source code as the annotations it shall process and have the compiler to find and run the processor during the compilation or whether it is mandatory to create a separate jar file with the META-INF data in order to get it work.

      Thanks for any response.
      Klaus
        • 1. Re: Applying an annotation processor on its own source code
          EJP
          It's a bootstrapping problem. You have to have a compiled version of your annotator available to the compiler so your annotations can be processed. So you have to start by

          1. Compile your annotation classes
          2. Compile your annotator.
          3. Compile your annotator, specifying the result of (2) as an annotation processor.

          Repeat (2) and (3) as many times as necessary to get what you want out the end.
          • 2. Re: Applying an annotation processor on its own source code
            843793
            KlausM wrote:
            Hi,

            I am developing a larger application and for a certain low level implementation problem, I came up with the idea of tagging some classes with own annotations and use an annotation processor to collect these classes and store the list of them somehow. I am a complete newbie in respect to this, and as my first attempts failed, I wonder whether it is really possible to include the implementation of Processor/AbstractProcessor (with the respective annotations on its own) in the same source code as the annotations it shall process and have the compiler to find and run the processor during the compilation or whether it is mandatory to create a separate jar file with the META-INF data in order to get it work.

            Thanks for any response.
            Klaus
            You can specify a particular processor to run using javac's "-processor" option without creating a jar file for the processor. However, in addition to source files, annotation processors can also run on class files so it would be possible for your annotation processor to be a post-pass after the build otherwise completes.

            You may also want to follow the recommendation on annotation processor build hygiene:
            http://blogs.sun.com/darcy/entry/annotation_processing_build_advice_set
            • 4. Re: Applying an annotation processor on its own source code
              843793
              @I_will_never_give_up - please don't post spam.