Class ProcessingException

All Implemented Interfaces:
Serializable

public final class ProcessingException extends RuntimeException
An error in the source, thrown to stop processing an element and report the error at a precise place: the element, one of its annotations, or one attribute of that annotation, as Diagnostics reports it.
context.forEach(elements.annotatedWith("com.example.Mapper"), mapper -> {
    if (mapper.getKind() != ElementKind.INTERFACE) {
        throw new ProcessingException("must be an interface", mapper, "com.example.Mapper");
    }
    // generate the mapper
});

ProcessingContext.forEach(Iterable, java.util.function.Consumer) reports it and goes on with the next element. A StepProcessor reports one that escapes Step.process(AnnotatedElements), and goes on with the next step. Any processor reports it with Diagnostics.error(ProcessingException). The report is an error, which makes the compilation fail, with the message of the exception; its stack trace is not reported.

The element and the annotation belong to javac's model of one compilation, which is not serializable: the exception cannot be serialized either.

See Also:
  • Constructor Details

    • ProcessingException

      public ProcessingException(String message, Element element)
      Creates an exception reporting an error on an element.
      Parameters:
      message - the message of the error
      element - the element the error is about
    • ProcessingException

      public ProcessingException(String message, Element element, AnnotationMirror annotation)
      Creates an exception reporting an error on an annotation of an element.
      Parameters:
      message - the message of the error
      element - the element the error is about
      annotation - the annotation the error is about, as found on the element: one of its annotations, or one nested in the value of an attribute of those, as a repeatable annotation is in its container, or one a class inherits from a superclass, which is reported on the class
      Throws:
      IllegalArgumentException - if the annotation is not on the element
    • ProcessingException

      public ProcessingException(String message, Element element, String annotation)
      Creates an exception reporting an error on the annotation of a type of an element.
      Parameters:
      message - the message of the error
      element - the element the error is about
      annotation - the fully qualified name of the type of the annotation the error is about
      Throws:
      IllegalArgumentException - if the element has no such annotation
    • ProcessingException

      public ProcessingException(String message, Element element, AnnotationMirror annotation, String attribute)
      Creates an exception reporting an error on an attribute of an annotation of an element. If the annotation does not write the attribute, leaving it to its default, the error is reported on the annotation.
      Parameters:
      message - the message of the error
      element - the element the error is about
      annotation - the annotation, as found on the element: one of its annotations, or one nested in the value of an attribute of those, as a repeatable annotation is in its container, or one a class inherits from a superclass, which is reported on the class
      attribute - the name of the attribute the error is about
      Throws:
      IllegalArgumentException - if the annotation is not on the element, or has no such attribute
    • ProcessingException

      public ProcessingException(String message, Element element, String annotation, String attribute)
      Creates an exception reporting an error on an attribute of the annotation of a type of an element. If the annotation does not write the attribute, leaving it to its default, the error is reported on the annotation.
      Parameters:
      message - the message of the error
      element - the element the error is about
      annotation - the fully qualified name of the type of the annotation
      attribute - the name of the attribute the error is about
      Throws:
      IllegalArgumentException - if the element has no such annotation, or the annotation no such attribute
  • Method Details

    • element

      public Element element()
      Returns the element the error is about.
      Returns:
      the element
    • annotation

      public Optional<AnnotationMirror> annotation()
      Returns the annotation of the element the error is about.
      Returns:
      the annotation; empty if the error is about the element as a whole
    • attribute

      public Optional<String> attribute()
      Returns the attribute of the annotation the error is about.
      Returns:
      the name of the attribute; empty if the error is about the element or the whole annotation