Search code examples
javaandroidannotation-processingannotation-processor

Annotation processing order - cannot find symbol


What I have:

  1. I have written an annotation processor @ViewModel which generates classes ViewModel_<annotated class name>.
  2. I have written another annotation processor @Retainable which generates builders for fields annotated with @Retainable.

My implementation is based on this article:

http://blog.stablekernel.com/the-10-step-guide-to-annotation-processing-in-android-studio

but I have two annotation processors.

My problem:

If I annotate field of type ViewModel_<annotated class name> with my @Retainable annotation.

...
@Retainable
ViewModel_<annotated class name> viewModelGenerated;
...

the compiler complains:

Error:(33, 15) error: cannot find symbol class ViewModel_<annotated class name>

but,

If I annotate this field with daggers @Inject instead of my @Retainable everything is compiling.

Question:

What is the difference between my annotation and daggers? What can I do to make it work?


Solution

  • From the Javadoc of the Processor interface.

    Annotation processing happens in a sequence of rounds. On each round, a processor may be asked to process a subset of the annotations found on the source and class files produced by a prior round. The inputs to the first round of processing are the initial inputs to a run of the tool; these initial inputs can be regarded as the output of a virtual zeroth round of processing.

    Processor.process is called once for each round. The type ViewModel_* will not be available in the first round. During this round, the type of the field will be ErrorType (i.e. TypeKind.ERROR). An annotation prcessor might handle such type by deferring further processing into the next round, as long as RoundEnvironment.processingOver() is false.

    Anyway, this doesn't quite explain the error message you are seeing on its own. To make any further claims about the cause of your issue, I would have to see a proper example.