{"record":{"id":"e927c30a229c8057","repo":"stanfordnlp/CoreNLP","slug":"cannot-cast-to-type-unhandled-type-type","errorCode":null,"errorMessage":"Cannot cast to type (unhandled type): \" + type","messagePattern":"Cannot cast to type \\(unhandled type\\): \" \\+ type","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":485,"sourceCode":"   *\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\n    Class <?> clazz;\n    if (type instanceof Class) {\n      clazz = (Class <?>) type;\n    } else if (type instanceof ParameterizedType) {\n      ParameterizedType pt = (ParameterizedType) type;\n      clazz = (Class <?>) pt.getRawType();\n    } else {\n      throw new IllegalArgumentException(\"Cannot cast to type (unhandled type): \" + type);\n    }\n    //--Cast\n    if (String.class.isAssignableFrom(clazz)) {\n      // (case: String)\n      return (E) value;\n    } else if (Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz)) {\n      //(case: boolean)\n      if(\"1\".equals(value)){ return (E) Boolean.TRUE; }\n      return (E) Boolean.valueOf(Boolean.parseBoolean(value));\n    } else if (Integer.class.isAssignableFrom(clazz) || int.class.isAssignableFrom(clazz)) {\n      //(case: integer)\n      try {\n        return (E) Integer.valueOf(Integer.parseInt(value));\n      } catch (NumberFormatException e) {\n        return (E) Integer.valueOf((int) Double.parseDouble(value));\n      }\n    } else if (BigInteger.class.isAssignableFrom(clazz)) {\n      //(case: biginteger)","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L467-L503","documentation":"MetaClass.cast(String, Type) converts a string value into an object of the given reflective Type. It only understands Class and ParameterizedType as the type argument; anything else (e.g. TypeVariable, WildcardType, GenericArrayType) throws IllegalArgumentException 'Cannot cast to type (unhandled type)'. The library cannot know the runtime class to coerce the string into.","triggerScenarios":"Calling MetaClass.cast(value, type) where type is a TypeVariable, WildcardType, or GenericArrayType; calling castWithoutKnowingType paths that pass unresolved generic types.","commonSituations":"Reflecting over generic method parameters or fields with unresolved type variables; passing getGenericReturnType() results straight into cast; library code paths where generics were erased to variables.","solutions":["Resolve the TypeVariable/WildcardType to its upper bound (e.g. ((TypeVariable<?>) t).getBounds()[0]) before calling cast","Pass a concrete Class<?> or ParameterizedType instead of a raw unresolved Type","If the type comes from a field/method, use the raw (non-generic) type via getDeclaredField(...).getType()","Guard with 'if (type instanceof Class || type instanceof ParameterizedType)' before calling cast"],"exampleFix":"// before\nObject v = MetaClass.cast(\"5\", typeVar); // throws\n// after\nType t = (typeVar instanceof TypeVariable<?>) ? ((TypeVariable<?>) typeVar).getBounds()[0] : typeVar;\nObject v = MetaClass.cast(\"5\", t);","handlingStrategy":"type-guard","validationCode":"if (!(type instanceof Class || type instanceof ParameterizedType)) {\n  throw new IllegalArgumentException(\"cast() requires Class or ParameterizedType, got \" + type.getClass());\n}","typeGuard":"static boolean isCastableType(Type t) {\n  return t instanceof Class || t instanceof ParameterizedType;\n}","tryCatchPattern":"try {\n  E v = MetaClass.cast(str, type);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"unhandled type\")) {\n    Type resolved = resolveToBounds(type); // unwrap TypeVariable/WildcardType first\n    v = MetaClass.cast(str, resolved);\n  } else throw e;\n}","preventionTips":["Always resolve generics (bounds) to concrete Classes before casting strings","Pass raw types when generic information is unnecessary","Write a small unwrap helper for TypeVariable/WildcardType and reuse it"],"tags":["reflection","generics","java","type-conversion"],"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"}