Interface Step


public interface Step
One unit of work of a StepProcessor: the processing of the elements annotated with some annotations, or with annotations that some meta-annotations annotate.
final class MapperStep implements Step {
    public Set<String> annotations() {
        return Set.of("com.example.Mapper");
    }

    public Set<Element> process(AnnotatedElements elements) {
        for (Element mapper : elements.annotatedWith("com.example.Mapper")) {
            // generate code for the mapper
        }
        return Set.of();
    }
}

A step reports an invalid element by throwing a ProcessingException, which ProcessingContext.forEach(Iterable, java.util.function.Consumer) catches for each element:

public Set<Element> process(AnnotatedElements elements) {
    context.forEach(elements.annotatedWith("com.example.Mapper"), mapper -> {
        if (mapper.getKind() != ElementKind.INTERFACE) {
            throw new ProcessingException("must be an interface", mapper);
        }
        // generate code for the mapper
    });
    return Set.of();
}
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the annotations this step processes.
    default Set<String>
    Returns the meta-annotations this step processes: the step also gets the elements annotated with any annotation whose type is annotated with one of them, such as each qualifier for javax.inject.Qualifier, when the annotations are not known in advance.
    default String
    Returns the name of this step in the debug output of its StepProcessor, which -Agaia.debug turns on.
    Set<? extends Element>
    Processes the elements annotated with this step's annotations that are ready: every type in their signature is resolved.
    default void
    Finishes this step once all rounds are done: to check what they produced together, to report a summary, or to register or discard the implementations of a service before its file is written.
  • Method Details

    • annotations

      Set<String> annotations()
      Returns the annotations this step processes. The annotation classes need not be on the processor's class path.
      Returns:
      the fully qualified names of the annotation types
    • metaAnnotations

      default Set<String> metaAnnotations()
      Returns the meta-annotations this step processes: the step also gets the elements annotated with any annotation whose type is annotated with one of them, such as each qualifier for javax.inject.Qualifier, when the annotations are not known in advance.
      public Set<String> annotations() {
          return Set.of();
      }
      
      public Set<String> metaAnnotations() {
          return Set.of("com.example.Injecting");
      }
      
      public Set<Element> process(AnnotatedElements elements) {
          for (Element element : elements.withAnnotationsAnnotatedWith("com.example.Injecting")) {
              for (AnnotationReader injecting : context.annotationsAnnotatedWith(element, "com.example.Injecting")) {
                  ...
              }
          }
          return Set.of();
      }
      

      Each element is given under the annotation it has, with AnnotatedElements.annotatedWith(String), and under each of the step's meta-annotations that annotate the type of that annotation, with AnnotatedElements.withAnnotationsAnnotatedWith(String). An element annotated with a meta-annotation itself, such as an annotation type, is not found by it. Only the meta-annotations of the annotation type itself count, not those of its meta-annotations, as for ProcessingContext.annotationsAnnotatedWith(Element, String): list each meta-annotation to follow. The annotations the container of a repeatable annotation wraps count, and a class has the @Inherited annotations of its superclasses. An annotation type read from a class file only shows the meta-annotations kept in class files, with a retention of CLASS or RUNTIME.

      As with annotations(), an element whose signature has a type javac could not resolve yet is held back until a later round resolves it. The annotation that has the meta-annotation must be resolved, though: until its type exists, javac cannot tell what annotates it. Javac only shows a round the elements of the sources and classes new in that round, so an element whose annotation type is generated in a later round than the element itself is never found by its meta-annotations; an annotation type generated with or before the code it annotates is.

      A StepProcessor with a step that has meta-annotations supports all annotations, *, and so takes part in every round. It still claims none, so that other processors see them.

      Returns:
      the fully qualified names of the meta-annotation types; none by default
    • process

      Set<? extends Element> process(AnnotatedElements elements)
      Processes the elements annotated with this step's annotations that are ready: every type in their signature is resolved. The others are kept for a later round, and given to this step once they are ready.
      Parameters:
      elements - the ready elements, in source order, by each of this step's annotations they have, and by each of its meta-annotations their annotations have
      Returns:
      the elements to give to this step again in the next round, for example because a type they need is not generated yet; an element still deferred after the last round is reported as an error
      Throws:
      ProcessingException - if an element is invalid: the StepProcessor reports the error, and gives none of these elements to this step again
    • processingOver

      default void processingOver()
      Finishes this step once all rounds are done: to check what they produced together, to report a summary, or to register or discard the implementations of a service before its file is written.
      public void processingOver() {
          if (hasHandWrittenServiceFile()) {
              context.services().discard(FACTORY);
              context.diagnostics().error("Remove " + FACTORY_FILE + ": the processor writes it");
              return;
          }
          context.diagnostics().note("On the module path, declare: provides " + FACTORY + " with ...;");
      }
      

      The StepProcessor calls it once, for each step in turn, in the last round, the one where RoundEnvironment.processingOver() is true: after it reports the elements still deferred, and before it writes the service files. Javac only runs that round for a processor it ran in an earlier round, as when the compilation has one of the annotations the processor supports. It is too late to generate sources then: no processor sees them. An element of an earlier round is found again by name, with ProcessingContext.elements(), and errors and notes are reported through ProcessingContext.diagnostics().

      The service files are written whatever errors were reported, except the files of the services whose implementations were discarded.

      Throws:
      ProcessingException - if something is invalid: the StepProcessor reports the error, and goes on with the next step; any other exception propagates, and the service files are not written
    • name

      default String name()
      Returns the name of this step in the debug output of its StepProcessor, which -Agaia.debug turns on. If several steps of a processor have the same name, each is followed there by # and its rank among them, as MapperStep#1 and MapperStep#2.
      Returns:
      the name; by default, the simple name of the class of this step, or for an anonymous class, its binary name without the package, such as MapperProcessor$1