{"record":{"id":"38a0e5ec00e9aae0","repo":"apache/flink","slug":"cannot-convert-type-to-class","errorCode":null,"errorMessage":"Cannot convert type to class","messagePattern":"Cannot convert type to class","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java","lineNumber":285,"sourceCode":"    public static List<Method> getAllDeclaredMethods(Class<?> clazz) {\n        List<Method> result = new ArrayList<>();\n        while (clazz != null) {\n            Method[] methods = clazz.getDeclaredMethods();\n            Collections.addAll(result, methods);\n            clazz = clazz.getSuperclass();\n        }\n        return result;\n    }\n\n    /** Convert ParameterizedType or Class to a Class. */\n    @SuppressWarnings(\"unchecked\")\n    public static <T> Class<T> typeToClass(Type t) {\n        if (t instanceof Class) {\n            return (Class<T>) t;\n        } else if (t instanceof ParameterizedType) {\n            return ((Class<T>) ((ParameterizedType) t).getRawType());\n        }\n        throw new IllegalArgumentException(\"Cannot convert type to class\");\n    }\n\n    /**\n     * Checks if a type can be converted to a Class. This is true for ParameterizedType and Class.\n     */\n    public static boolean isClassType(Type t) {\n        return t instanceof Class<?> || t instanceof ParameterizedType;\n    }\n\n    /** Checks whether two types are type variables describing the same. */\n    public static boolean sameTypeVars(Type t1, Type t2) {\n        return t1 instanceof TypeVariable\n                && t2 instanceof TypeVariable\n                && ((TypeVariable<?>) t1).getName().equals(((TypeVariable<?>) t2).getName())\n                && ((TypeVariable<?>) t1)\n                        .getGenericDeclaration()\n                        .equals(((TypeVariable<?>) t2).getGenericDeclaration());\n    }","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java#L267-L303","documentation":"Thrown by TypeExtractionUtils.typeToClass when the given Type is neither a Class nor a ParameterizedType. This covers TypeVariable (unresolved generic type variables like T), GenericArrayType, or WildcardType. The method only knows how to extract a raw Class from Class or ParameterizedType; all other Type subtypes are rejected.","triggerScenarios":"Type extraction encounters an unresolved TypeVariable (T) because the generic was never bound to a concrete type through the hierarchy. A GenericArrayType (T[]) is passed where a simple type was expected. The type hierarchy has a gap where a type variable is not resolved.","commonSituations":"Function generics not fully resolved (e.g. class MyMapper<T> implements MapFunction<T, String> where T is never bound). Using arrays of generic types. Deep generic inheritance chains where type variables are not resolved at some level.","solutions":["Bind all generic type parameters to concrete types (e.g. class MyMapper implements MapFunction<String, Integer>).","Use an anonymous class with explicit type arguments.","Specify the type information explicitly with .returns(TypeInformation.of(...)).","Implement ResultTypeQueryable to bypass automatic extraction."],"exampleFix":"// before\npublic class MyMapper<T> implements MapFunction<T, String> {\n    public String map(T value) { return value.toString(); }\n}\n// after\npublic class MyMapper implements MapFunction<String, String> {\n    public String map(String value) { return value.toUpperCase(); }\n}","handlingStrategy":"type-guard","validationCode":"if (!(t instanceof Class) && !(t instanceof ParameterizedType)) {\n    throw new IllegalArgumentException(\n        \"Cannot convert type \" + t + \" to Class; must be Class or ParameterizedType\");\n}","typeGuard":"static boolean isClassType(Type t) {\n    return t instanceof Class<?> || t instanceof ParameterizedType;\n}","tryCatchPattern":null,"preventionTips":["Bind all generic type variables to concrete types in function classes.","Use anonymous classes with explicit generics instead of raw type variables.","Implement ResultTypeQueryable when generics cannot be resolved."],"tags":["type-extraction","generics","type-variable","reflection"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}