{"record":{"id":"257b92bc24fe6a0a","repo":"apache/beam","slug":"a-method-marked-with-schemacreate-in-class-does-not-return-a","errorCode":null,"errorMessage":"A method marked with SchemaCreate in class {} does not return a type assignable to {}","messagePattern":"A method marked with SchemaCreate in class (.+?) does not return a type assignable to (.+?)","errorType":"exception","errorClass":"InvalidParameterException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/ReflectUtils.java","lineNumber":136,"sourceCode":"        .filter(m -> m.getAnnotation(SchemaCreate.class) != null)\n        .findFirst()\n        .orElse(null);\n  }\n\n  public static @Nullable Method getAnnotatedCreateMethod(Class<?> clazz) {\n    return ANNOTATED_CONSTRUCTORS.computeIfAbsent(\n        clazz,\n        c -> {\n          Method method =\n              Arrays.stream(clazz.getDeclaredMethods())\n                  .filter(m -> !Modifier.isPrivate(m.getModifiers()))\n                  .filter(m -> !Modifier.isProtected(m.getModifiers()))\n                  .filter(m -> Modifier.isStatic(m.getModifiers()))\n                  .filter(m -> m.getAnnotation(SchemaCreate.class) != null)\n                  .findFirst()\n                  .orElse(null);\n          if (method != null && !clazz.isAssignableFrom(method.getReturnType())) {\n            throw new InvalidParameterException(\n                \"A method marked with SchemaCreate in class \"\n                    + clazz\n                    + \" does not return a type assignable to \"\n                    + clazz);\n          }\n          return method;\n        });\n  }\n\n  // Get all public, non-static, non-transient fields.\n  public static List<Field> getFields(Class<?> clazz) {\n    return DECLARED_FIELDS.computeIfAbsent(\n        clazz,\n        c -> {\n          Map<String, Field> types = new LinkedHashMap<>();\n          do {\n            if (c.getPackage() != null && c.getPackage().getName().startsWith(\"java.\")) {\n              break; // skip java built-in classes","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/ReflectUtils.java#L118-L154","documentation":"ReflectUtils.getAnnotatedCreateMethod searches a class for a static method annotated with @SchemaCreate and verifies that its return type is assignable to the class itself. If a @SchemaCreate method returns an incompatible type, this InvalidParameterException is thrown, since such a method could not be used to create instances of the class.","triggerScenarios":"Annotating a static method with @SchemaCreate whose declared return type is not the class itself (or a subtype), e.g. a factory returning a builder, a different class, or void.","commonSituations":"Copy-pasting @SchemaCreate onto helper/static methods, annotating a method that returns a common interface not implemented by the annotated class's hierarchy, or moving the annotation during refactoring.","solutions":["Change the @SchemaCreate method's return type to the annotated class (or a subtype assignable to it).","Remove @SchemaCreate from methods that are not intended as creators.","If creating via a different concrete type, annotate the correct target class instead and register that type with the schema."],"exampleFix":"// before\nclass Order {\n  @SchemaCreate\n  public static OrderBuilder builder() { ... } // returns wrong type\n}\n// after\nclass Order {\n  @SchemaCreate\n  public static Order create(String id) { return new Order(id); }\n}","handlingStrategy":"validation","validationCode":"for (Method m : MyPojo.class.getDeclaredMethods()) {\n  if (m.isAnnotationPresent(org.apache.beam.sdk.schemas.SchemaCreate.class)\n      && !MyPojo.class.isAssignableFrom(m.getReturnType())) {\n    throw new IllegalStateException(\"@SchemaCreate method \" + m + \" must return MyPojo\");\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  Method m = ReflectUtils.getAnnotatedCreateMethod(MyPojo.class, MyPojo.class, SchemaCreate.class);\n} catch (InvalidParameterException e) {\n  // fix the @SchemaCreate method's return type and retry\n}","preventionTips":["Only annotate static factory methods whose return type is exactly the annotated class.","Grep for @SchemaCreate in code review to verify return types.","Cover @SchemaCreate usage in unit tests so mismatched return types fail fast."],"tags":["java","beam-schema","annotation","reflection"],"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-14T16:17:12.679Z"}