apache/beam · error · IllegalArgumentException

cannot register Coder : method named 'of' with arguments of

Error message

cannot register Coder : method named 'of' with  arguments of Coder type is not static

What it means

After locating a method named `of` with the right signature, getFactoryMethod checks Modifier.isStatic. If the `of` method is an instance method, registration with CoderProviders.fromStaticMethods fails with this IllegalArgumentException, because the provider needs to invoke `of` reflectively without any Coder instance.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/CoderProviders.java:125

      // Find the static factory method of coderClazz named 'of' with
      // the appropriate number of type parameters.
      int numTypeParameters = coderClazz.getTypeParameters().length;
      Class<?>[] factoryMethodArgTypes = new Class<?>[numTypeParameters];
      Arrays.fill(factoryMethodArgTypes, Coder.class);
      try {
        factoryMethodCandidate = coderClazz.getDeclaredMethod("of", factoryMethodArgTypes);
      } catch (NoSuchMethodException | SecurityException exn) {
        throw new IllegalArgumentException(
            "cannot register Coder "
                + coderClazz
                + ": "
                + "does not have an accessible method named 'of' with "
                + numTypeParameters
                + " arguments of Coder type",
            exn);
      }
      if (!Modifier.isStatic(factoryMethodCandidate.getModifiers())) {
        throw new IllegalArgumentException(
            "cannot register Coder "
                + coderClazz
                + ": "
                + "method named 'of' with "
                + numTypeParameters
                + " arguments of Coder type is not static");
      }
      if (!coderClazz.isAssignableFrom(factoryMethodCandidate.getReturnType())) {
        throw new IllegalArgumentException(
            "cannot register Coder "
                + coderClazz
                + ": "
                + "method named 'of' with "
                + numTypeParameters
                + " arguments of Coder type does not return a "
                + coderClazz);
      }
      try {

View on GitHub (pinned to 12126d8942)

Solutions

  1. Add the `static` modifier to the `of` method in the Coder class
  2. Ensure the method also matches the required signature: return type assignable to the Coder class, one Coder parameter per type parameter
  3. Alternatively register via CoderProviders.forCoder(typeDescriptor, coderInstance) with a pre-built coder instance

Example fix

// before
public <T> MyCoder<T> of(Coder<T> componentCoder) { return new MyCoder<>(componentCoder); }
// after
public static <T> MyCoder<T> of(Coder<T> componentCoder) { return new MyCoder<>(componentCoder); }
Defensive patterns

Strategy: validation

Validate before calling

static boolean isOfStatic(Class<?> coderClazz) throws NoSuchMethodException {
  Class<?>[] argTypes = new Class<?>[coderClazz.getTypeParameters().length];
  java.util.Arrays.fill(argTypes, Coder.class);
  return java.lang.reflect.Modifier.isStatic(
      coderClazz.getDeclaredMethod("of", argTypes).getModifiers());
}

Try / catch

try {
  CoderProvider provider = CoderProviders.fromStaticMethods(rawType, coderClazz);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("is not static")) {
    throw new IllegalStateException("Fix: declare 'of' as static in " + coderClazz, e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Declaring `public MyCoder<T> of(Coder<T> component)` (no static keyword) in a Coder class and then calling CoderProviders.fromStaticMethods(rawType, coderClazz).

Common situations: Copying a factory method from another coder and dropping the static modifier; auto-generating factory methods (e.g. Lombok @Builder or IDE generation) that produce instance methods; converting a constructor to a factory without making it static.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/818dc80c05831871. Report an issue: GitHub.