{"record":{"id":"475a832b0858bcc7","repo":"chinabugotech/hutool","slug":"can-not-convert-to-475a83","errorCode":null,"errorMessage":"Can not convert {} to {}","messagePattern":"Can not convert (.+?) to (.+?)","errorType":"exception","errorClass":"ConvertException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/EnumConverter.java","lineNumber":53,"sourceCode":"\t * @param enumClass 转换成的目标Enum类\n\t */\n\tpublic EnumConverter(Class enumClass) {\n\t\tthis.enumClass = enumClass;\n\t}\n\n\t@Override\n\tprotected Object convertInternal(Object value) {\n\t\tEnum enumValue = tryConvertEnum(value, this.enumClass);\n\t\tif (enumValue == null && !(value instanceof String)) {\n\t\t\t// 最后尝试先将value转String，再valueOf转换\n\t\t\tenumValue = Enum.valueOf(this.enumClass, convertToStr(value));\n\t\t}\n\n\t\tif (null != enumValue) {\n\t\t\treturn enumValue;\n\t\t}\n\n\t\tthrow new ConvertException(\"Can not convert {} to {}\", value, this.enumClass);\n\t}\n\n\t@Override\n\tpublic Class getTargetType() {\n\t\treturn this.enumClass;\n\t}\n\n\t/**\n\t * 尝试转换，转换规则为：\n\t * <ul>\n\t *     <li>如果实现{@link EnumItem}接口，则调用fromInt或fromStr转换</li>\n\t *     <li>找到类似转换的静态方法调用实现转换且优先使用</li>\n\t *     <li>约定枚举类应该提供 valueOf(String) 和 valueOf(Integer)用于转换</li>\n\t *     <li>oriInt /name 转换托底</li>\n\t * </ul>\n\t *\n\t * @param value     被转换的值\n\t * @param enumClass enum类","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/EnumConverter.java#L35-L71","documentation":"EnumConverter.convertInternal tries EnumItem conversion, then a static valueOf-like method on the enum, then Enum.valueOf. If none yields a constant it throws ConvertException with the value and enum class.","triggerScenarios":"Calling Convert.convert(\"BLUE\", Color.class) when Color has no constant named BLUE; converting an int that matches no enum ordinal/EnumItem int code; case mismatch (\"blue\" vs \"BLUE\").","commonSituations":"User input to an enum where the value does not exist; external data using a different casing or synonym; numeric value out of the enum's range.","solutions":["Validate the value against EnumUtil.getNames(enumClass) / fromString before converting.","Pass a defaultValue so AbstractConverter returns it instead of throwing.","Normalize input (trim, uppercase) to match constant names, or have your enum implement EnumItem for int/string mapping."],"exampleFix":"// before\nColor c = Convert.convert(userInput, Color.class);\n\n// after - validate then convert\nString name = userInput.trim().toUpperCase();\nif (!EnumUtil.getNames(Color.class).contains(name)) {\n    return Color.DEFAULT;\n}\nColor c = Convert.convert(name, Color.class);","handlingStrategy":"validation","validationCode":"if (value instanceof String) {\n    String name = ((String) value).trim().toUpperCase();\n    if (!EnumUtil.getNames(enumClass).contains(name)) {\n        return defaultValue; // or throw a controlled error\n    }\n}\nConvert.convert(value, enumClass);","typeGuard":null,"tryCatchPattern":"try {\n    return Convert.convert(value, enumClass);\n} catch (ConvertException e) {\n    if (e.getMessage().startsWith(\"Can not convert\") && e.getMessage().contains(enumClass.getSimpleName())) {\n        return defaultValue;\n    }\n    throw e;\n}","preventionTips":["Validate enum input against EnumUtil.getNames / fromString at the boundary.","Normalize casing (trim + uppercase) before converting.","Have domain enums implement EnumItem for stable int/str mapping."],"tags":["convert","enum","validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}