{"record":{"id":"336a08f3738b1840","repo":"apache/hadoop","slug":"error-setting-field-annotated-with","errorCode":null,"errorMessage":"Error setting field {} annotated with {}","messagePattern":"Error setting field (.+?) annotated with (.+?)","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java","lineNumber":154,"sourceCode":"        continue;\n      }\n      try {\n        // skip fields already set\n        field.setAccessible(true);\n        if (field.get(source) != null) continue;\n      } catch (Exception e) {\n        LOG.warn(\"Error accessing field \"+ field +\" annotated with\"+\n                 annotation, e);\n        continue;\n      }\n      MutableMetric mutable = factory.newForField(field, (Metric) annotation,\n                                                  registry);\n      if (mutable != null) {\n        try {\n          field.set(source, mutable); // Set the source field to MutableMetric\n          hasAtMetric = true;\n        } catch (Exception e) {\n          throw new MetricsException(\"Error setting field \"+ field +\n                                     \" annotated with \"+ annotation, e);\n        }\n      }\n    }\n  }\n\n  /** Add {@link MutableMetric} for a method annotated with {@link Metric} */\n  private void add(Object source, Method method) {\n    for (Annotation annotation : method.getAnnotations()) {\n      if (!(annotation instanceof Metric)) {\n        continue;\n      }\n      factory.newForMethod(source, method, (Metric) annotation, registry);\n      hasAtMetric = true;\n    }\n  }\n}\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java#L136-L172","documentation":"After building the MutableMetric for an @Metric-annotated field, MetricsSourceBuilder injects it with field.set(source, mutable). Any reflection failure at that assignment — most commonly a final field the builder cannot overwrite, or access blocked by security/module restrictions — is wrapped in MetricsException('Error setting field <field> annotated with <annotation>').","triggerScenarios":"Declaring @Metric on a final field (the builder must assign the MutableMetric into it), or on a field whose reflective write is disallowed in the runtime environment.","commonSituations":"Style habit of marking injected fields final; annotated fields that the developer also initializes themselves; restrictive security managers or module access rules around reflection.","solutions":["Remove final from @Metric-annotated fields so the builder can assign them","Leave the field uninitialized (the builder creates and injects the MutableMetric)","Ensure the class is reflectively accessible from the metrics2 code path (no security-manager denial)"],"exampleFix":"// before\n@Metric\npublic final MutableGaugeLong size;\n\n// after\n@Metric\npublic MutableGaugeLong size;","handlingStrategy":"validation","validationCode":"for (Field f : cls.getDeclaredFields()) {\n  if (f.isAnnotationPresent(Metric.class) && Modifier.isFinal(f.getModifiers())) {\n    throw new IllegalStateException(\"@Metric field must not be final: \" + f);\n  }\n}","typeGuard":"static boolean isInjectableMetricField(Field f) {\n  return f.isAnnotationPresent(Metric.class)\n      && !Modifier.isFinal(f.getModifiers())\n      && !Modifier.isStatic(f.getModifiers());\n}","tryCatchPattern":null,"preventionTips":["Never mark @Metric fields final — the builder assigns them reflectively","Leave @Metric fields uninitialized instead of assigning your own MutableMetric","Add a reflection-based lint (isInjectableMetricField) to the build or test suite"],"tags":["metrics2","hadoop","annotation","reflection","field-injection"],"backgroundTag":"reflection-field-assignment-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}