{"record":{"id":"b00e73c4ec22ac87","repo":"chinabugotech/hutool","slug":"is-not-a-primitive-class","errorCode":null,"errorMessage":"[{}] is not a primitive class!","messagePattern":"\\[(.+?)\\] is not a primitive class!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java","lineNumber":42,"sourceCode":" *\n * @author Looly\n */\npublic class PrimitiveConverter extends AbstractConverter<Object> {\n\tprivate static final long serialVersionUID = 1L;\n\n\tprivate final Class<?> targetType;\n\n\t/**\n\t * 构造<br>\n\t *\n\t * @param clazz 需要转换的原始\n\t * @throws IllegalArgumentException 传入的转换类型非原始类型时抛出\n\t */\n\tpublic PrimitiveConverter(Class<?> clazz) {\n\t\tif (null == clazz) {\n\t\t\tthrow new NullPointerException(\"PrimitiveConverter not allow null target type!\");\n\t\t} else if (false == clazz.isPrimitive()) {\n\t\t\tthrow new IllegalArgumentException(\"[\" + clazz + \"] is not a primitive class!\");\n\t\t}\n\t\tthis.targetType = clazz;\n\t}\n\n\t@Override\n\tprotected Object convertInternal(Object value) {\n\t\treturn PrimitiveConverter.convert(value, this.targetType, this::convertToStr);\n\t}\n\n\t@Override\n\tprotected String convertToStr(Object value) {\n\t\treturn StrUtil.trim(super.convertToStr(value));\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tpublic Class<Object> getTargetType() {\n\t\treturn (Class<Object>) this.targetType;","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/PrimitiveConverter.java#L24-L60","documentation":"Thrown by the PrimitiveConverter constructor when the class passed in is not a Java primitive type (byte, short, int, long, float, double, char, boolean). The constructor explicitly checks clazz.isPrimitive() and rejects wrapper classes (Integer.class, String.class, etc.) or any reference type. This is a hard precondition — PrimitiveConverter only handles the eight primitive class literals.","triggerScenarios":"Directly instantiating 'new PrimitiveConverter(Integer.class)' or 'new PrimitiveConverter(String.class)'. Indirectly triggered when ConverterRegistry or Convert.convert routes a target type to PrimitiveConverter with a non-primitive Class object (e.g., passing Integer.class where int.class was intended, or a custom converter registration mismatch).","commonSituations":"Passing a wrapper class (Integer.class) instead of the primitive literal (int.class). Dynamically deriving the target Class from reflection (Field.getType() on a generic field) and handing it to the converter without checking isPrimitive(). Custom ConverterRegistry registrations that misroute types.","solutions":["Pass the primitive class literal: use int.class, long.class, byte.class, etc. instead of Integer.class, Long.class.","If you hold an arbitrary Class<?>, gate it with clazz.isPrimitive() before constructing PrimitiveConverter.","For wrapper types, use the corresponding NumberConverter or Convert.convert(WrapperType.class, value) directly instead of PrimitiveConverter.","If the class comes from reflection, resolve it to its primitive equivalent via a lookup or check Class.isPrimitive() first and fall back to the wrapper converter."],"exampleFix":"// before\nnew PrimitiveConverter(Integer.class); // throws\n\n// after\nnew PrimitiveConverter(int.class);\n// or for wrapper types:\nConvert.convert(Integer.class, value);","handlingStrategy":"validation","validationCode":"if (clazz == null || !clazz.isPrimitive()) {\n    // use NumberConverter or other appropriate converter\n    throw new IllegalArgumentException(\"Class must be primitive\");\n}\nnew PrimitiveConverter(clazz);","typeGuard":"static boolean isSupportedPrimitive(Class<?> clazz) {\n    return clazz != null && clazz.isPrimitive() && clazz != void.class;\n}","tryCatchPattern":"try {\n    new PrimitiveConverter(targetClass);\n} catch (IllegalArgumentException e) {\n    // fall back to wrapper conversion\n    Convert.convert(wrapperFor(targetClass), value);\n}","preventionTips":["Always use primitive class literals (int.class) not wrappers (Integer.class) when targeting PrimitiveConverter.","Gate arbitrary Class<?> inputs with isPrimitive() before constructing the converter.","Prefer the high-level Convert.convert() facade which routes to the correct converter automatically."],"tags":["convert","primitive-type","constructor","argument-validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}