prestodb/presto · error · IllegalArgumentException

Expected a Class type for javaType, but got:

Error message

Expected a Class type for javaType, but got: 

What it means

GenericThriftCodec requires the javaType argument to be a concrete Class so the ThriftCodecManager can resolve a codec for it. Passing a parameterized Type (e.g. a generic List<T> type token) or other Type implementation fails this check.

Source

Thrown at presto-thrift-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/GenericThriftCodec.java:40

import java.lang.reflect.Type;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_ARGUMENTS;
import static com.facebook.presto.thrift.codec.ThriftCodecUtils.fromThrift;
import static com.facebook.presto.thrift.codec.ThriftCodecUtils.toThrift;
import static java.util.Objects.requireNonNull;

public class GenericThriftCodec<T>
        implements ConnectorCodec<T>
{
    private final ThriftCodec<T> thriftCodec;

    public GenericThriftCodec(ThriftCodecManager codecManager, Type javaType)
    {
        requireNonNull(codecManager, "codecManager is null");
        requireNonNull(javaType, "javaType is null");

        if (!(javaType instanceof Class<?>)) {
            throw new IllegalArgumentException("Expected a Class type for javaType, but got: " + javaType.getTypeName());
        }

        Class<?> clazz = (Class<?>) javaType;

        this.thriftCodec = (ThriftCodec<T>) codecManager.getCodec(clazz);
    }

    @Override
    public byte[] serialize(T value)
    {
        try {
            return toThrift(value, thriftCodec);
        }
        catch (TProtocolException e) {
            throw new PrestoException(INVALID_ARGUMENTS, "Unable to serialize object of type " + value.getClass().getSimpleName(), e);
        }
    }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Pass the raw Class object (e.g. MyEvent.class) instead of a parameterized Type
  2. Register a specific codec for the generic type with the ThriftCodecManager and wrap it directly
  3. Use a dedicated codec class for the collection/generic type

Example fix

// before
new GenericThriftCodec(manager, new TypeToken<List<MyEvent>>(){}.getType());
// after
new GenericThriftCodec(manager, MyEvent.class);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(javaType instanceof Class<?>)) { throw new IllegalArgumentException("GenericThriftCodec requires a raw Class, got: " + javaType.getTypeName()); }

Type guard

boolean isRawClass(Type t) { return t instanceof Class<?>; }

Try / catch

try { codec = new GenericThriftCodec<>(manager, javaType); } catch (IllegalArgumentException e) { log.error("Pass a raw Class to GenericThriftCodec"); throw e; }

Prevention

When it happens

Trigger: new GenericThriftCodec(codecManager, someGenericType) where javaType is a ParameterizedType or GenericArrayType rather than a plain Class object.

Common situations: Registering codecs for generic Java types in thrift connector toolkit setup, or passing TypeToken.getType() results instead of raw classes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/2b3a2c05dbff4661. Report an issue: GitHub.