{"id":"7f636ae9a998dfbd","repo":"google/gson","slug":"class-typeadapter-getclass-getname-does-not","errorCode":null,"errorMessage":"Class {typeAdapter.getClass().getName()} does not implement any supported type adapter class or interface","messagePattern":"Class (.+?) does not implement any supported type adapter class or interface","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/GsonBuilder.java","lineNumber":747,"sourceCode":"   * TypeAdapter} should be used instead.\n   *\n   * @param type the type definition for the type adapter being registered\n   * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, {@link\n   *     InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.\n   * @return a reference to this {@code GsonBuilder} object to fulfill the \"Builder\" pattern\n   * @throws IllegalArgumentException if the type adapter being registered is for {@code Object}\n   *     class or {@link JsonElement} or any of its subclasses\n   * @see #registerTypeHierarchyAdapter(Class, Object)\n   */\n  @CanIgnoreReturnValue\n  public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {\n    Objects.requireNonNull(type);\n    Objects.requireNonNull(typeAdapter);\n    if (!(typeAdapter instanceof JsonSerializer<?>\n        || typeAdapter instanceof JsonDeserializer<?>\n        || typeAdapter instanceof InstanceCreator<?>\n        || typeAdapter instanceof TypeAdapter<?>)) {\n      throw new IllegalArgumentException(\n          \"Class \"\n              + typeAdapter.getClass().getName()\n              + \" does not implement any supported type adapter class or interface\");\n    }\n\n    if (hasNonOverridableAdapter(type)) {\n      throw new IllegalArgumentException(\"Cannot override built-in adapter for \" + type);\n    }\n\n    if (typeAdapter instanceof InstanceCreator<?>) {\n      instanceCreators.put(type, (InstanceCreator<?>) typeAdapter);\n    }\n    if (typeAdapter instanceof JsonSerializer<?> || typeAdapter instanceof JsonDeserializer<?>) {\n      TypeToken<?> typeToken = TypeToken.get(type);\n      factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(typeToken, typeAdapter));\n    }\n    if (typeAdapter instanceof TypeAdapter<?>) {\n      @SuppressWarnings({\"unchecked\", \"rawtypes\"})","sourceCodeStart":729,"sourceCodeEnd":765,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/GsonBuilder.java#L729-L765","documentation":"Thrown by registerTypeAdapter(Type, Object) when the supplied object implements none of TypeAdapter, JsonSerializer, JsonDeserializer, or InstanceCreator. Gson inspects the runtime type with instanceof and refuses to register an object it cannot dispatch on.","triggerScenarios":"Calling gsonBuilder.registerTypeAdapter(MyType.class, obj) where obj is a plain POJO, a lambda with no matching functional interface, a class whose generic implements were erased, or the Class literal instead of an instance.","commonSituations":"Forgetting to extend TypeAdapter<T> and only adding a method named write/read; passing new MyClass() where MyClass implements only a custom (non-Gson) interface; passing a JsonSerializer with a raw/missing type parameter; accidentally registering the adapter class object (FooAdapter.class) instead of new FooAdapter().","solutions":["Make the object implement at least one of TypeAdapter<?>, JsonSerializer<?>, JsonDeserializer<?>, or InstanceCreator<?>.","Confirm you are passing an instance, not a Class literal (drop the .class, add new).","If using generics, declare the type parameter on the implements clause, e.g. class FooAdapter extends TypeAdapter<Foo>.","If you want factory-based dispatch, use registerTypeAdapterFactory(factory) instead."],"exampleFix":"// before\ngsonBuilder.registerTypeAdapter(Foo.class, new FooSerializer()); // FooSerializer implements nothing Gson knows\n// after\nclass FooSerializer implements JsonSerializer<Foo> {\n  public JsonElement serialize(Foo src, Type t, JsonSerializationContext c) { ... }\n}\ngsonBuilder.registerTypeAdapter(Foo.class, new FooSerializer());","handlingStrategy":"validation","validationCode":"// Assert the contract before registering\nObject adapter = new FooAdapter();\nboolean ok = adapter instanceof com.google.gson.TypeAdapter\n         || adapter instanceof com.google.gson.JsonSerializer\n         || adapter instanceof com.google.gson.JsonDeserializer\n         || adapter instanceof com.google.gson.InstanceCreator;\nif (!ok) throw new IllegalStateException(\"adapter implements no Gson interface\");\ngsonBuilder.registerTypeAdapter(Foo.class, adapter);","typeGuard":"// Type guard narrowing to a Gson-supported adapter\nboolean isGsonAdapter(Object o) {\n  return o instanceof com.google.gson.TypeAdapter\n      || o instanceof com.google.gson.JsonSerializer\n      || o instanceof com.google.gson.JsonDeserializer\n      || o instanceof com.google.gson.InstanceCreator;\n}","tryCatchPattern":null,"preventionTips":["Implement the Gson interface on the adapter class, not just matching method names.","Pass new FooAdapter(), never FooAdapter.class.","Register adapters in a single builder-config method you can unit-test."],"tags":["gson","type-adapter","configuration","registration"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}