title: Is it possible for an annotation processor to access types generated in a previous round?
body: <p>I have an annotation processor that generates an ID class for each annotated class. I would like classes to be able to refer to the generated ID types of other classes in the same compilation unit. Unfortunately, it seems like the annotation processor always gives the kind of a generated class as ERROR, even if that type was generated in a previous compilation round or by an entirely separate processor. Is there a way around this?</p>
  
  <p>Here's a minimal example. Say I have the following class:</p>
  
  <pre><code>package tmp;
  
  @MyAnnotation
  public class Foo {
    private Foo me;
    private FooId myId;
  }
  </code></pre>
  
  <p>It's processed first by this annotation processor to generate the IDs:</p>
  
  <pre><code>@SupportedAnnotationTypes("tmp.proc.MyAnnotation")
  @SupportedSourceVersion(SourceVersion.RELEASE_7)
  public class CreateIdProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) {
      for (Element elem : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
        System.out.println("Writing ID for " + elem.getSimpleName());
        try {
          Writer file = processingEnv.getFiler().createSourceFile(elem + "Id").openWriter();
          file.write(String.format("package tmp; public class %sId {}", elem.getSimpleName()));
          file.close();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return false;
    }
  }
  </code></pre>
  
  <p>And then processed by this annotation processor to analyze the types:</p>
  
  <pre><code>@SupportedAnnotationTypes("tmp.proc.MyAnnotation")
  @SupportedSourceVersion(SourceVersion.RELEASE_7)
  public class CheckIdProcessor extends AbstractProcessor {
    @Override
    public boolean process(Set&lt;? extends TypeElement&gt; annotations, RoundEnvironment roundEnv) {
      for (Element elem : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
        for (Element element : elem.getEnclosedElements()) {
          System.out.printf("%s = %s (%s)\n", element.getSimpleName(), element.asType(), element.asType().getKind());
        }
      }
      return false;
    }
  }
  </code></pre>
  
  <p>The output of running the build looks like this:</p>
  
  <pre><code>Writing ID for Foo
  &lt;init&gt; = ()void (EXECUTABLE)
  me = tmp.Foo (DECLARED)
  myId = FooId (ERROR)
  </code></pre>
  
  <p>Everything compiles fine, but the second processor sees the type of <code>myId</code> as ERROR even though it was generated by the first processor (and the FooId class is indeed there in the output jar). This prevents the annotation processor from analyzing <code>FooId</code> to, for example, find out the package it would belong to in order to import it. Are there any workarounds for this problem?</p>