{"record":{"id":"1e39a5124e350b3a","repo":"chinabugotech/hutool","slug":"record-class-has-no-components","errorCode":null,"errorMessage":"Record class [] has no components","messagePattern":"Record class \\[\\] has no components","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/bean/RecordUtil.java","lineNumber":102,"sourceCode":"\t\t\t\tReflectUtil.invoke(components[i], METHOD_COMPONENT_GET_NAME),\n\t\t\t\tReflectUtil.invoke(components[i], METHOD_COMPONENT_GET_GENERIC_TYPE)\n\t\t\t);\n\t\t}\n\n\t\treturn entries;\n\t}\n\n\t/**\n\t * 实例化Record类\n\t *\n\t * @param recordClass   类\n\t * @param valueProvider 参数值提供器\n\t * @return Record类\n\t */\n\tpublic static Object newInstance(final Class<?> recordClass, final ValueProvider<String> valueProvider) {\n\t\tfinal Map.Entry<String, Type>[] recordComponents = getRecordComponents(recordClass);\n\t\tif(ArrayUtil.isEmpty(recordComponents)){\n\t\t\tthrow new IllegalArgumentException(\"Record class [\" + recordClass.getName() + \"] has no components\");\n\t\t}\n\t\tfinal Class<?>[] argTypes = new Class<?>[recordComponents.length];\n\t\tfinal Object[] args = new Object[recordComponents.length];\n\t\tfor (int i = 0; i < args.length; i++) {\n\t\t\targTypes[i] = TypeUtil.getClass(recordComponents[i].getValue());\n\t\t\targs[i] = valueProvider.value(recordComponents[i].getKey(), recordComponents[i].getValue());\n\t\t}\n\n\t\treturn ReflectUtil.newInstance(recordClass, argTypes, args);\n\t}\n}\n","sourceCodeStart":84,"sourceCodeEnd":114,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/bean/RecordUtil.java#L84-L114","documentation":"RecordUtil.newInstance(recordClass, valueProvider) instantiates a Java record via its canonical constructor using component values from the provider. If the record declares no components (an empty record), getRecordComponents returns an empty array and the method throws IllegalArgumentException because there is nothing to populate. Note the message hardcodes the class name in brackets.","triggerScenarios":"Calling RecordUtil.newInstance on `record Empty() {}` or any record with zero components. Also triggered if a non-record class is passed and reflection yields no record components.","commonSituations":"Generic record-handling utilities that accept arbitrary record types; passing a class that is not actually a record (getRecordComponents returns empty for non-records in some JVM paths).","solutions":["Guard with a check: skip instantiation for records with no components (call ReflectUtil.newInstance(recordClass) directly).","Verify recordClass.isRecord() and getRecordComponents().length > 0 before calling.","Ensure the target type is a real record with at least one component."],"exampleFix":"// before\nObject o = RecordUtil.newInstance(EmptyRecord.class, provider);\n// after\nClass<?> rc = EmptyRecord.class;\nObject o = RecordUtil.getRecordComponents(rc).length == 0\n    ? ReflectUtil.newInstance(rc)\n    : RecordUtil.newInstance(rc, provider);","handlingStrategy":"validation","validationCode":"Class<?> rc = recordClass;\nif (!rc.isRecord() || RecordUtil.getRecordComponents(rc).length == 0) throw new IllegalArgumentException(\"not a record with components: \" + rc);","typeGuard":"static boolean isNonEmptyRecord(Class<?> c) { return c.isRecord() && c.getRecordComponents() != null && c.getRecordComponents().length > 0; }","tryCatchPattern":null,"preventionTips":["Guard empty records before calling RecordUtil.newInstance.","Verify isRecord() and component count for generic record utilities."],"tags":["record","reflection","argument-validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}