{"id":"518b97522e02e1aa","repo":"apache/kafka","slug":"klass-is-not-an-instance-of-t-getname","errorCode":null,"errorMessage":"klass is not an instance of t.getName()","messagePattern":"klass is not an instance of t\\.getName\\(\\)","errorType":"exception","errorClass":"KafkaException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java","lineNumber":416,"sourceCode":"\n    private <T> T getConfiguredInstance(Object klass, Class<T> t, Map<String, Object> configPairs) {\n        if (klass == null)\n            return null;\n        Object o;\n\n        if (klass instanceof String) {\n            try {\n                o = Utils.newInstance((String) klass, t);\n            } catch (ClassNotFoundException e) {\n                throw new KafkaException(\"Class \" + klass + \" cannot be found\", e);\n            }\n        } else if (klass instanceof Class<?>) {\n            o = Utils.newInstance((Class<?>) klass);\n        } else\n            throw new KafkaException(\"Unexpected element of type \" + klass.getClass().getName() + \", expected String or Class\");\n        try {\n            if (!t.isInstance(o))\n                throw new KafkaException(klass + \" is not an instance of \" + t.getName());\n            if (o instanceof Configurable)\n                ((Configurable) o).configure(configPairs);\n        } catch (Exception e) {\n            maybeClose(o, \"AutoCloseable object constructed and configured during failed call to getConfiguredInstance\");\n            throw e;\n        }\n        return t.cast(o);\n    }\n\n    /**\n     * Get a configured instance of the give class specified by the given configuration key. If the object implements\n     * Configurable configure it using the configuration.\n     *\n     * @param key The configuration key for the class\n     * @param t   The interface the class should implement\n     * @return A configured instance of the class\n     */\n    public <T> T getConfiguredInstance(String key, Class<T> t) {","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java#L398-L434","documentation":"KafkaException thrown by getConfiguredInstance after successfully instantiating a class when the resulting object is not an instance of the expected super-type T (i.e. t.isInstance(o) is false). The expected interface name (t.getName()) and the class are both in the message. It is a type-assignment guard: the configured class loaded fine but does not implement/extend the interface Kafka requires for that slot (e.g. Partitioner, Serializer, Converter, Transform, ConfigProvider, MetricsReporter).","triggerScenarios":"A config key requiring a specific interface is pointed at a class that exists on the classpath but does not implement it: e.g. setting key.serializer to a class that does not implement org.apache.kafka.common.serialization.Serializer, or a Connect transform class that does not implement Transformation. The class instantiates via its no-arg/default constructor, then the isInstance check fails.","commonSituations":"See trigger scenarios.","solutions":["Confirm the configured class implements the required interface named in the message (Serializer, Deserializer, Partitioner, Converter, Transformation, ConfigProvider, MetricsReporter, etc.).","Check the fully-qualified name: a same-named class in the wrong package is a frequent cause.","If you wrote the class, add `implements <expected-interface>` and implement its methods.","Align versions: if the interface changed across Kafka versions, rebuild/upgrade the plugin against the matching kafka-clients version."],"exampleFix":"// before: configured class does not implement Serializer\npublic class MyJsonEncoder { /* encodes but is not a Serializer */ }\nprops.put(\"value.serializer\", \"com.example.MyJsonEncoder\");\n\n// after\npublic class MyJsonEncoder implements org.apache.kafka.common.serialization.Serializer<MyType> {\n    @Override public byte[] serialize(String topic, MyType data) { /* ... */ return null; }\n}\nprops.put(\"value.serializer\", \"com.example.MyJsonEncoder\");","handlingStrategy":"validation","validationCode":"// Verify the class is assignable to the expected interface before asking Kafka to build it.\nString className = String.valueOf(config.get(key));\nClass<?> candidate = Class.forName(className, false, Thread.currentThread().getContextClassLoader());\nif (!expectedInterface.isAssignableFrom(candidate)) {\n    throw new IllegalArgumentException(\n        className + \" does not implement \" + expectedInterface.getName());\n}\nT plugin = config.getConfiguredInstance(key, expectedInterface);","typeGuard":"boolean implementsT(Class<?> candidate, Class<?> t) {\n    return candidate != null && t.isAssignableFrom(candidate);\n}","tryCatchPattern":"try {\n    T plugin = config.getConfiguredInstance(key, T.class);\n} catch (org.apache.kafka.common.KafkaException ke) {\n    if (ke.getMessage().contains(\"is not an instance of\")) {\n        // right class name, wrong interface — e.g. a Serializer used where Deserializer was required\n        log.error(\"Plugin '{}' does not implement the required interface\", config.get(key));\n    } else {\n        throw ke;\n    }\n}","preventionTips":["Common cause: pairing a class with the wrong role (Serializer vs Deserializer, Partitioner vs ProducerInterceptor).","Keep one ConfigDef entry per role; don't reuse a key for multiple plugin types.","Add a unit test that loads each configured class and asserts isAssignableFrom against its expected interface."],"tags":["configuration","kafka-client","configdef","classloading","plugins"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}