{"record":{"id":"bf7480c651223cc1","repo":"dotnet/aspnetcore","slug":"cannot-handle-type-class-type-getclass","errorCode":null,"errorMessage":"Cannot handle type class: {type.getClass()}","messagePattern":"Cannot handle type class: (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/Utils.java","lineNumber":98,"sourceCode":"    public static Class<?> typeToClass(Type type) {\n        if (type == null) {\n            return null;\n        }\n        if (type instanceof Class) {\n            return (Class<?>) type;\n        } else if (type instanceof GenericArrayType) {\n            // Instantiate an array of the same type as this type, then return its class\n            return Array.newInstance(typeToClass(((GenericArrayType)type).getGenericComponentType()), 0).getClass();\n        } else if (type instanceof ParameterizedType) {\n            return typeToClass(((ParameterizedType) type).getRawType());\n        } else if (type instanceof TypeVariable) {\n            Type[] bounds = ((TypeVariable<?>) type).getBounds();\n            return bounds.length == 0 ? Object.class : typeToClass(bounds[0]);\n        } else if (type instanceof WildcardType) {\n            Type[] bounds = ((WildcardType) type).getUpperBounds();\n            return bounds.length == 0 ? Object.class : typeToClass(bounds[0]);\n        } else { \n            throw new UnsupportedOperationException(\"Cannot handle type class: \" + type.getClass());\n        }\n    }\n}\n","sourceCodeStart":80,"sourceCodeEnd":102,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/Utils.java#L80-L102","documentation":"Utils.typeToClass maps a java.lang.reflect.Type to a concrete Class for casting. It handles Class, GenericArrayType, ParameterizedType, TypeVariable, and WildcardType; any other Type implementation is rejected because the protocol has no way to cast the decoded value to it. The rendered message is the actual runtime class name (the catalog template {type.getClass()} expands via string concatenation: 'Cannot handle type class: ' + type.getClass()).","triggerScenarios":"A hub method parameter or return type resolves to an exotic Type that is not one of the five handled subtypes - e.g. a custom Type implementation, or a synthetic/generic-bridge type produced by some reflection libraries (Utils.java:94-98).","commonSituations":"Using a third-party TypeResolver, a TypeToken library, or Kotlin/Scala reified generics that surface as non-standard Type implementations; registering handlers with generic capture that resolves to an odd Type.","solutions":["Provide a concrete Class or a standard ParameterizedType (e.g. via TypeReference/ParameterizedTypeReference) for the hub method signature.","Avoid custom java.lang.reflect.Type implementations in hub contracts.","Wrap the argument in a plain POJO whose field types are concrete."],"exampleFix":"// before\nhubConnection.on(\"M\", (x) -> {}, customTypeImpl); // customTypeImpl instanceof MyType == true\n// after - use a plain Class\nhubConnection.on(\"M\", (x) -> {}, MyPojo.class);","handlingStrategy":"type-guard","validationCode":"static boolean isSupportedType(java.lang.reflect.Type t) {\n    return t instanceof Class || t instanceof java.lang.reflect.GenericArrayType\n        || t instanceof java.lang.reflect.ParameterizedType\n        || t instanceof java.lang.reflect.TypeVariable\n        || t instanceof java.lang.reflect.WildcardType;\n}","typeGuard":"static boolean isSupportedType(java.lang.reflect.Type t) {\n    return t instanceof Class || t instanceof java.lang.reflect.GenericArrayType\n        || t instanceof java.lang.reflect.ParameterizedType\n        || t instanceof java.lang.reflect.TypeVariable\n        || t instanceof java.lang.reflect.WildcardType;\n}","tryCatchPattern":"try {\n    Class<?> c = Utils.typeToClass(type);\n} catch (UnsupportedOperationException ex) {\n    // replace the exotic Type with a concrete Class before binding\n    throw new IllegalArgumentException(\"Use a concrete/ParameterizedType, got \" + type.getClass(), ex);\n}","preventionTips":["Prefer concrete Class or ParameterizedType (via TypeReference) for hub signatures.","Avoid custom java.lang.reflect.Type implementations in contracts.","Wrap exotic generics in a POJO with concrete field types."],"tags":["java","signalr","reflection","types","binding"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}