{"record":{"id":"68f46146cf877ebb","repo":"dotnet/aspnetcore","slug":"cannot-handle-type-class","errorCode":null,"errorMessage":"Cannot handle type class: ","messagePattern":"Cannot handle type class: ","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/Utils.java","lineNumber":43,"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    @SuppressWarnings(\"unchecked\")\n    public static <T> T cast(Class<?> returnClass, Object obj) {\n        // Primitive types can't be cast with the Class cast function\n        if (returnClass.isPrimitive()) {\n            return (T) obj;\n        } else {\n            return (T)returnClass.cast(obj);\n        }\n    }\n\n    public static <T> T cast(Type returnType, Object obj) {\n        return cast(typeToClass(returnType), obj);\n    }\n}","sourceCodeStart":25,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/Utils.java#L25-L60","documentation":"Utils.typeToClass maps a java.lang.reflect.Type to its raw Class for casting invocation results. It handles Class, GenericArrayType, ParameterizedType, TypeVariable, and WildcardType. Any other Type implementation (rare - e.g. a custom Type) falls through to UnsupportedOperationException. The formatted message appends the runtime class of the unexpected Type.","triggerScenarios":"Calling invoke(stream) with a Type that is not one of the five supported kinds - typically a custom java.lang.reflect.Type implementation, or an edge case from a third-party generics library used to build the Type.","commonSituations":"Passing a custom Type captured via unusual reflection (e.g. gson's $Gson$Types or a hand-built ParameterizedType that does not implement the expected interfaces); library version producing a Type subclass the client did not anticipate.","solutions":["Pass a plain Class or a standard ParameterizedType built via TypeReference instead of a custom Type.","If you hold a custom Type, extract its raw class and pass that to the Class-based invoke overload.","Upgrade the signalr client - new Type kinds may be handled in later versions; if not, file an issue with the Type class."],"exampleFix":"// before\nType custom = someLibrary.buildType(); // not a standard Type\nhub.invoke(custom, \"Method\");\n\n// after\nhub.invoke(MyResult.class, \"Method\");\n// or\nhub.invoke(new TypeReference<MyResult>(){}.getType(), \"Method\");","handlingStrategy":"type-guard","validationCode":"Type t = /* from caller */;\nif (!(t instanceof Class || t instanceof ParameterizedType\n    || t instanceof GenericArrayType || t instanceof TypeVariable\n    || t instanceof WildcardType)) {\n  throw new IllegalArgumentException(\"Unsupported Type kind: \" + t.getClass());\n}","typeGuard":"static boolean isSupportedType(Type t) {\n  return t instanceof Class || t instanceof ParameterizedType\n      || t instanceof GenericArrayType || t instanceof TypeVariable\n      || t instanceof WildcardType;\n}","tryCatchPattern":null,"preventionTips":["Pass Class or TypeReference-derived Types only.","Avoid third-party generics utilities that mint custom Type subclasses.","Unit-test invoke() with each Type kind you intend to support."],"tags":["signalr","java","reflection","generics","api-misuse"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}