{"record":{"id":"5a35fd688d5a654f","repo":"apache/beam","slug":"overloaded-factory-methods-s","errorCode":null,"errorMessage":"Overloaded factory methods: %s","messagePattern":"Overloaded factory methods: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/thrift/src/main/java/org/apache/beam/sdk/io/thrift/ThriftSchema.java","lineNumber":251,"sourceCode":"    return (Map<FieldT, FieldMetaData>) FieldMetaData.getStructMetaDataMap((Class<T>) targetClass);\n  }\n\n  private FieldValueTypeInformation fieldValueTypeInfo(Class<?> type, String fieldName) {\n    if (TUnion.class.isAssignableFrom(type)) {\n      final List<Method> factoryMethods =\n          Stream.of(type.getDeclaredMethods())\n              .filter(m -> m.getName().equals(fieldName))\n              .filter(m -> m.getModifiers() == (Modifier.PUBLIC | Modifier.STATIC))\n              .filter(m -> m.getParameterCount() == 1)\n              .filter(m -> m.getReturnType() == type)\n              .collect(Collectors.toList());\n      if (factoryMethods.isEmpty()) {\n        throw new IllegalArgumentException(\n            String.format(\n                \"No suitable static factory method: %s.%s(...)\", type.getName(), fieldName));\n      }\n      if (factoryMethods.size() > 1) {\n        throw new IllegalStateException(\"Overloaded factory methods: \" + factoryMethods);\n      }\n      return FieldValueTypeInformation.forSetter(\n          TypeDescriptor.of(type), factoryMethods.get(0), \"\");\n    } else {\n      try {\n        return FieldValueTypeInformation.forField(\n            TypeDescriptor.of(type), type.getDeclaredField(fieldName), 0);\n      } catch (NoSuchFieldException e) {\n        throw new IllegalArgumentException(e);\n      }\n    }\n  }\n\n  @Override\n  public @NonNull SchemaUserTypeCreator schemaTypeCreator(\n      @NonNull TypeDescriptor<?> targetTypeDescriptor, @NonNull Schema schema) {\n    final Map<TFieldIdEnum, FieldMetaData> fieldDescriptors =\n        schemaFieldDescriptors(targetTypeDescriptor.getRawType(), schema);","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/thrift/src/main/java/org/apache/beam/sdk/io/thrift/ThriftSchema.java#L233-L269","documentation":"When more than one static factory method matches a thrift union case field (same name, public static, one parameter, returning the case type), ThriftSchema cannot disambiguate and throws an IllegalStateException listing the overloaded methods.","triggerScenarios":"A union case type declaring multiple overloads of the field-named static factory (e.g. differing parameter types), so the reflection filter matches more than one method.","commonSituations":"Hand-added convenience overloads in generated thrift union classes; thrift compiler changes producing extra factory signatures; subclassing generated unions and adding overloads.","solutions":["Remove or rename extra overloads so exactly one static factory method matches the field name","Regenerate the union classes from the .thrift file to restore a single canonical factory per case","Check the exception message listing the offending methods and keep only the generated one"],"exampleFix":"// before\npublic static MyUnion intValue(Integer v) {...}\npublic static MyUnion intValue(long v) {...}   // ambiguous overload\n// after\npublic static MyUnion intValue(Integer v) {...}","handlingStrategy":"validation","validationCode":"long count = java.util.Arrays.stream(MyUnion.class.getDeclaredMethods()).filter(m -> m.getName().equals(\"intValue\") && m.getParameterCount() == 1).count(); if (count != 1) { throw new IllegalStateException(\"expected exactly one static factory for union case\"); }","typeGuard":null,"tryCatchPattern":"try { schema = ThriftSchema.provider().schemaFor(td); } catch (IllegalStateException e) { throw new IllegalStateException(\"Remove overloaded union factory methods\", e); }","preventionTips":["Never add convenience overloads to generated union classes","Keep generated thrift sources unmodified and regenerate from IDL","Review diffs of generated code in code review"],"tags":["java","thrift","union","reflection"],"backgroundTag":"invalid-state-transition","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"}