{"record":{"id":"cfc38c5bf2cc563d","repo":"stanfordnlp/CoreNLP","slug":"cannot-convert-type-to-class-type","errorCode":null,"errorMessage":"Cannot convert type to class: \" + type","messagePattern":"Cannot convert type to class: \" \\+ type","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":459,"sourceCode":"    return new MetaClass(clazz);\n  }\n\n  /**\n   * Utility method for cast\n   * @param type The type to cast into a class\n   * @return The class corresponding to the passed in type\n   */\n  private static Class <?> type2class(Type type){\n\t\tif(type instanceof Class <?>){\n\t\t\treturn (Class <?>) type;\t//base case\n\t\t}else if(type instanceof ParameterizedType){\n\t\t\treturn type2class( ((ParameterizedType) type).getRawType() );\n\t\t}else if(type instanceof TypeVariable<?>){\n\t\t\treturn type2class( ((TypeVariable<?>) type).getBounds()[0] );\n\t\t}else if(type instanceof WildcardType){\n\t\t\treturn type2class( ((WildcardType) type).getUpperBounds()[0] );\n\t\t}else{\n\t\t\tthrow new IllegalArgumentException(\"Cannot convert type to class: \" + type);\n\t\t}\n\t}\n\n  /**\n   * Cast a String representation of an object into that object.\n   * E.g. \"5.4\" will be cast to a Double; \"[1,2,3]\" will be cast\n   * to an Integer[].\n   *\n   * NOTE: Date parses from a Long\n   *\n   * @param <E> The type of the object returned (same as type)\n   * @param value The string representation of the object\n   * @param type The type (usually class) to be returned (same as E)\n   * @return An object corresponding to the String value passed\n   */\n  @SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n  public static <E> E cast(String value, Type type){\n    //--Get Type","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L441-L477","documentation":"MetaClass.type2class(Type) converts a java.lang.reflect Type (Class, ParameterizedType, TypeVariable, WildcardType) into its raw Class. If the Type is none of those recognized forms (e.g. a GenericArrayType like T[] or some custom Type implementation), it throws IllegalArgumentException. This is an internal utility hit when reflective generics contain shapes the helper does not handle.","triggerScenarios":"Calling type2class (directly or via cast helpers) with a GenericArrayType (e.g. String[]), a custom Type implementation, or any Type that is not Class/ParameterizedType/TypeVariable/WildcardType.","commonSituations":"Reflecting over a field or method with a generic array type; passing a Type obtained from exotic generic declarations into MetaClass casting utilities; library version differences producing new Type shapes.","solutions":["Avoid calling type2class with generic array types; unwrap arrays yourself via GenericArrayType handling","Convert array component types: extract the generic component type and call type2class on it","Inspect the offending Type's runtime class in a debugger before conversion","Upgrade/patch CoreNLP if your Type shape is legitimate but unsupported"],"exampleFix":"// before\nClass<?> c = MetaClass.type2class(field.getGenericType()); // String[] generic array -> throws\n// after\nType t = field.getGenericType();\nClass<?> c = (t instanceof GenericArrayType)\n    ? MetaClass.type2class(((GenericArrayType) t).getGenericComponentType())\n    : MetaClass.type2class(t);","handlingStrategy":"type-guard","validationCode":"static boolean isConvertable(Type t) {\n  return t instanceof Class || t instanceof ParameterizedType\n      || t instanceof TypeVariable<?> || t instanceof WildcardType;\n}","typeGuard":"static Class<?> safeType2class(Type t) {\n  if (t instanceof GenericArrayType) {\n    Class<?> comp = safeType2class(((GenericArrayType) t).getGenericComponentType());\n    return java.lang.reflect.Array.newInstance(comp, 0).getClass();\n  }\n  if (t instanceof Class) return (Class<?>) t;\n  if (t instanceof ParameterizedType) return safeType2class(((ParameterizedType) t).getRawType());\n  if (t instanceof TypeVariable<?>) return safeType2class(((TypeVariable<?>) t).getBounds()[0]);\n  if (t instanceof WildcardType) return safeType2class(((WildcardType) t).getUpperBounds()[0]);\n  throw new IllegalArgumentException(\"Unsupported type: \" + t);\n}","tryCatchPattern":"try {\n  Class<?> c = MetaClass.type2class(type);\n} catch (IllegalArgumentException e) {\n  logger.warning(\"Unhandled reflective Type: \" + type + \" (\" + type.getClass().getName() + \")\");\n  // fall back to Object.class or skip\n}","preventionTips":["Resolve TypeVariables against the declaring class before reflective conversion","Handle GenericArrayType explicitly in any Type-walking code","Prefer field.getType() over field.getGenericType() when generics are not needed"],"tags":["reflection","generics","java","argument"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}