bazelbuild/bazel · error · OptionProcessorException

No annotation %s found for this element.

Error message

No annotation %s found for this element.

What it means

Thrown by the options annotation processor when it inspects an Element that does not actually carry an instance of the expected annotation. ProcessorUtils.getAnnotation(Element, ...) looks up the annotation's TypeMirror and scans the element's annotation mirrors; if none matches, this OptionProcessorException aborts processing of that element. It usually means the processor was asked to process an element that the annotation was never applied to, or that a different annotation instance (same simple name, different package) is present.

Source

Thrown at src/main/java/com/google/devtools/common/options/processor/ProcessorUtils.java:52

      Types typeUtils,
      Element element,
      Class<? extends Annotation> annotation)
      throws OptionProcessorException {
    TypeElement annotationElement = elementUtils.getTypeElement(annotation.getCanonicalName());
    if (annotationElement == null) {
      // This can happen if the annotation is on the -processorpath but not on the -classpath.
      throw new OptionProcessorException(
          element, "Unable to find the type of annotation %s.", annotation);
    }
    TypeMirror annotationMirror = annotationElement.asType();

    for (AnnotationMirror annot : element.getAnnotationMirrors()) {
      if (typeUtils.isSameType(annot.getAnnotationType(), annotationMirror)) {
        return annot;
      }
    }
    // No annotation of this requested type found.
    throw new OptionProcessorException(
        element, "No annotation %s found for this element.", annotation);
  }

  /**
   * Returns the contents of a {@code Class}-typed field in an annotation.
   *
   * <p>Taken & adapted from AutoValueProcessor.java
   *
   * <p>This method is needed because directly reading the value of such a field from an
   * AnnotationMirror throws:
   *
   * <pre>
   * javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror Foo.
   * </pre>
   *
   * @param annotation The annotation to read from.
   * @param fieldName The name of the field to read, e.g. "exclude".
   * @return a set of fully-qualified names of classes appearing in 'fieldName' on 'annotation' on

View on GitHub (pinned to e6e199d060)

Solutions

  1. Verify the element actually carries the annotation (check the source file and the @Retention/@Target of the annotation).
  2. Confirm the annotation class on -classpath is the exact same fully-qualified type the processor requests (no duplicate copies in different packages or jars).
  3. In a custom processor, filter root elements with RoundEnvironment.getElementsAnnotatedWith before calling getAnnotation.
  4. Clean and rebuild so stale annotation classes are regenerated.

Example fix

// before
for (Element e : roundEnv.getRootElements()) {
  AnnotationMirror m = ProcessorUtils.getAnnotation(e, MyOption.class); // throws
}

// after
for (Element e : roundEnv.getElementsAnnotatedWith(MyOption.class)) {
  AnnotationMirror m = ProcessorUtils.getAnnotation(e, MyOption.class);
}
Defensive patterns

Strategy: validation

Validate before calling

// Before calling the lookup, confirm the element carries the annotation type
boolean hasAnnotation(Element element, Class<? extends Annotation> ann) {
  for (AnnotationMirror m : element.getAnnotationMirrors()) {
    if (m.getAnnotationType().toString().equals(ann.getCanonicalName())) {
      return true;
    }
  }
  return false;
}

Prevention

When it happens

Trigger: Calling getAnnotation(element, annotationClass) where element.getAnnotationMirrors() contains no mirror of that exact type; processing round where a source file references the annotation but the element passed is e.g. the enclosing package or a different method; annotation present only on the -processorpath copy of the annotation class so the TypeMirror comparison fails.

Common situations: Refactoring an annotation to a new package while stale compiled classes remain on the classpath; a custom annotation processor iterating all elements instead of filtering getElementsByAnnotation; version skew where the processor expects a newer annotation with extra retention/meta-annotations.

Related errors


AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14). Data as JSON: /api/errors/452afc0aa0b406a5. Report an issue: GitHub.