{"record":{"id":"97d28a69f9a31b63","repo":"chinabugotech/hutool","slug":"unsupport-number-type","errorCode":null,"errorMessage":"Unsupport Number type: {}","messagePattern":"Unsupport Number type: (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java","lineNumber":220,"sourceCode":"\t\t\t\tfinal DoubleAdder doubleAdder = new DoubleAdder();\n\t\t\t\tdoubleAdder.add(number.doubleValue());\n\t\t\t\treturn doubleAdder;\n\t\t\t}\n\t\t} else if (BigDecimal.class == targetType) {\n\t\t\treturn toBigDecimal(value, toStrFunc);\n\t\t} else if (BigInteger.class == targetType) {\n\t\t\treturn toBigInteger(value, toStrFunc);\n\t\t} else if (Number.class == targetType) {\n\t\t\tif (value instanceof Number) {\n\t\t\t\treturn (Number) value;\n\t\t\t} else if (value instanceof Boolean) {\n\t\t\t\treturn BooleanUtil.toInteger((Boolean) value);\n\t\t\t}\n\t\t\tfinal String valueStr = toStrFunc.apply((value));\n\t\t\treturn StrUtil.isBlank(valueStr) ? null : NumberUtil.parseNumber(valueStr);\n\t\t}\n\n\t\tthrow new UnsupportedOperationException(StrUtil.format(\"Unsupport Number type: {}\", targetType.getName()));\n\t}\n\n\t/**\n\t * 转换为BigDecimal<br>\n\t * 如果给定的值为空，或者转换失败，返回默认值<br>\n\t * 转换失败不会报错\n\t *\n\t * @param value     被转换的值\n\t * @param toStrFunc 转换为字符串的函数规则\n\t * @return 结果\n\t */\n\tprivate static BigDecimal toBigDecimal(Object value, Function<Object, String> toStrFunc) {\n\t\tif (value instanceof Number) {\n\t\t\treturn NumberUtil.toBigDecimal((Number) value);\n\t\t} else if (value instanceof Boolean) {\n\t\t\treturn ((boolean) value) ? BigDecimal.ONE : BigDecimal.ZERO;\n\t\t}\n","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/NumberConverter.java#L202-L238","documentation":"NumberConverter handles Byte, Short, Integer, AtomicInteger, Long, AtomicLong, Float, Double, DoubleAdder, LongAdder, BigDecimal, BigInteger, and the generic Number supertype. Any other Number subclass (or class passed in) as targetType throws UnsupportedOperationException.","triggerScenarios":"Constructing new NumberConverter(customNumberSubclass.class) where the subclass is not in the supported set; converting to a third-party Number type.","commonSituations":"Custom Number subclasses, third-party numeric wrappers, or accidentally passing a non-Number class.","solutions":["Target a supported boxed type (Long/Double/BigDecimal) then adapt to your type.","Register a custom Converter for your Number subtype via ConverterRegistry.putCustom.","If you only need a generic number, target Number.class which uses NumberUtil.parseNumber."],"exampleFix":"// before\nMyNum n = Convert.convert(\"42\", MyNum.class); // MyNum extends Number\n\n// after - supported type then adapt\nBigDecimal bd = Convert.convert(\"42\", BigDecimal.class);\nMyNum n = new MyNum(bd);","handlingStrategy":"validation","validationCode":"private static final Set<Class<?>> SUPPORTED = Set.of(\n        Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class,\n        BigDecimal.class, BigInteger.class, Number.class,\n        AtomicInteger.class, AtomicLong.class, DoubleAdder.class, LongAdder.class);\nif (!SUPPORTED.contains(targetType)) {\n    targetType = BigDecimal.class; // safe generic fallback\n}\nnew NumberConverter(targetType);","typeGuard":null,"tryCatchPattern":"try {\n    return new NumberConverter(targetType).convert(value, null);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().startsWith(\"Unsupport Number type\")) {\n        return new NumberConverter(BigDecimal.class).convert(value, null);\n    }\n    throw e;\n}","preventionTips":["Restrict target Number types to the supported boxed set.","Fall back to BigDecimal for arbitrary precision, then adapt to your subtype."],"tags":["convert","number","unsupported-type"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}