This discussion is archived
5 Replies Latest reply: Jun 2, 2010 6:16 PM by 843793 RSS

Communicaton between two (or more) annotation processors ?

843793 Newbie
Currently Being Moderated
Hi,

I am writing two annotion processors.
The second processor needs information that is put in a Hashtable by the first processor.

Is there a way to do that ?

thanks !
  • 1. Re: Communicaton between two (or more) annotation processors ?
    800389 Newbie
    Currently Being Moderated
    jean-fr wrote:
    I am writing two annotion processors.
    The second processor needs information that is put in a Hashtable by the first processor.
    Why do you need to share in-memory data between two different processor?
  • 2. Re: Communicaton between two (or more) annotation processors ?
    843793 Newbie
    Currently Being Moderated
    Because the first processor extracts information that would be useful
    to the second one.

    Should I interpret your question as an indication that I use annotation processors in a wrong way ? in fact, I
    could probably use one processor but I thought that it would be more
    elegant (if not fast) with 2 processors. may be I am on the wrong way !?
  • 3. Re: Communicaton between two (or more) annotation processors ?
    608410 Newbie
    Currently Being Moderated
    A couple of ideas.

    You could store the information in a singleton. I think javac uses the same classloader for all annotations but I could be wrong. (if not you'll have 2 singletons !!)

    Alternately drop the "implements Processor" from your two processors and make a new Processor that just delegates to your old ones, then either use a singleton (they'll be in the same classloader) or just pass the data in from the new master processor.


    Bruce
  • 4. Re: Communicaton between two (or more) annotation processors ?
    800389 Newbie
    Currently Being Moderated
    jean-fr wrote:
    Should I interpret your question as an indication that I use annotation processors in a wrong way ? in fact, I
    could probably use one processor but I thought that it would be more
    elegant (if not fast) with 2 processors. may be I am on the wrong way !?
    That's what my confusion is. Is it correct to share the in-memory data between two different annotation processor? Java guys must have thought about it?

    One more option to try out is (Its just theoretical thought and not sure it is the elegant way to do?) to create one shared static collection in factory class and provide static method to access it.
  • 5. Re: Communicaton between two (or more) annotation processors ?
    843793 Newbie
    Currently Being Moderated
    Adhir_Mehta wrote:
    jean-fr wrote:
    Should I interpret your question as an indication that I use annotation processors in a wrong way ? in fact, I
    could probably use one processor but I thought that it would be more
    elegant (if not fast) with 2 processors. may be I am on the wrong way !?
    That's what my confusion is. Is it correct to share the in-memory data between two different annotation processor? Java guys must have thought about it?

    One more option to try out is (Its just theoretical thought and not sure it is the elegant way to do?) to create one shared static collection in factory class and provide static method to access it.
    Whether it is correct or not depends on what you are trying to do; it is certainly against the default usage pattern of annotation processors.

    Quoting the Processor specification:

    The tool infrastructure will interact with classes implementing this interface as follows:

    1. If an existing Processor object is not being used, to create an instance of a processor the tool calls the no-arg constructor of the processor class.
    2. Next, the tool calls the init method with an appropriate ProcessingEnvironment.
    3. Afterwards, the tool calls getSupportedAnnotationTypes, getSupportedOptions, and getSupportedSourceVersion. These methods are only called once per run, not on each round.
    4. As appropriate, the tool calls the process method on the Processor object; a new Processor object is not created for each round.

    [...]

    To be robust when running in different tool implementations, an annotation processor should have the following properties:

    1. The result of processing a given input is not a function of the presence or absence of other inputs (orthogonality).
    2. Processing the same input produces the same output (consistency).
    3. Processing input A followed by processing input B is equivalent to processing B then A (commutativity)
    4. Processing an input does not rely on the presence of the output of other annotation processors (independence)

    If you are using the programmatic interface from javax.tools to do annotation processing, you can pass in already instantiated Processor objects, which can share state however they like. However, if you are using the discovery process and the annotation processing infrastructure is creating the Processor objects, sharing state will be a bit of work since the Processors and intended to be independent.

    Note that it is relatively easy to proxy the annotation processing infrastructure if you want to instantiate processors yourself and manage the extra state. In other words, if is possible that the Processor object that you return to the infrastructure could logically be a composite of several Processor objects you manage yourself.