{"record":{"id":"cc22356b5a6a48ba","repo":"chinabugotech/hutool","slug":"can-not-cast-value-to","errorCode":null,"errorMessage":"Can not cast value to [{}]","messagePattern":"Can not cast value to \\[(.+?)\\]","errorType":"exception","errorClass":"ConvertException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/CastConverter.java","lineNumber":21,"sourceCode":"import cn.hutool.core.convert.AbstractConverter;\nimport cn.hutool.core.convert.ConvertException;\n\n/**\n * 强转转换器\n *\n * @author Looly\n * @param <T> 强制转换到的类型\n * @since 4.0.2\n */\npublic class CastConverter<T> extends AbstractConverter<T> {\n\tprivate static final long serialVersionUID = 1L;\n\n\tprivate Class<T> targetType;\n\n\t@Override\n\tprotected T convertInternal(Object value) {\n\t\t// 由于在AbstractConverter中已经有类型判断并强制转换，因此当在上一步强制转换失败时直接抛出异常\n\t\tthrow new ConvertException(\"Can not cast value to [{}]\", this.targetType);\n\t}\n\n\t@Override\n\tpublic Class<T> getTargetType() {\n\t\treturn this.targetType;\n\t}\n}\n","sourceCodeStart":3,"sourceCodeEnd":29,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/CastConverter.java#L3-L29","documentation":"CastConverter.convertInternal is a last-resort force-cast. AbstractConverter already attempted targetType.cast(value) before reaching convertInternal, so arriving here means the value is not assignable to targetType; it throws ConvertException naming the target type.","triggerScenarios":"A CastConverter (or registered cast path) is invoked on a value whose runtime class is not assignable to the configured targetType.","commonSituations":"Custom conversion chains that fall back to CastConverter; converting between unrelated class hierarchies (e.g. String to Integer via cast).","solutions":["Pre-check value instanceof targetType before attempting a cast conversion.","Use the proper typed converter (NumberConverter, etc.) instead of CastConverter for cross-type conversion.","Pass a defaultValue so AbstractConverter returns it rather than invoking convertInternal."],"exampleFix":"// before\nInteger i = castConverter.convert(\"abc\", null);\n\n// after - guard then convert properly\nInteger i = (value instanceof Integer) ? (Integer) value : NumberConverter.convert(value);","handlingStrategy":"type-guard","validationCode":"if (!castConverter.getTargetType().isInstance(value)) {\n    // use a real converter instead of a cast\n    return Convert.convert(value, castConverter.getTargetType());\n}","typeGuard":"castConverter.getTargetType() != null && castConverter.getTargetType().isInstance(value)","tryCatchPattern":"try {\n    return castConverter.convert(value, null);\n} catch (ConvertException e) {\n    if (e.getMessage().startsWith(\"Can not cast value to\")) { /* fall back to Convert */ }\n    else throw e;\n}","preventionTips":["Avoid CastConverter for cross-type conversion; use the typed converter for the source.","Pre-check instanceof before attempting a cast-based conversion."],"tags":["convert","cast","type-mismatch"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}