{"record":{"id":"452afc0aa0b406a5","repo":"bazelbuild/bazel","slug":"no-annotation-s-found-for-this-element","errorCode":null,"errorMessage":"No annotation %s found for this element.","messagePattern":"No annotation (.+?) found for this element\\.","errorType":"validation","errorClass":"OptionProcessorException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/google/devtools/common/options/processor/ProcessorUtils.java","lineNumber":52,"sourceCode":"      Types typeUtils,\n      Element element,\n      Class<? extends Annotation> annotation)\n      throws OptionProcessorException {\n    TypeElement annotationElement = elementUtils.getTypeElement(annotation.getCanonicalName());\n    if (annotationElement == null) {\n      // This can happen if the annotation is on the -processorpath but not on the -classpath.\n      throw new OptionProcessorException(\n          element, \"Unable to find the type of annotation %s.\", annotation);\n    }\n    TypeMirror annotationMirror = annotationElement.asType();\n\n    for (AnnotationMirror annot : element.getAnnotationMirrors()) {\n      if (typeUtils.isSameType(annot.getAnnotationType(), annotationMirror)) {\n        return annot;\n      }\n    }\n    // No annotation of this requested type found.\n    throw new OptionProcessorException(\n        element, \"No annotation %s found for this element.\", annotation);\n  }\n\n  /**\n   * Returns the contents of a {@code Class}-typed field in an annotation.\n   *\n   * <p>Taken & adapted from AutoValueProcessor.java\n   *\n   * <p>This method is needed because directly reading the value of such a field from an\n   * AnnotationMirror throws:\n   *\n   * <pre>\n   * javax.lang.model.type.MirroredTypeException: Attempt to access Class object for TypeMirror Foo.\n   * </pre>\n   *\n   * @param annotation The annotation to read from.\n   * @param fieldName The name of the field to read, e.g. \"exclude\".\n   * @return a set of fully-qualified names of classes appearing in 'fieldName' on 'annotation' on","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/com/google/devtools/common/options/processor/ProcessorUtils.java#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the element actually carries the annotation (check the source file and the @Retention/@Target of the annotation).","Confirm the annotation class on -classpath is the exact same fully-qualified type the processor requests (no duplicate copies in different packages or jars).","In a custom processor, filter root elements with RoundEnvironment.getElementsAnnotatedWith before calling getAnnotation.","Clean and rebuild so stale annotation classes are regenerated."],"exampleFix":"// before\nfor (Element e : roundEnv.getRootElements()) {\n  AnnotationMirror m = ProcessorUtils.getAnnotation(e, MyOption.class); // throws\n}\n\n// after\nfor (Element e : roundEnv.getElementsAnnotatedWith(MyOption.class)) {\n  AnnotationMirror m = ProcessorUtils.getAnnotation(e, MyOption.class);\n}","handlingStrategy":"validation","validationCode":"// Before calling the lookup, confirm the element carries the annotation type\nboolean hasAnnotation(Element element, Class<? extends Annotation> ann) {\n  for (AnnotationMirror m : element.getAnnotationMirrors()) {\n    if (m.getAnnotationType().toString().equals(ann.getCanonicalName())) {\n      return true;\n    }\n  }\n  return false;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter round elements with getElementsAnnotatedWith before per-element annotation lookup.","Keep exactly one version of annotation classes on the compile classpath.","Assert element kind (class/method/parameter) matches where the annotation's @Target allows it."],"tags":["annotation-processing","java","compile-time","options"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}