{"record":{"id":"700a6f89b79ca642","repo":"apache/beam","slug":"malformed-s-class-s-state-declaration-field-s-is-not","errorCode":null,"errorMessage":"Malformed %s class %s: state declaration field %s is not accessible.","messagePattern":"Malformed (.+?) class (.+?): state declaration field (.+?) is not accessible\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java","lineNumber":2534,"sourceCode":"      }\n    }\n  }\n\n  public static StateSpec<?> getStateSpecOrThrow(\n      StateDeclaration stateDeclaration, DoFn<?, ?> target) {\n    try {\n      Object fieldValue = stateDeclaration.field().get(target);\n      checkState(\n          fieldValue instanceof StateSpec,\n          \"Malformed %s class %s: state declaration field %s does not have type %s.\",\n          format(DoFn.class),\n          target.getClass().getName(),\n          stateDeclaration.field().getName(),\n          StateSpec.class);\n\n      return (StateSpec<?>) stateDeclaration.field().get(target);\n    } catch (IllegalAccessException exc) {\n      throw new RuntimeException(\n          String.format(\n              \"Malformed %s class %s: state declaration field %s is not accessible.\",\n              format(DoFn.class), target.getClass().getName(), stateDeclaration.field().getName()));\n    }\n  }\n\n  public static TimerSpec getTimerSpecOrThrow(\n      TimerDeclaration timerDeclaration, DoFn<?, ?> target) {\n    try {\n      Object fieldValue = timerDeclaration.field().get(target);\n      checkState(\n          fieldValue instanceof TimerSpec,\n          \"Malformed %s class %s: timer declaration field %s does not have type %s.\",\n          format(DoFn.class),\n          target.getClass().getName(),\n          timerDeclaration.field().getName(),\n          TimerSpec.class);\n","sourceCodeStart":2516,"sourceCodeEnd":2552,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java#L2516-L2552","documentation":"Beam reads a DoFn's @StateId-decorated field reflectively to get its StateSpec. If Field.get(target) throws IllegalAccessException (the field is not public or otherwise inaccessible), Beam wraps it in a RuntimeException stating the DoFn class is malformed. This indicates a state declaration field that violates Beam's accessibility requirement.","triggerScenarios":"Using @StateId on a field that is private/protected/package-private (or in a non-public class with restricted access), so reflection via stateDeclaration.field().get(target) fails with IllegalAccessException while retrieving the StateSpec.","commonSituations":"Developers habitually declare fields private; refactoring moves a DoFn into another class/package and field access rules tighten; annotation-processor-less environments where a private @StateId field was never validated at compile time.","solutions":["Make the @StateId-annotated field public (Beam requires state declaration fields to be public)","Ensure the field is an instance field on the DoFn class itself, not inherited from an inaccessible superclass","Confirm the field type is StateSpec<...> with a @StateId annotation","If using a superclass DoFn, expose the field publicly there or restructure so the subclass declares it"],"exampleFix":"// before\n@StateId(\"seen\")\nprivate final StateSpec<ValueState<Integer>> seenSpec = StateSpecs.value();\n\n// after\n@StateId(\"seen\")\npublic final StateSpec<ValueState<Integer>> seenSpec = StateSpecs.value();","handlingStrategy":"validation","validationCode":"// Verify @StateId fields are public before building the pipeline\nfor (Field f : MyFn.class.getDeclaredFields()) {\n  if (f.isAnnotationPresent(StateId.class) && !Modifier.isPublic(f.getModifiers())) {\n    throw new IllegalStateException(\"@StateId field must be public: \" + f.getName());\n  }\n}","typeGuard":"static boolean isAccessibleStateField(Field f) {\n  return Modifier.isPublic(f.getModifiers())\n      && StateSpec.class.isAssignableFrom(f.getType());","tryCatchPattern":"try {\n  pipeline.run();\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"state declaration field\")) {\n    // make the field public and retry\n  }\n}","preventionTips":["Declare @StateId/@TimerId/@TimerFamily fields always public","Add a unit test that reflectively checks DoFn declaration field visibility","Avoid storing spec fields in private base classes"],"tags":["java","apache-beam","reflection","state-api","illegal-access"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}